Bitmap.createBitmap学习笔记
来源:互联网 发布:梅西c罗数据对比最新 编辑:程序博客网 时间:2024/05/22 02:16
在编写MusicPlayer的过程中,播放歌曲时搜索歌手的图片,并动态地将图片设为当前Activity的背景。当图片大小与屏幕不适应时会导致图片被拉神效果很不好看。比如你的手机分辨率是320X480,而图片的分辨率是320X320,那么,该图片将被拉长。
那么,如何放大图片的同时不影响其显示效果呢?
网上有用 drawable.setTileModeXY(TileMode.REPEAT , TileMode.REPEAT ) 的方法来设置图片的平铺,但是我没有试验成功,
下面是Google官方文档对createBitmap方法的解释 public static
source The bitmap we are subsettingx The x coordinate of the first pixel in sourcey The y coordinate of the first pixel in sourcewidth The number of pixels in each rowheight The number of rowsm Optional matrix to be applied to the pixelsfilter true if the source should be filtered. Only applies if the matrix contains more than just translation.IllegalArgumentException if the x, y, width, height values are outside of the dimensions of the source bitmap. surce:用来剪裁的图片源; x:剪裁x方向的起始位置; y:剪裁y方向的起始位置; width:剪裁的宽度; height:剪裁的高度; filer:不是很清楚它的作用 - -!
我遇到的问题是图片为320X320,手机屏幕是320X480,需将图片按照手机屏幕的比例剪裁后再适当放大。这里的执行过程并非是先放大再剪裁。 一、获取手机屏幕宽高(width,height)与图片的宽高(widthDrawable,heightDrawable); 二、计算其比例,取大的。比如width/widthDrawable=1.0,height/heightDrawable=1.5,那么取scale=1.5; 三、利用 Matrix构建缩放矩阵: Matrix matrix = new Matrix(); matrix.postScale(scale, scale); 四、计算起始剪裁的位置x,y和剪裁宽度width,高度height; 五、调用createBitmap方法,在原图片的基础上剪裁新的图片并缩放相应的比例。
于是尝试了Bitmap.createBitmap方法。
public static Bitmap createBitmap (Bitmap source, int x, int y, intwidth, int height, Matrix m, boolean filter)
Added in API level 1
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
Returns
- A bitmap that represents the specified subset of source
Throws
各参数含义:
需要注意的是:必须满足条件:x+width<=bitmap.width()(图片源的原始宽度)否则会抛出IllegalArgumentException异常。
下面是思路:
下面是相关的代码:
@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);//320X480int widthDrawable = backGroundMap.getWidth(); int heightDrawable = backGroundMap.getHeight();//获取背景图片的宽和高Log.v("franco-widthDrawable", "widthDrawable = " + widthDrawable);Log.v("franco-heightDrawable", "heightDrawable = " + heightDrawable);//320X320float scaleWidth = (float)width/widthDrawable; float scaleHeight = (float)height/heightDrawable;//宽高比Log.v("franco", "scaleWidth = " + scaleWidth);Log.v("franco", "scaleHeight = " + scaleHeight);//1.0:1.5Bitmap resizeBmp;Matrix matrix = new Matrix();if(scaleWidth <</span> scaleHeight) {float scale = scaleHeight;//取大的matrix.postScale(scale, scale);//缩放比例int xStart = (int)(widthDrawable-widthDrawable/scale)/2;Log.v("franco-xStart", "xStart = " + xStart);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 转化为 DrawableBitmapDrawable drawable = new BitmapDrawable(getResources(), resizeBmp); //drawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);lrcBackGround.setBackgroundDrawable(drawable);lrcBackGround.getBackground().setAlpha(150);//将背景该图片透明度设低,避免歌词显示效果不好,数值越大透明度越高}
最后效果如下:
但是这样做必将会剪裁掉部分原图,如果不剪裁的话图片的比例会失真,期待更好地方案。
0 0
- Bitmap.createBitmap学习笔记
- Bitmap.createBitmap
- Bitmap.createBitmap函数用法
- Bitmap createBitmap参数(一)
- Bitmap createBitmap参数(二)
- Bitmap createBitmap参数(三)
- bitmap的createBitmap
- Android--图片裁剪-Bitmap.createBitmap
- 2.27学习内容 dialog不让其关闭 bitmap.createBitmap时的错误,drawable表
- Bitmap.createBitmap 得到 的bitmap 不一定是新的bitmap
- 使用Bitmap.createBitmap遇到的问题
- Bitmap.createBitmap那个最长参数的方法
- Bitmap.createBitmap的6个重载方法
- 使用Bitmap createBitmap遇到的问题
- Bitmap.createBitmap几个参数的理解、
- Bitmap.createBitmap那个最长参数的方法
- Bitmap.createBitmap函数6个重载方法
- Bitmap 相关2:Bitmap.createBitmap() 使用(转)
- android switch button 报 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean
- Detecting Insults in Social Commentary 数据分析报告(python)
- 移动开发利器-Bmob后端云使用体验
- nyoj 541 最强DE 战斗力(数论)
- 通知的综合应用
- Bitmap.createBitmap学习笔记
- 查找旋转数组的最小值
- 机器学习(Machine Learning)&深度学习(Deep Learning)资料
- shell脚本中echo显示内容带颜色
- MathType中批量修改公式的格式
- 高精度总结(高精度类和重载运算高精度阶乘)
- [Asp.Net MVC4]验证用户登录实现
- javascript:void(0)与return false的区别
- 给你5个数a,b,c,d,k。x属于[a,b]y属于[c,d]。 问你有多少对(x,y)的公约数为k