Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Adjust Image view width dynamically according to the screen size in android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 5.72k
    Comment on it

    It is very difficult to re-size the image-view width according to the screen without losing the quality of the image or you can say increase the width in proportionate to height of the image view.

    I will show you the trick how you can do that, you just have to set this class from the xml.

    public class ResizableImageView extends ImageView {
    public ResizableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
         Drawable d = getDrawable();
                 // ceil not round - avoid thin vertical gaps along the left/right edges
                 int width = MeasureSpec.getSize(widthMeasureSpec);
                 int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
                 setMeasuredDimension(width, height);
                 super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    like this


    Enjoy:- android:scaleType="fitXY" />

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: