Android(安卓)Bitmap用法总结
16lz
2021-01-24
Bitmap用法总结
1、Drawable → Bitmap
- 1、Drawable→Bitmap
- publicstaticBitmapdrawableToBitmap(Drawabledrawable){
- Bitmapbitmap=Bitmap
- .createBitmap(
- drawable.getIntrinsicWidth(),
- drawable.getIntrinsicHeight(),
- drawable.getOpacity()!=PixelFormat.OPAQUE?Bitmap.Config.ARGB_8888
- :Bitmap.Config.RGB_565);
- Canvascanvas=newCanvas(bitmap);
- //canvas.setBitmap(bitmap);
- drawable.setBounds(0,0,drawable.getIntrinsicWidth(),
- drawable.getIntrinsicHeight());
- drawable.draw(canvas);
- returnbitmap;
- }
Java代码
- <span style="font-size: medium;">Resources res=getResources();
- Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);</span>
3、Bitmap → byte[]
- privatebyte[] Bitmap2Bytes(Bitmap bm){
- ByteArrayOutputStream baos =newByteArrayOutputStream();
- bm.compress(Bitmap.CompressFormat.PNG,100, baos);
- returnbaos.toByteArray();</span>
Java代码
- privateBitmap Bytes2Bimap(byte[] b){
- if(b.length!=0){
- returnBitmapFactory.decodeByteArray(b,0, b.length);
- }
- else{
- returnnull;
- }
- }</span>
5、保存bitmap
- staticbooleansaveBitmap2file(Bitmap bmp,String filename){
- CompressFormat format= Bitmap.CompressFormat.JPEG;
- intquality =100;
- OutputStream stream =null;
- try{
- stream =newFileOutputStream("/sdcard/"+ filename);
- }catch(FileNotFoundException e) {
- // TODO Auto-generated catch block
- Generated by Foxit PDF Creator © Foxit Software
- http://www.foxitsoftware.com For evaluation only.
- e.printStackTrace();
- }
- returnbmp.compress(format, quality, stream);
- }</span>
6、将图片按自己的要求缩放
- // 图片源
- Bitmap bm = BitmapFactory.decodeStream(getResources()
- .openRawResource(R.drawable.dog));
- // 获得图片的宽高
- intwidth = bm.getWidth();
- intheight = bm.getHeight();
- // 设置想要的大小
- intnewWidth =320;
- intnewHeight =480;
- // 计算缩放比例
- floatscaleWidth = ((float) newWidth) / width;
- floatscaleHeight = ((float) newHeight) / height;
- // 取得想要缩放的matrix参数
- Matrix matrix =newMatrix();
- matrix.postScale(scaleWidth, scaleHeight);
- // 得到新的图片
- Bitmap newbm = Bitmap.createBitmap(bm,0,0, width, height, matrix,
- true);
- // 放在画布上
- canvas.drawBitmap(newbm,0,0, paint);</span>
7、bitmap的用法小结
Java代码
- BitmapFactory.Options option =newBitmapFactory.Options();
- option.inSampleSize =2;//将图片设为原来宽高的1/2,防止内存溢出
- Bitmap bm = BitmapFactory.decodeFile("",option);//文件流
- URL url =newURL("");
- InputStream is = url.openStream();
- Bitmap bm = BitmapFactory.decodeStream(is);
- android:scaleType:
- android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType /
- android:scaleType值的意义区别:
- CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分
- 显示
- CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长
- (宽)
- CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片
- 长/宽等于或小于View的长/宽
- Generated by Foxit PDF Creator © Foxit Software
- http://www.foxitsoftware.com For evaluation only.
- FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
- FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
- FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
- FIT_XY / fitXY 把图片 不按比例 扩大/缩小到View的大小显示
- MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。</span>
8. 放大缩小图片
Java代码
- publicstaticBitmap zoomBitmap(Bitmap bitmap,intw,inth){
- intwidth = bitmap.getWidth();
- intheight = bitmap.getHeight();
- Matrix matrix =newMatrix();
- floatscaleWidht = ((float)w / width);
- floatscaleHeight = ((float)h / height);
- matrix.postScale(scaleWidht, scaleHeight);
- Bitmap newbmp = Bitmap.createBitmap(bitmap,0,0, width, height, matrix,
- true);
- returnnewbmp;
- }</span>
Java代码
- publicstaticBitmap drawableToBitmap(Drawable drawable){
- intwidth = drawable.getIntrinsicWidth();
- intheight = drawable.getIntrinsicHeight();
- Bitmap bitmap = Bitmap.createBitmap(width, height,
- drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
- : Bitmap.Config.RGB_565);
- Canvas canvas =newCanvas(bitmap);
- drawable.setBounds(0,0,width,height);
- drawable.draw(canvas);
- returnbitmap;
- Generated by Foxit PDF Creator © Foxit Software
- http://www.foxitsoftware.com For evaluation only.
- }</span>
10. 获得圆角图片的方法
- publicstaticBitmap getRoundedCornerBitmap(Bitmap bitmap,floatroundPx){
- Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
- .getHeight(), Config.ARGB_8888);
- Canvas canvas =newCanvas(output);
- finalintcolor =0xff424242;
- finalPaint paint =newPaint();
- finalRect rect =newRect(0,0, bitmap.getWidth(), bitmap.getHeight());
- finalRectF rectF =newRectF(rect);
- paint.setAntiAlias(true);
- canvas.drawARGB(0,0,0,0);
- paint.setColor(color);
- canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
- paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
- canvas.drawBitmap(bitmap, rect, rect, paint);
- returnoutput;
- }</span>
Java代码
- publicstaticBitmap createReflectionImageWithOrigin(Bitmap bitmap){
- finalintreflectionGap =4;
- intwidth = bitmap.getWidth();
- intheight = bitmap.getHeight();
- Matrix matrix =newMatrix();
- matrix.preScale(1, -1);
- Bitmap reflectionImage = Bitmap.createBitmap(bitmap,
- 0, height/2, width, height/2, matrix,false);
- Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2),
- Config.ARGB_8888);
- Canvas canvas =newCanvas(bitmapWithReflection);
- canvas.drawBitmap(bitmap,0,0,null);
- Paint deafalutPaint =newPaint();
- Generated by Foxit PDF Creator © Foxit Software
- http://www.foxitsoftware.com For evaluation only.
- canvas.drawRect(0, height,width,height + reflectionGap,
- deafalutPaint);
- canvas.drawBitmap(reflectionImage,0, height + reflectionGap,null);
- Paint paint =newPaint();
- LinearGradient shader =newLinearGradient(0,
- bitmap.getHeight(),0, bitmapWithReflection.getHeight()
- + reflectionGap,0x70ffffff,0x00ffffff, TileMode.CLAMP);
- paint.setShader(shader);
- // Set the Transfer mode to be porter duff and destination in
- paint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));
- // Draw a rectangle using the paint with our linear gradient
- canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
- + reflectionGap, paint);
- returnbitmapWithReflection;
- }
- }<
更多相关文章
- Android(安卓)webview上传图片(相册/相机)
- Android(安卓)主动 try-catch 主线程的 Exception
- Android(安卓)活动的最佳实践
- android context调用startActivity时报错:Calling startActivity(
- Android(安卓)JNI和NDK学习(2)--编程入门
- android 系统定制的小技巧
- Android判断是Wifi还是4G网络代码
- Android(安卓)Bitmap保存为.bmp格式,图像转化为黑白图片