简体   繁体   中英

Scale bitmap on surfaceview for smaller high density screens

I have a list of over 300 bitmaps that the user is able to place on a surfaceview screen. I have resources for densities XHDPI,HDPI,MDPI and LDPI for each of these images. The problem is when a user has a smaller screen that is high density they are using the larger resources (XHDPI) which are taking up much of their screen.

The images display perfect on a 7" tablet screen (Galaxy Tab 2 7.0) but take a device with a 4" screen and the images are too big.

How can I scale the bitmaps based off of the users screen size so that each user can have the same experience?

I believe I have it figured out and it was actually fairly simple. Basically I take the width() and height() of the original bitmap and convert them to dp and then scale the bitmap using the new dp values for height and width. The result is a bitmap that is the same size across all screens. I am still testing this but so far it has worked on each emulator I've loaded up.

        public BitmapDrawable getScaledBitmapDrawable(int indexID) {
        Bitmap bitmapOrg  = BitmapFactory.decodeResource(mResources,
                             myNewImageList.get(indexID));            
        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        int nwidth = (int) convertPixelsToDp(width,c);
        int nheight = (int) convertPixelsToDp(height,c);

        Bitmap bitmapResized = Bitmap.createScaledBitmap(bitmapOrg, nwidth, 
                                nheight, false);
        return BitmapDrawable bitmapDrawable = new BitmapDrawable(mResources,
                                                    bitmapResized); 
        }
           //conversion method
          public static float convertPixelsToDp(float px, Context context){
          Resources resources = context.getResources();
          DisplayMetrics metrics = resources.getDisplayMetrics();
          float dp = px / (metrics.densityDpi / 160f);
          return dp;
         }

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