Files
android_translation_layer/src/api-impl/android/widget/ImageView.java

206 lines
6.2 KiB
Java
Raw Normal View History

package android.widget;
import android.content.Context;
2023-09-19 23:22:21 +02:00
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
2024-12-13 21:34:35 +01:00
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.PorterDuff;
2024-12-13 21:34:35 +01:00
import android.graphics.PorterDuffColorFilter;
2023-08-06 14:27:30 +02:00
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
public class ImageView extends View {
2023-08-06 14:27:30 +02:00
private Bitmap bitmap = null;
private ScaleType scaleType = ScaleType.FIT_CENTER;
private Drawable drawable = null;
2024-12-13 21:34:35 +01:00
private ColorFilter colorFilter = null;
2023-08-06 14:27:30 +02:00
public ImageView(Context context) {
this(context, null);
}
public ImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ImageView(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public ImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
2023-01-09 12:07:57 +01:00
haveCustomMeasure = false;
TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.ImageView, defStyleAttr, 0);
2024-12-13 21:34:35 +01:00
if (a.hasValue(com.android.internal.R.styleable.ImageView_tint))
colorFilter = new PorterDuffColorFilter(a.getColor(com.android.internal.R.styleable.ImageView_tint, 0),
PorterDuff.Mode.values()[a.getInt(com.android.internal.R.styleable.ImageView_tintMode, PorterDuff.Mode.SRC_IN.nativeInt)]);
setImageDrawable(a.getDrawable(com.android.internal.R.styleable.ImageView_src));
if (a.hasValue(com.android.internal.R.styleable.ImageView_scaleType))
setScaleType(scaletype_from_int[a.getInt(com.android.internal.R.styleable.ImageView_scaleType, 3 /*CENTER*/)]);
a.recycle();
}
public void setImageResource(final int resid) {
if (Context.this_application.getResources().getString(resid).endsWith(".xml")) {
setImageDrawable(getResources().getDrawable(resid));
return;
}
2023-08-06 14:27:30 +02:00
bitmap = BitmapFactory.decodeResource(Context.this_application.getResources(), resid);
native_setDrawable(widget, bitmap.getTexture());
}
public void setAdjustViewBounds(boolean adjustViewBounds) {}
public void setScaleType(ScaleType scaleType) {
this.scaleType = scaleType;
native_setScaleType(widget, scaleType.nativeInt);
}
public ScaleType getScaleType() {
return scaleType;
}
2023-08-06 14:27:30 +02:00
public Drawable getDrawable() {
return drawable;
2023-08-06 14:27:30 +02:00
}
public void setImageDrawable(Drawable drawable) {
if (drawable != null && colorFilter != null) {
2024-12-13 21:34:35 +01:00
drawable = drawable.mutate();
drawable.setColorFilter(colorFilter);
}
if (this.drawable != null) {
this.drawable.setCallback(null);
}
this.drawable = drawable;
if (drawable != null) {
2024-04-03 01:54:30 +02:00
drawable.setCallback(this);
}
native_setDrawable(widget, drawable != null ? drawable.paintable : 0);
}
public void setImageMatrix(Matrix matrix) {}
public void setImageBitmap(Bitmap bitmap) {
if (bitmap != null)
native_setDrawable(widget, bitmap.getTexture());
}
/**
* Options for scaling the bounds of an image to the bounds of this view.
*/
public enum ScaleType {
/**
* Scale using the image matrix when drawing. The image matrix can be set using
* {@link ImageView#setImageMatrix(Matrix)}. From XML, use this syntax:
* <code>android:scaleType="matrix"</code>.
*/
MATRIX(0),
/**
* Scale the image using {@link Matrix.ScaleToFit#FILL}.
* From XML, use this syntax: <code>android:scaleType="fitXY"</code>.
*/
FIT_XY(1),
/**
* Scale the image using {@link Matrix.ScaleToFit#START}.
* From XML, use this syntax: <code>android:scaleType="fitStart"</code>.
*/
FIT_START(2),
/**
* Scale the image using {@link Matrix.ScaleToFit#CENTER}.
* From XML, use this syntax:
* <code>android:scaleType="fitCenter"</code>.
*/
FIT_CENTER(3),
/**
* Scale the image using {@link Matrix.ScaleToFit#END}.
* From XML, use this syntax: <code>android:scaleType="fitEnd"</code>.
*/
FIT_END(4),
/**
* Center the image in the view, but perform no scaling.
* From XML, use this syntax: <code>android:scaleType="center"</code>.
*/
CENTER(5),
/**
* Scale the image uniformly (maintain the image's aspect ratio) so
* that both dimensions (width and height) of the image will be equal
* to or larger than the corresponding dimension of the view
* (minus padding). The image is then centered in the view.
* From XML, use this syntax: <code>android:scaleType="centerCrop"</code>.
*/
CENTER_CROP(6),
/**
* Scale the image uniformly (maintain the image's aspect ratio) so
* that both dimensions (width and height) of the image will be equal
* to or less than the corresponding dimension of the view
* (minus padding). The image is then centered in the view.
* From XML, use this syntax: <code>android:scaleType="centerInside"</code>.
*/
CENTER_INSIDE(7);
ScaleType(int ni) {
nativeInt = ni;
}
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) {}
2023-09-19 23:22:21 +02:00
2025-01-27 18:00:14 +01:00
public void setImageTintList(ColorStateList tint) {
if (tint == null)
colorFilter = null;
else
colorFilter = new PorterDuffColorFilter(tint.getDefaultColor(), PorterDuff.Mode.SRC_IN);
2025-01-27 18:00:14 +01:00
setImageDrawable(drawable);
}
public void setImageAlpha(int alpha) {}
public void setMaxWidth(int width) {}
public void setMaxHeight(int height) {}
public void setImageState(int[] state, boolean merge) {}
2024-05-27 19:02:31 +02:00
public ColorStateList getImageTintList() {
return null;
}
public PorterDuff.Mode getImageTintMode() {
return null;
}
public void setImageTintMode(PorterDuff.Mode tintMode) {}
public void setCropToPadding(boolean crop) {}
public void setColorFilter(int color) {}
public void setColorFilter(ColorFilter cf) {}
public Matrix getImageMatrix() {
return Matrix.IDENTITY_MATRIX;
}
@Override
protected native long native_constructor(Context context, AttributeSet attrs);
protected native void native_setDrawable(long widget, long paintable);
protected native void native_setScaleType(long widget, int scale_type);
}