0

I have an image and ImageView of width 45dp & height 45dp. If I use my phone this image looks good but on another phone image seems very small. If you use picture converter and put xhdpi xxhdpi... the picture is still small. (I want to get the same experience in all screen size. Example, in pixel 2 width 45dp height 45dp looks very good, Nexus width 65dp height 65dp very good, Samsung tab3 100dp looks very good. How can I do this?

Sorry for my poor English.

cagdas
  • 19
  • 5

2 Answers2

0

In the Dimens package of your application under res folder, use separate dimen values like dimens-ldpi, dimens-hdpi, dimens-mdpi, dimens-xhdpi, dimens-xxhdpi, dimens-xxxhdpi. Create a value in each file and use different values for them.

Or, you can visit this question. There's multiple solution mentioned with example.

0

You need to maintain the aspect ratio of image view by calculating the ratio of screen width and height .

Create a Java File , say ProportionalImageView :

public class ProportionalImageView extends ImageView {

public ProportionalImageView(Context context) {
    super(context);
}

public ProportionalImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ProportionalImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Drawable d = getDrawable();
    if (d != null) {
        int w = MeasureSpec.getSize(widthMeasureSpec);
        int h = w * d.getIntrinsicHeight() / d.getIntrinsicWidth();
        setMeasuredDimension(w, h);
    }
    else super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

then use this image View in your xml file :

<com.example.ProportionalImageView
        android:layout_width="matchParent"
        android:layout_height="wrapContent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@mipmap/img" />

Here , replace com.example. with your package name Hope it helps

xaif
  • 553
  • 6
  • 27