Reputation: 5551
I've created a custom ImageView class, as I wanted to override the onDraw method for my Canvas; however, the XML attributes scaleType="centerInside"
and adjustViewBounds="true"
are not being inherited by my custom ImageView class. If I switch to the default ImageView, everything works fine - but how can I have it work in my custom Image View?
In the class below, I'm simply retrieving a bitmap from my application class and drawing it to the canvas; however, when using my CustomImageView - the image is massive and not scaled properly. Any idea how to fix this? Thanks!
Custom ImageView XML:
<com.project.app.controls.CustomImageView
android:id="@+id/myImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerInside"/>
Custom ImageView Class:
public class CustomImageView extends ImageView {
ProjectApp wApp;
Paint paint;
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
wApp = ProjectApp.createInstance();
paint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawBitmap(wApp.photoTaken, 0, 0, null);
}
}
Upvotes: 4
Views: 2674
Reputation: 17140
I think you want to use fitXY instead so the image will be bound by the constraints of the CustomImageView's layout params (and its parent in this case).
android:scaleType="fitXY"
Upvotes: 1
Reputation: 22064
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
wApp = ProjectApp.createInstance();
paint = new Paint();
setAdjustViewBounds(true);
setScaleType(ImageView.ScaleType.CENTER_INSIDE);
}
Upvotes: 2