android圆角图片的制作

先将Drawable的图片格式转化成bitmap,然后在转化成圆角图片

// 将Drawable转化为Bitmap
	public static Bitmap drawableToBitmap(Drawable drawable) {
		int width = drawable.getIntrinsicWidth();
		int height = drawable.getIntrinsicHeight();
		Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
				: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, width, height);
		drawable.draw(canvas);
		return bitmap;

	}

	// 获得圆角图片的方法
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {

		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);

		return output;
	}
// 放大缩小图片
<span style="white-space:pre">	</span>public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
<span style="white-space:pre">		</span>int width = bitmap.getWidth();
<span style="white-space:pre">		</span>int height = bitmap.getHeight();
<span style="white-space:pre">		</span>Matrix matrix = new Matrix();
<span style="white-space:pre">		</span>float scaleWidht = ((float) w / width);
<span style="white-space:pre">		</span>float scaleHeight = ((float) h / height);
<span style="white-space:pre">		</span>matrix.postScale(scaleWidht, scaleHeight);
<span style="white-space:pre">		</span>Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
<span style="white-space:pre">		</span>return newbmp;
<span style="white-space:pre">	</span>}

然后就可以直接调用了

	Bitmap bitmap = ImageUtil.drawableToBitmap(drawable);
		// 缩放图片
		Bitmap zoomBitmap = ImageUtil.zoomBitmap(bitmap, 480, 300);
		// 获取圆角图片
		Bitmap roundBitmap = ImageUtil.getRoundedCornerBitmap(zoomBitmap, 50.0f);
mImageView01.setImageBitmap(roundBitmap);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值