Android:图片转圆角(saveLayer、restore)

来源:互联网 发布:java编程思想 doc 编辑:程序博客网 时间:2024/05/08 18:41
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {  
  2.         int x = bitmap.getWidth();  
  3.         int y = bitmap.getHeight();  
  4.         float[] mOuter = new float[] { roundPx, roundPx, roundPx, roundPx,  
  5.                 roundPx, roundPx, roundPx, roundPx };  
  6.         // 根据源文件新建一个darwable对象  
  7.         Drawable imageDrawable = new BitmapDrawable(bitmap);  
  8.   
  9.         // 新建一个新的输出图片  
  10.         Bitmap output = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);  
  11.         Canvas canvas = new Canvas(output);  
  12.   
  13.         // 新建一个矩形  
  14.         RectF outerRect = new RectF(00, x, y);  
  15.         Path mPath = new Path();  
  16.         // 创建一个圆角矩形路径  
  17.         mPath.addRoundRect(outerRect, mOuter, Path.Direction.CW);  
  18.   
  19.         // 画出一个红色的圆角矩形  
  20.         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  21.         paint.setColor(Color.RED);  
  22.         paint.setAntiAlias(true);  
  23.         canvas.drawPath(mPath, paint);  
  24.         // 设置绘图两层交汇时的模式  
  25.         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));  
  26.         // 设置绘图区域(在那个区域内绘图)  
  27.         imageDrawable.setBounds(00, x, y);  
  28.         // 将用于绘制图片的图层拷贝到单独图层并压入图层栈  
  29.         canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);  
  30.         // 将源图片绘制到这个拷贝的图片图层上  
  31.         imageDrawable.draw(canvas);  
  32.         // 将图片图层退出栈并将图像绘制到矩形的那一层  
  33.         canvas.restore();  
  34.         return output;  
  35.     }  



[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static Bitmap getCircleBitmap(Bitmap bitmap, float roundPx) {  
  2.         Drawable imageDrawable = new BitmapDrawable(bitmap);  
  3.         // 新建一个新的输出图片  
  4.         Bitmap output = Bitmap.createBitmap((int)roundPx, (int)roundPx, Bitmap.Config.ARGB_8888);  
  5.         Canvas canvas = new Canvas(output);  
  6.         Path mPath = new Path();  
  7.         mPath.addCircle(roundPx/2, roundPx/2, roundPx/2, Path.Direction.CW);  
  8.   
  9.         // 画出一个红色的圆角矩形  
  10.         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  11.         paint.setColor(Color.RED);  
  12.         paint.setAntiAlias(true);  
  13.         canvas.drawPath(mPath, paint);  
  14.         // 设置绘图两层交汇时的模式  
  15.         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));  
  16.         // 设置绘图区域(在那个区域内绘图)  
  17.         imageDrawable.setBounds(00, (int)roundPx, (int)roundPx);  
  18.         // 将用于绘制图片的图层压入图层栈  
  19.         canvas.saveLayer(00, (int)roundPx, (int)roundPx, paint, Canvas.ALL_SAVE_FLAG);  
  20.         // 将源图片绘制到这个圆角矩形上  
  21.         imageDrawable.draw(canvas);  
  22.         // 将图片图层退出栈并将图像绘制到矩形的那一层  
  23.         canvas.restore();  
  24.         return output;  
  25.     }  


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static Bitmap getRoundCornerImage(Bitmap bitmap, int roundPixels) {  
  2.         // 创建一个和原始图片一样大小位图  
  3.         Bitmap roundConcerImage = Bitmap.createBitmap(bitmap.getWidth(),  
  4.                 bitmap.getHeight(), Config.ARGB_8888);  
  5.         // 创建带有位图roundConcerImage的画布  
  6.         Canvas canvas = new Canvas(roundConcerImage);  
  7.         // 创建画笔  
  8.         Paint paint = new Paint();  
  9.         // 创建一个和原始图片一样大小的矩形  
  10.         Rect rect = new Rect(00, bitmap.getWidth(), bitmap.getHeight());  
  11.         RectF rectF = new RectF(rect);  
  12.         // 去锯齿  
  13.         paint.setAntiAlias(true);  
  14.         // 画一个和原始图片一样大小的圆角矩形  
  15.         canvas.drawRoundRect(rectF, roundPixels, roundPixels, paint);  
  16.         // 设置相交模式  
  17.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  18.         // 把图片画到矩形去  
  19.         canvas.drawBitmap(bitmap, null, rect, paint);  
  20.         return roundConcerImage;  
  21.     }  
0 0
原创粉丝点击