首页 > 代码库 > android如果给imageview做圆角,如果在原有的bitmap上加上一些修饰的drawable
android如果给imageview做圆角,如果在原有的bitmap上加上一些修饰的drawable
先上效果图:
Layout文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#99CC99" tools:context=".MainActivity" > <ImageView android:id="@+id/img1" android:layout_centerInParent="true" android:layout_width="50dp" android:layout_height="50dp" android:adjustViewBounds="true" android:scaleType="centerInside" android:src=http://www.mamicode.com/"@drawable/portrait">首先介绍两种把资源里的drawable转成bitmap的方式
第一种:
Bitmap bmp=BitmapFactory.decodeResource(this.getResources(), R.drawable.portrait);第二种:Drawable d = this.getResources().getDrawable(R.drawable.portrait); Bitmap bmp = drawableToBitmap(d); public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888 ); Canvas canvas = new Canvas(bitmap); //canvas.setBitmap(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; }
把Bitmap加上圆角的方法:private static RectF rrbRectf = new RectF(); private static Path rrbRath = new Path(); private static final int RRB_DEFAULT_SIZE = 10; public static Bitmap outputBmp(Context ctx, Bitmap src, boolean isCover) { Bitmap v = null; if (isCover) { v = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.v); }else{ v = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.tv); } Bitmap bmp = null; int arcLength = RRB_DEFAULT_SIZE; if (src != null && arcLength > 0) { int width = src.getWidth(); int height = src.getHeight(); // Utils.loge("width:" + width + "height:" + height); Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); Canvas canvas = new Canvas(newBitmap); rrbRath.reset(); rrbRectf.set(0, 0, width, height); <strong>rrbRath.addRoundRect(rrbRectf, arcLength, arcLength, Path.Direction.CW); //这里是加上圆角</strong> canvas.clipPath(rrbRath); canvas.drawBitmap(src, 0, 0, new Paint(Paint.ANTI_ALIAS_FLAG)); if (newBitmap != null && v != null) { int width1 = newBitmap.getWidth(); int height1 = newBitmap.getHeight(); int width2 = v.getWidth(); int height2 = v.getHeight(); bmp = Bitmap.createBitmap(width1 + (width2 / 2), height1 + (height2 / 2), Bitmap.Config.ARGB_4444); bmp.eraseColor(Color.TRANSPARENT); Canvas canvas2 = new Canvas(bmp); canvas2.drawBitmap(newBitmap, 0, 0, new Paint( Paint.ANTI_ALIAS_FLAG)); <strong>canvas2.drawBitmap(v, width1 - (width2 / 2), height1 - (height2 / 2), new Paint(Paint.ANTI_ALIAS_FLAG)); //这里是在图片右下角加上v</strong> newBitmap.recycle(); v.recycle(); return bmp; } src.recycle(); } return bmp; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。