Android scaling of images to screen density

前端 未结 3 1994
执念已碎
执念已碎 2020-12-05 08:18

I have an application with an embedded drawable 48x48 pixel at 71,12 pixels/inch I load the same image via a stream to a webserver, then load that stream

re         


        
相关标签:
3条回答
  • 2020-12-05 08:58

    Get the current density of the screen and set the drawable to that density

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int density  =  metrics.densityDpi
    myDrawable.setDensity(densityDpi);
    
    0 讨论(0)
  • 2020-12-05 09:00

    Do something like this:

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
    Bitmap bitmapOrg = new BitmapDrawable(getResources(), new  ByteArrayInputStream(imageThumbnail)).getBitmap();
    
    int width = bitmapOrg.getWidth();
    int height = bitmapOrg.getHeight();
    
    float scaleWidth = metrics.scaledDensity;
    float scaleHeight = metrics.scaledDensity;
    
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);
    

    Or

    Maybe try a differnet approach... try setting the height and width of images in the XML layout in dips, I am guessing you have the ImageView with wrap_content height and width at the moment, try setting the height and width to 48dip

    0 讨论(0)
  • 2020-12-05 09:17

    you can trigger android bitmapfactory to scale bitmap automatically, codes for this:

    BitmapFactory.Options options = new BitmapFactory.Options();
    DisplayMetrics metrics = context.getApplicationContext().getResources().getDisplayMetrics();
    options.inScreenDensity = metrics.densityDpi;
    options.inTargetDensity =  metrics.densityDpi;
    options.inDensity = DisplayMetrics.DENSITY_DEFAULT;
    
    Bitmap bm = BitmapFactory.decodeStream(in, null, options);
    in.close();
    BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bm);
    
    0 讨论(0)
提交回复
热议问题