android如果給imageview做圓角,如果在原有的bitmap上加上一些修飾的drawable

來源:互聯網
上載者:User

標籤:圓角   bitmap   

先上:



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;}

把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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.