- 1./**
- 2. * 将图片设置为圆角
- 3. */
- 4.public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
- 5. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
- 6. bitmap.getHeight(), Config.ARGB_8888);
- 7. Canvas canvas = new Canvas(output);
- 8. final int color = 0xff424242;
- 9. final Paint paint = new Paint();
- 10. final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
- 11. final RectF rectF = new RectF(rect);
- 12. final float roundPx = pixels;
- 13. paint.setAntiAlias(true);
- 14. canvas.drawARGB(0, 0, 0, 0);
- 15. paint.setColor(color);
- 16. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
- 17. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
- 18. canvas.drawBitmap(bitmap, rect, rect, paint);
- 19. return output;
- 20.}
当我们需要圆角的时候,调用这个方法,第一个参数是传入需要转化成圆角的图片,第二个参数是圆角的度数,数值越大,圆角越大
下面举个例子,有一个LinearLayout,想把它的背景图片设置成圆角
布局文件main.xml
- 1.<?xml version="1.0" encoding="utf-8"?>
- 2.<LinearLayout
- 3. xmlns:android=""
- 4. android:id="@+id/layout" android:orientation="vertical"
- 5. android:layout_width="wrap_content"
- 6. android:layout_height="wrap_content">
- 7.</LinearLayout>
- 1.mInflater = LayoutInflater.from(this);
- 2.
- 3.Drawable drawable = getResources().getDrawable(R.drawable.icon);
- 4.BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
- 5.Bitmap bitmap = bitmapDrawable.getBitmap();
- 6.
- 7.View view = mInflater.inflate(
- 8. R.layout.share_xiaohua_revert_lv_item_top, null);
- 9.ImageView mImageView = (ImageView) view
- 10. .findViewById(R.id.lv_xiaohua_image);
- 11.mImageView.setImageBitmap(toRoundCorner(bitmap, 30));
阅读(3958) | 评论(0) | 转发(0) |