如下图所示,把拿到的招商银行的图标做成右侧半透明的不规则形状的背景
由于这张图的白色背景部分是一个圆角矩形带阴影的背景图片,所以要注意放上去的图标不要遮住圆角和阴影,需要一个圆角矩形的ImageView的控件,圆角的弧度和白色背景的圆角弧度一致。圆角矩形ImageView,弧度可以自己调节
public class XCRoundRectImageView extends ImageView {
private Paint paint;
public XCRoundRectImageView(Context context) {
this(context,null);
}
public XCRoundRectImageView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public XCRoundRectImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint = new Paint();
}
/**
* 绘制圆角矩形图片
* @author caizhiming
*/
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (null != drawable) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
Bitmap b = getRoundBitmap(bitmap, 25);
final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight());
final Rect rectDest = new Rect(0,0,getWidth(),getHeight());
paint.reset();
canvas.drawBitmap(b, rectSrc, rectDest, paint);
} else {
super.onDraw(canvas);
}
}
/**
* 获取圆角矩形图片方法
* @param bitmap
* @param roundPx,一般设置成14
* @return Bitmap
* @author caizhiming
*/
private Bitmap getRoundBitmap(Bitmap bitmap, int roundPx) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
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);
int x = bitmap.getWidth();
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
使用,背景图标需要一定的透明度,设置 android:alpha=“0.06”
<com.basestonedata.instalment.ui.scanpay.wiget.XCRoundRectImageView
android:id="@+id/iv_bg_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:scaleType="centerInside"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:alpha="0.06" />
把资源文件中的图片转化为bitmap
Resources res = MainActivity.this.getResources();
Bitmap bmp= BitmapFactory.decodeResource(res, R.mipmap.flower);
把Url图片链接转化为bitmap
//ImageLoader
Bitmap bitmap = ImageLoader.getInstance().loadImageSync((url),options);
//Glide框架
Glide.with(getActivity()).load(mUserEntity.getData().getCover()).asBitmap().into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
bitmap = resource;
}
});
public class BitmapCropUtils {
//放大图片 新的宽高dp,放大缩小
public static Bitmap zoomImg(Context context, Bitmap bm, int newWidth , int newHeight){
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) dip2px(context,newWidth)) / width;
float scaleHeight = ((float) dip2px(context,newHeight)) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
Bitmap returnBm=imageCrop(context,newbm);
return returnBm;
}
//裁剪图片
public static Bitmap imageCrop(Context context, Bitmap bitmap) {
// 得到图片的宽,高
int w = bitmap.getWidth();
int h = bitmap.getHeight();
//width最大不能超过长方形的短边
if (w < width || h < width) {
width = w > h ? h : w;
}
//可以从图片的中心裁剪
// int retX = (w - width) / 2;
// int retY = (h - width) / 2;
//public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
// surce:用来剪裁的图片源;
// x:剪裁x方向的起始位置;
//y:剪裁y方向的起始位置;
//width:剪裁的宽度;
//height:剪裁的高度;
//filer:不是很清楚它的作用 - -!
//需要注意的是:必须满足条件:x+width<=bitmap.width()(图片源的原始宽度)否则会抛出IllegalArgumentException异常。
return Bitmap.createBitmap(bitmap, 0, 0,dip2px(context,145), dip2px(context,125), null, false);
}
//把图片裁剪成圆形(补充)
public static Bitmap getCircleBitmap(Bitmap bitmap) {
if (bitmap == null) {
return null;
}
bitmap = cropBitmap(bitmap);//裁剪成正方形
try {
Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(circleBitmap);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());
final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight()));
float roundPx = 0.0f;
roundPx = bitmap.getWidth();
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.WHITE);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
final Rect src = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());
canvas.drawBitmap(bitmap, src, rect, paint);
return circleBitmap;
} catch (Exception e) {
return bitmap;
}
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
}