Android Bitmap 使用
来源:互联网 发布:暴雪台服战网客户端mac 编辑:程序博客网 时间:2024/04/30 00:23
1、Drawable → Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);Canvas canvas = new Canvas(bitmap);// canvas.setBitmap(bitmap);drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());drawable.draw(canvas);return bitmap;}
2、从资源中获取Bitmap
Resources res=getResources();Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
3、Bitmap → byte[]
private byte[] Bitmap2Bytes(Bitmap bm) {ByteArrayOutputStream baos = new ByteArrayOutputStream();bm.compress(Bitmap.CompressFormat.PNG, 100, baos);return baos.toByteArray();}
4、byte[] → Bitmap
private Bitmap Bytes2Bimap(byte[] b) {if (b.length != 0) {return BitmapFactory.decodeByteArray(b, 0, b.length);} else {return null;}}
5、保存bitmap
public static boolean saveBitmap2file(Bitmap bmp, String filename) {CompressFormat format = Bitmap.CompressFormat.JPEG;int quality = 100;OutputStream stream = null;try {stream = new FileOutputStream("/sdcard/" + filename);} catch (FileNotFoundException e) {e.printStackTrace();}return bmp.compress(format, quality, stream);}
6、将图片按自己的要求缩放
public Bitmap scaleBitmap() {// 图片源Bitmap bm = BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.img));// 获得图片的宽高int width = bm.getWidth();int height = bm.getHeight();// 设置想要的大小int newWidth = 320;int newHeight = 480;// 计算缩放比例float scaleWidth = ((float) newWidth) / width;float scaleHeight = ((float) newHeight) / height;// 取得想要缩放的matrix参数Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);// 得到新的图片return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);}
7、以最省内存的方式读取本地资源的图片
public static Bitmap readBitmap(Context context, int rid) {BitmapFactory.Options options = new BitmapFactory.Options();options.inPreferredConfig = Bitmap.Config.RGB_565;options.inPurgeable = true;options.inInputShareable = true;// 获取资源图片InputStream is = context.getResources().openRawResource(rid);return BitmapFactory.decodeStream(is, null, options);}
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Config.ARGB_8888);Canvas canvas = new Canvas(output);final int color = 0xff424242;final Paint paint = new Paint();final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());final RectF rectF = new RectF(rect);paint.setAntiAlias(true);canvas.drawARGB(0, 0, 0, 0);paint.setColor(color);canvas.drawRoundRect(rectF, roundPx, roundPx, paint);paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));canvas.drawBitmap(bitmap, rect, rect, paint);return output;}9、获得倒影图片
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {final int reflectionGap = 4;int width = bitmap.getWidth();int height = bitmap.getHeight();Matrix matrix = new Matrix();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 = new Canvas(bitmapWithReflection);canvas.drawBitmap(bitmap, 0, 0, null);Paint deafalutPaint = new Paint();canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);Paint paint = new Paint();LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,0x00ffffff, TileMode.CLAMP);paint.setShader(shader);// Set the Transfer mode to be porter duff and destination inpaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));// Draw a rectangle using the paint with our linear gradientcanvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);return bitmapWithReflection;}
- Android Bitmap 使用
- Android Bitmap的使用
- Android Bitmap使用
- android Bitmap使用技巧
- Android Bitmap的使用
- android中bitmap使用
- Android Bitmap使用心得
- Android Bitmap使用
- android bitmap 怎么使用缓存?
- Android Bitmap 蒙版使用
- android bitmap 内存使用分析:
- Android:使用Canvas合并Bitmap
- android开发之bitmap使用
- Android bitmap实际使用总结
- android 使用Bitmap压缩图片
- Android之bitmap的使用
- 【android 优化】ImageView的Bitmap的使用
- 在android中使用图像资源 bitmap
- ADF Region性能调优之 Activation 属性
- 关于2-sat判定求解中“同一组内必须选择”问题的思考
- JScript版的的CollectionBase类
- 有两个双向循环链表A,B,知道其头指针为:pHeadA,pHeadB,请写一函数将两链表中data值相同的结点删除
- 背景图片的居中显示
- Android Bitmap 使用
- WinCE下自定义的大软键盘
- ios消息推送机制原理与实现
- Openfire和Spark本地开发环境搭建记要
- 2012年08月29日--如何在JSP页面中提高响应速度
- 持久框架对象
- extern与static用法
- 装饰模式
- c++中类的大小问题