首页 > 代码库 > Android学习笔记之详细讲解画圆角图片
Android学习笔记之详细讲解画圆角图片
[java] view plain copy
- 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学习笔记之详细讲解画圆角图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。