Reputation: 10929
I have a cardview with an ImageView aligned to bottom left.
I am getting the height of the ImageView and setting it as the height of the bitmap.
iv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int height = iv.getMeasuredHeight();
Paint widthPaint = new Paint();
widthPaint.setTextSize(200f);
float width = widthPaint.measureText(text);
Bitmap bmOverlay = Bitmap.createBitmap((int) width + S.dpToPx(ctx, 20), height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmOverlay);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
canvas.drawColor(Color.argb(220, 244, 244, 244));
paint.setColor(Color.TRANSPARENT);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
paint.setTextSize(200f);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(text, canvas.getWidth() / 2, (canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2), paint);
iv.setImageBitmap(bmOverlay);
}
});
The output of both iv.getMeasuredHeight();
and bmOverlay.getHeight()
is 120. But the bitmap is not covering the ImageView. I cannot understand why.
XML of the ImageView.
<ImageView
android:id="@+id/rquiz_iv_topic"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
Upvotes: 0
Views: 523
Reputation: 1136
What you want to archive: Set ImageView exactly size with the Bitmap is something wrong.
You needs to fits all the phone screen instead of settings it View by pixel.
You can:
1. Respect Parent Way:
Put ImageView
in LinearLayout
, Set Weight as 0.5
(means fits 50% of LinearLayout
), and set related (Vertical or Horizontal) Height or Width to 0dp
(to let Layout Manager
to handles the size).
And, set your image scale to fitXY
/ centerCrop
.(Or you can try other scaling)
2.Respect Bitmap Way:
Put ImageView
in RelativeLayout
, set other nearby Views to stand with ImageView
by layout_alignBottom
/layout_alignLeft
etc. And set ImageView
Height and/or Width to wrap_content
, let the Layout handles the size.
Upvotes: 1
Reputation: 13358
In your imageview set scaleType="centerInside" and adjustViewBounds="true".
<ImageView
android:id="@+id/rquiz_iv_topic"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
/>
Refer for more details ImageView.ScaleType
Upvotes: 0