在编写MusicPlayer的过程中,播放歌曲时搜索歌手的图片,并动态地将图片设为当前Activity的背景。当图片大小与屏幕不适应时会导致图片被拉神效果很不好看。比如你的手机分辨率是320X480,而图片的分辨率是320X320,那么,该图片将被拉长。
下面是Google官方文档对createBitmap方法的解释
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix. The new bitmap may be the same object as source, or a copy may have been made. It is initialized with the same density as the original bitmap. If the source bitmap is immutable and the requested subset is the same as the source bitmap itself, then the source bitmap is returned and no new bitmap is created.
Parameters
source | The bitmap we are subsetting |
---|---|
x | The x coordinate of the first pixel in source |
y | The y coordinate of the first pixel in source |
width | The number of pixels in each row |
height | The number of rows |
m | Optional matrix to be applied to the pixels |
filter | true if the source should be filtered. Only applies if the matrix contains more than just translation. |
Returns
- A bitmap that represents the specified subset of source
Throws
IllegalArgumentException | if the x, y, width, height values are outside of the dimensions of the source bitmap. |
---|
各参数含义:
@SuppressWarnings("deprecation")
private void setDrawable(Bitmap backGroundMap, RelativeLayout lrcBackGround) {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;//获取屏幕的宽和高
Log.v("franco-width", "width = " + width);
Log.v("franco-height", "height = " + height);//320X480
int widthDrawable = backGroundMap.getWidth();
int heightDrawable = backGroundMap.getHeight();//获取背景图片的宽和高
Log.v("franco-widthDrawable", "widthDrawable = " + widthDrawable);
Log.v("franco-heightDrawable", "heightDrawable = " + heightDrawable);//320X320
float scaleWidth = (float)width/widthDrawable;
float scaleHeight = (float)height/heightDrawable;//宽高比
Log.v("franco", "scaleWidth = " + scaleWidth);
Log.v("franco", "scaleHeight = " + scaleHeight);//1.0:1.5
Bitmap resizeBmp;
Matrix matrix = new Matrix();
if(scaleWidth < scaleHeight) { float scale = scaleHeight;//取大的 matrix.postScale(scale, scale);//缩放比例 int xStart = (int)(widthDrawable-widthDrawable/scale)/2; Log.v("franco-xStart", "xStart = " + xStart); /* * Bitmap source:要从中截图的原始位图 * int x:起始x坐标 * int y:起始y坐标 * int width:要截的图的宽度 * int height:要截的图的宽度 * x+width must be <= bitmap.width()不然会报错 * 原理是先截图再缩放,而不是先缩放再截图!! */ resizeBmp = Bitmap.createBitmap(backGroundMap, xStart, 0, (int)(widthDrawable/scale), heightDrawable, matrix, true); } else { float scale = scaleWidth; matrix.postScale(scale, scale); int yStart = (int)(scaleHeight-scaleHeight/scale)/2; Log.v("franco-yStart", "yStart = " + yStart); resizeBmp = Bitmap.createBitmap(backGroundMap, 0, yStart, widthDrawable, (int)(heightDrawable/scale), matrix, true); } //Bitmap 转化为 Drawable BitmapDrawable drawable = new BitmapDrawable(getResources(), resizeBmp); //drawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT); lrcBackGround.setBackgroundDrawable(drawable); lrcBackGround.getBackground().setAlpha(150);//将背景该图片透明度设低,避免歌词显示效果不好,数值越大透明度越高 }