简体   繁体   中英

Use xxhdpi on xhdpi device

I created my app to support screens from hdpi to xxxdpi and everything was working perfect. Images were shown just like they're supposed to. But, now I have a problem with Xiaomi Mi Max 3. It has 6,9" display with a resolution of only 1080x2160 which gives a ~350 dpi. That falls into xhdpi category (usually 720p screens) and now my images are smaller then they should be. Is there any way to force the use of drawable-xxhdpi folder with code?

just create another file like as:

drawable-xxxdpi/example.png

drawable-anydpi-v21/example.xml

and link your image file from anydpi xml code for supporting every screen size. see more information from official website or visit What Is The Difference Between -anydpi And -nodpi?

Ok, thanks to Md.Abdullah I found a solution. You cannot choose which folder of resources to use but you can use specific resource from a folder with getDrawableForDensity() method. This is what I did:

int width = Resources.getSystem().getDisplayMetrics().widthPixels;
int dpi = getResources().getDisplayMetrics().densityDpi;
int imageID = R.drawable.YOURIMAGE;
if (width > VALUE && dpi < VALUE) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        Drawable drawable = getResources().getDrawableForDensity(imageId, DisplayMetrics.DENSITY_XXHIGH);
//you will probably need to adjust width & height of your ImageView, but since the image
//will keep its aspect ratio, you only have to change width | heigth
        YOURIMAGEVIEW.setAdjustViewBounds(true);
        YOURIMAGEVIEW.setMaxWidth(VALUE);
        YOURIMAGEVIEW.setImageDrawable(drawable);
   }
}

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