Android converts the local resource picture into Drawable and performs the size setting operation

  • 2021-12-05 07:17:22
  • OfStack

Foreword:

Because the project shows pictures with Picasso, when setting placeholder and error pictures, I found that the size of local pictures could not meet my needs, so I need to change the size of pictures before displaying them.

The parameters of placeholder and error of Picasso are only int, resId and Drawable, drawable

So I intend to change the size of Drawable into the display, hey, the effect is very satisfactory!

Thoughts of the whole process:

Change the local picture (R. drawable. image) into an Drawable object

Convert an Drawable object to an Bitmap object

Creates a new Bitmap object from the Bitmap object according to the specified size

Convert an Bitmap object to an Drawable object

Code:

1. Turn the local picture (R. drawable. image) into an Drawable object

Drawable drawable = ContextCompat.getDrawable(context, R.drawable.image);

2. Convert an Drawable object to an Bitmap object


/**
 *  Will Drawable Convert to Bitmap
 * @param drawable
 * @return
 */
private Bitmap drawableToBitmap(Drawable drawable) {
  // Take drawable Width and height of 
  int width = drawable.getIntrinsicWidth();
  int height = drawable.getIntrinsicHeight();
  // Take drawable Color format of 
  Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE 
        ? Bitmap.Config.ARGB_8888
        : Bitmap.Config.RGB_565;
  // Create the corresponding bitmap
  Bitmap bitmap = Bitmap.createBitmap(width, height, config);
  // Create the corresponding bitmap Canvas of 
  Canvas canvas = new Canvas(bitmap);
  drawable.setBounds(0, 0, width, height);
  // Put drawable Content to canvas 
  drawable.draw(canvas);
  return bitmap;
}

3. Execution of the entire process

Later, I found a simpler method


/**
 *  Size the local resource picture 
 * @param resId
 * @param w
 * @param h
 * @return
 */
 public Drawable zoomImage(int resId, int w, int h){
  Resources res = mContext.getResources();
  Bitmap oldBmp = BitmapFactory.decodeResource(res, resId);
  Bitmap newBmp = Bitmap.createScaledBitmap(oldBmp,w, h, true);
  Drawable drawable = new BitmapDrawable(res, newBmp);
  return drawable;
}

The original complicated thinking


/**
 *  Zoom Drawable
 *@drawable  Original Drawable
 *@w  Specified width 
 *@h  Specified height 
 */
public Drawable zoomDrawable(Drawable drawable, int w, int h){
  // Get the original Drawable Width and height of 
  int width = drawable.getIntrinsicWidth();
  int height = drawable.getIntrinsicHeight();
  // Will Drawable Convert to Bitmap
  Bitmap oldbmp = drawableToBitmap(drawable);
  // Calculation scale
  Matrix matrix = new Matrix();
  float scaleWidth = ((float)w/width);
  float scaleHeight = ((float)h/height);
  matrix.postScale(scaleWidth, scaleHeight);
  // Generate a new Bitmap
  Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
  // Settings bitmap Turn into drawable The rear size remains unchanged  
  // This is very important to explain later! ! 
  DisplayMetrics metrics = new DisplayMetrics();
  manager.getDefaultDisplay().getMetrics(metrics);
  Resources resources = new Resources(mContext.getAssets(), metrics, null);
  return new BitmapDrawable(resources, newbmp);
}

Problems encountered in learning

Look at the online tutorial without the following


DisplayMetrics metrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
Resources resources = new Resources(mContext.getAssets(), metrics, null);

Of this code.

If the width and height I specified is 200, the width and height of the generated Drawable is only 100.

Originally, the size of Bitmap converted into Drawable will become smaller.

That code can solve the problem of smaller size.

If the passing God has a better way, I hope I can give directions to Xiaobai.

Additional knowledge: How to convert pictures in res into Bitmap in Android.

1. Just copy the code:

Resources res = MainActivity.this.getResources();

Bitmap bmp= BitmapFactory.decodeResource(res, R.mipmap.flower);


Related articles: