简体   繁体   中英

Android studio how can i image size all screen

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM