
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;
}
}
本文介绍了一种使用Android平台实现图片圆角效果的方法。通过自定义Activity并利用Bitmap、Canvas等API,可以将任意图片转化为带有圆角的图像,并应用于ImageView中。文章提供了完整的代码示例,包括创建圆角图片的具体步骤。
804

被折叠的 条评论
为什么被折叠?



