Android学习笔记之详细讲解画圆角图片
openkk
13年前
package xiaosi.RoundConcer; 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.widget.ImageView; public class RoundConcerActivity extends Activity { /** Called when the activity is first created. */ private ImageView roundImage = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); roundImage = (ImageView)findViewById(R.id.roundconcer); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a); roundImage.setImageBitmap(getRoundCornerImage(bitmap, 50)); } public static Bitmap getRoundCornerImage(Bitmap bitmap, int roundPixels) { //创建一个和原始图片一样大小位图 Bitmap roundConcerImage = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); //创建带有位图roundConcerImage的画布 Canvas canvas = new Canvas(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, roundPixels, roundPixels, paint); //设置相交模式 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); //把图片画到矩形去 canvas.drawBitmap(bitmap, null, rect, paint); return roundConcerImage; } }