1. 程式人生 > 其它 >直播原始碼搭建教程,如何通過圖片獲取指定大小的bitmap

直播原始碼搭建教程,如何通過圖片獲取指定大小的bitmap

直播原始碼搭建教程,如何通過圖片獲取指定大小的bitmap
一般通過圖片獲取其Bitmap的方法如下

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.head);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
System.out.println("Bitmap weight = "+width+" Bitmap height "+height);
Bitmap bitmap1 = Bitmap.createBitmap(bitmap, width/3
, height/4, bitmap.getWidth() /3, bitmap.getHeight() /2); imageView.setImageBitmap(bitmap1);

但是以上方法只是’裁剪’,如果只是想要固定大小的而不裁剪呢?

/**
 * Creates a new bitmap, scaled from an existing bitmap, when possible. If the
 * specified width and height are the same as the current width and height of
 * the source bitmap, the source bitmap is returned and no new bitmap is
 * created.
 *
 * @param src       The source bitmap.
 * @param dstWidth  The new bitmap's desired width.
 * @param dstHeight The new bitmap's desired height.
 * @param filter    Whether or not bilinear filtering should be used when scaling the
 *                  bitmap. If this is true then bilinear filtering will be used when
 *                  scaling which has better image quality at the cost of worse performance.
 *                  If this is false then nearest-neighbor scaling is used instead which
 *                  will have worse image quality but is faster. Recommended default
 *                  is to set filter to 'true' as the cost of bilinear filtering is
 *                  typically minimal and the improved image quality is significant.
 * @return The new scaled bitmap or the source bitmap if no scaling is required.
 * @throws IllegalArgumentException if width is <= 0, or height is <= 0
 
*/ public static Bitmap createScaledBitmap(@NonNull Bitmap src, int dstWidth, int dstHeight, boolean filter) { Matrix m = new Matrix(); final int width = src.getWidth(); final int height = src.getHeight(); if (width != dstWidth || height != dstHeight) { final float sx = dstWidth / (float
) width; final float sy = dstHeight / (float) height; m.setScale(sx, sy); } return Bitmap.createBitmap(src, 0, 0, width, height, m, filter); }

以上就是直播原始碼搭建教程,如何通過圖片獲取指定大小的bitmap, 更多內容歡迎關注之後的文章