ImageView: handle setScaleType where it maps trivially to Gtk

This commit is contained in:
Mis012
2024-04-10 23:48:07 +02:00
parent b12cebe66a
commit fefd2f108b
3 changed files with 57 additions and 6 deletions

View File

@@ -223,6 +223,14 @@ JNIEXPORT void JNICALL Java_android_widget_ImageView_native_1setPixbuf
JNIEXPORT void JNICALL Java_android_widget_ImageView_native_1setDrawable
(JNIEnv *, jobject, jlong, jlong);
/*
* Class: android_widget_ImageView
* Method: native_setScaleType
* Signature: (JI)V
*/
JNIEXPORT void JNICALL Java_android_widget_ImageView_native_1setScaleType
(JNIEnv *, jobject, jlong, jint);
#ifdef __cplusplus
}
#endif

View File

@@ -7,6 +7,15 @@
#include "../generated_headers/android_widget_ImageView.h"
#define SCALE_TYPE_MATRIX 0
#define SCALE_TYPE_FIT_XY 1
#define SCALE_TYPE_FIT_START 2
#define SCALE_TYPE_FIT_CENTER 3
#define SCALE_TYPE_FIT_END 4
#define SCALE_TYPE_CENTER 5
#define SCALE_TYPE_CENTER_CROP 6
#define SCALE_TYPE_CENTER_INSIDE 7
JNIEXPORT jlong JNICALL Java_android_widget_ImageView_native_1constructor(JNIEnv *env, jobject this, jobject context, jobject attrs)
{
GtkWidget *wrapper = g_object_ref(wrapper_widget_new());
@@ -29,3 +38,24 @@ JNIEXPORT void JNICALL Java_android_widget_ImageView_native_1setDrawable(JNIEnv
GdkPaintable *paintable = _PTR(paintable_ptr);
gtk_picture_set_paintable(picture, paintable);
}
JNIEXPORT void JNICALL Java_android_widget_ImageView_native_1setScaleType(JNIEnv *env, jobject this, jlong widget_ptr, jint scale_type)
{
GtkPicture *picture = _PTR(widget_ptr);
/* TODO: somehow handle all the types */
switch (scale_type) {
case SCALE_TYPE_FIT_XY:
gtk_picture_set_content_fit(picture, GTK_CONTENT_FIT_FILL);
break;
case SCALE_TYPE_CENTER:
/* should probably let it overflow instead */
gtk_picture_set_content_fit(picture, GTK_CONTENT_FIT_SCALE_DOWN);
break;
case SCALE_TYPE_CENTER_CROP:
gtk_picture_set_content_fit(picture, GTK_CONTENT_FIT_COVER);
break;
case SCALE_TYPE_CENTER_INSIDE:
gtk_picture_set_content_fit(picture, GTK_CONTENT_FIT_CONTAIN);
break;
}
}

View File

@@ -31,15 +31,11 @@ public class ImageView extends View {
haveCustomMeasure = false;
TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.ImageView, defStyleAttr, 0);
setImageDrawable(a.getDrawable(com.android.internal.R.styleable.ImageView_src));
setScaleType(scaletype_from_int[a.getInt(com.android.internal.R.styleable.ImageView_scaleType, 3 /*CENTER*/)]);
a.recycle();
}
@Override
protected native long native_constructor(Context context, AttributeSet attrs);
protected native void native_setPixbuf(long pixbuf);
protected native void native_setDrawable(long widget, long paintable);
public /*native*/ void setImageResource(final int resid) {
public void setImageResource(final int resid) {
if (Context.this_application.getResources().getString(resid).endsWith(".xml")) {
setImageDrawable(getResources().getDrawable(resid));
return;
@@ -51,6 +47,7 @@ public class ImageView extends View {
public void setScaleType(ScaleType scaleType) {
this.scaleType = scaleType;
native_setScaleType(widget, scaleType.nativeInt);
}
public ScaleType getScaleType() {
@@ -138,6 +135,16 @@ public class ImageView extends View {
}
final int nativeInt;
}
private final ScaleType[] scaletype_from_int = {
ScaleType.MATRIX,
ScaleType.FIT_XY,
ScaleType.FIT_START,
ScaleType.FIT_CENTER,
ScaleType.FIT_END,
ScaleType.CENTER,
ScaleType.CENTER_CROP,
ScaleType.CENTER_INSIDE,
};
public final void setColorFilter(int color, PorterDuff.Mode mode) {}
@@ -150,4 +157,10 @@ public class ImageView extends View {
public void setMaxHeight(int height) {}
public void setImageState(int[] state, boolean merge) {}
@Override
protected native long native_constructor(Context context, AttributeSet attrs);
protected native void native_setPixbuf(long pixbuf);
protected native void native_setDrawable(long widget, long paintable);
protected native void native_setScaleType(long widget, int scale_type);
}