MainActivity.java
package com.xzq.imageviewroundconcer; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; import android.os.Bundle; import android.view.Menu; import android.widget.ImageView; /** * @author XZQ * @see http://blog.csdn.net/cmdkmd?viewmode=contents */ public class MainActivity extends Activity { private ImageView roundImage;// 需要处理的图片 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(); processBiz(); } /** * 业务的处理 */ private void processBiz() { Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a); roundImage.setImageBitmap(getRoundCornerImage(bitmap)); } /** * 圆角的处理 * * @param bitmap */ private static Bitmap getRoundCornerImage(Bitmap bitmap) { Bitmap oldBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);// 创建一个原图 Canvas canvas = new Canvas(oldBitmap);// 创建带有位图roundConcerImage的画布 Paint paint = new Paint();// 创建画笔 Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());// 创建一个和原始图片一样大小的矩形 RectF rectf = new RectF(rect); paint.setAntiAlias(true);// 去锯齿 canvas.drawRoundRect(rectf, 50, 50, paint);// 画一个和原始图片一样大小的圆角矩形 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); // 设置相交模式 canvas.drawBitmap(bitmap, null, rect, paint); // 把图片画到矩形去 return oldBitmap; } /** * 页面空间初始化 */ private void findViewById() { roundImage = (ImageView) findViewById(R.id.img_concer); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }