先上效果图:
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="@drawable/portrait" /> <ImageView android:id="@+id/img5" android:layout_centerVertical="true" android:layout_toLeftOf="@id/img1" android:layout_marginRight="20dp" android:layout_width="50dp" android:layout_height="50dp" android:adjustViewBounds="true" android:scaleType="fitXY" android:src="@drawable/portrait" /> <ImageView android:id="@+id/img6" android:layout_centerVertical="true" android:layout_toLeftOf="@id/img5" android:layout_marginRight="20dp" android:layout_width="50dp" android:layout_height="50dp" android:adjustViewBounds="true" android:scaleType="center" android:src="@drawable/portrait" /> <ImageView android:id="@+id/img4" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/img1" android:layout_width="50dp" android:layout_height="50dp" android:adjustViewBounds="true" android:scaleType="centerCrop" android:src="@drawable/portrait" /> <ImageView android:id="@+id/img7" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@id/img4" android:layout_width="50dp" android:layout_height="50dp" android:adjustViewBounds="true" android:scaleType="fitCenter" android:src="@drawable/portrait" /> <ImageView android:id="@+id/img2" android:layout_marginBottom="20dp" android:layout_centerHorizontal="true" android:layout_above="@id/img1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/img3" android:layout_marginTop="20dp" android:layout_centerHorizontal="true" android:layout_below="@id/img1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
首先介绍两种把资源里的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; }
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; }
android如果给imageview做圆角,如果在原有的bitmap上加上一些修饰的drawable,布布扣,bubuko.com
android如果给imageview做圆角,如果在原有的bitmap上加上一些修饰的drawable
原文地址:http://blog.csdn.net/baidu_nod/article/details/38341395