Chinaunix首页 | 论坛 | 博客
  • 博客访问: 38758
  • 博文数量: 3
  • 博客积分: 310
  • 博客等级: 一等列兵
  • 技术积分: 85
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-09 13:02
文章分类

全部博文(3)

文章存档

2011年(3)

我的朋友

分类: 嵌入式

2011-10-16 09:26:07

  1. 1./**
  2. 2. * 将图片设置为圆角
  3. 3. */
  4. 4.public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
  5. 5. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
  6. 6. bitmap.getHeight(), Config.ARGB_8888);
  7. 7. Canvas canvas = new Canvas(output);
  8. 8. final int color = 0xff424242;
  9. 9. final Paint paint = new Paint();
  10. 10. final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
  11. 11. final RectF rectF = new RectF(rect);
  12. 12. final float roundPx = pixels;
  13. 13. paint.setAntiAlias(true);
  14. 14. canvas.drawARGB(0, 0, 0, 0);
  15. 15. paint.setColor(color);
  16. 16. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
  17. 17. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  18. 18. canvas.drawBitmap(bitmap, rect, rect, paint);
  19. 19. return output;
  20. 20.}

当我们需要圆角的时候,调用这个方法,第一个参数是传入需要转化成圆角的图片,第二个参数是圆角的度数,数值越大,圆角越大

下面举个例子,有一个LinearLayout,想把它的背景图片设置成圆角

布局文件main.xml

  1. 1.<?xml version="1.0" encoding="utf-8"?>
  2. 2.<LinearLayout
  3. 3. xmlns:android=""
  4. 4. android:id="@+id/layout" android:orientation="vertical"
  5. 5. android:layout_width="wrap_content"
  6. 6. android:layout_height="wrap_content">
  7. 7.</LinearLayout>
  1. 1.mInflater = LayoutInflater.from(this);
  2. 2.
  3. 3.Drawable drawable = getResources().getDrawable(R.drawable.icon);
  4. 4.BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
  5. 5.Bitmap bitmap = bitmapDrawable.getBitmap();
  6. 6.
  7. 7.View view = mInflater.inflate(
  8. 8. R.layout.share_xiaohua_revert_lv_item_top, null);
  9. 9.ImageView mImageView = (ImageView) view
  10. 10. .findViewById(R.id.lv_xiaohua_image);
  11. 11.mImageView.setImageBitmap(toRoundCorner(bitmap, 30));
阅读(3912) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:android线性布局参数详解

给主人留下些什么吧!~~