Android学习笔记之详细讲解画圆角图片

Android学习笔记之详细讲解画圆角图片

01 package xiaosi.RoundConcer;
02  
03 import android.app.Activity;
04 import android.graphics.Bitmap;
05 import android.graphics.Bitmap.Config;
06 import android.graphics.BitmapFactory;
07 import android.graphics.Canvas;
08 import android.graphics.Paint;
09 import android.graphics.PorterDuff.Mode;
10 import android.graphics.PorterDuffXfermode;
11 import android.graphics.Rect;
12 import android.graphics.RectF;
13 import android.os.Bundle;
14 import android.widget.ImageView;
15  
16 public class RoundConcerActivity extends Activity
17 {
18     /** Called when the activity is first created. */
19     private ImageView roundImage = null;
20     @Override
21     public void onCreate(Bundle savedInstanceState)
22     {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.main);
25          
26         roundImage = (ImageView)findViewById(R.id.roundconcer);
27          
28          
29         Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);
30         roundImage.setImageBitmap(getRoundCornerImage(bitmap, 50));
31     }
32  
33     public static Bitmap getRoundCornerImage(Bitmap bitmap, int roundPixels)
34     {
35         //创建一个和原始图片一样大小位图
36         Bitmap roundConcerImage = Bitmap.createBitmap(bitmap.getWidth(),
37                 bitmap.getHeight(), Config.ARGB_8888);
38         //创建带有位图roundConcerImage的画布
39         Canvas canvas = new Canvas(roundConcerImage);
40         //创建画笔
41         Paint paint = new Paint();
42         //创建一个和原始图片一样大小的矩形
43         Rect rect = new Rect(00, bitmap.getWidth(), bitmap.getHeight());
44         RectF rectF = new RectF(rect);
45         // 去锯齿
46         paint.setAntiAlias(true);
47         //画一个和原始图片一样大小的圆角矩形
48         canvas.drawRoundRect(rectF, roundPixels, roundPixels, paint);
49         //设置相交模式
50         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
51         //把图片画到矩形去
52         canvas.drawBitmap(bitmap, null, rect, paint);
53         return roundConcerImage;
54     }
55 }
上一篇:解决方案应用实例 |数据中台全面赋能,三只松鼠多渠道、多业态数智化转变


下一篇:android 程序漰溃 后台handle处理类