自定義圓形圖片顯示控制元件CircleImageView
阿新 • • 發佈:2019-02-04
專案中我們經常會碰到需要使用圓形圖片的地方,以前我都是直接在程式碼裡面畫。這次碰到一個專案需要使用的地方太多了,所以就自己在網上查了一下資料,集合了一些大神寫的方法,然後搞了一套非常簡單圓形圖片顯示控制元件CircleImageVIew。不多說,先貼出這個類的程式碼
CircleImageView.java
package cn.nodemedia.cc.chenzhou.three.ui.view; /** * Created by zhangyabo on 2016/3/31. */ import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; public class CircleImageView extends ImageView { public CircleImageView(Context context) { super(context); // TODO Auto-generated constructor stub } public CircleImageView(Context context, AttributeSet attrs) { super(context, attrs); } public CircleImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (drawable == null) { return; } if (getWidth() == 0 || getHeight() == 0) { return; } Bitmap b = ((BitmapDrawable)drawable).getBitmap(); if(null == b) { return; } Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); int w = getWidth(), h = getHeight(); Bitmap roundBitmap = getCroppedBitmap(bitmap, w); canvas.drawBitmap(roundBitmap, 0,0, null); } public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) { Bitmap sbmp; if(bmp.getWidth() != radius || bmp.getHeight() != radius) sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false); else sbmp = bmp; Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xffa19774; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight()); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(Color.parseColor("#BAB399")); canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f, sbmp.getWidth() / 2+0.1f, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(sbmp, rect, rect, paint); return output; } }
對的,就是這麼簡單,也不用再去其他地方配置什麼值了。
下面貼出我在專案直接在佈局檔案中使用的程式碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:gravity="center_vertical" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginTop="8dp"> <TextView android:id="@+id/tv_first_name" android:layout_width="26dp" android:layout_height="wrap_content" android:layout_marginLeft="16dp" android:text="A" android:textColor="#000000" android:textSize="24sp" android:visibility="invisible" /> <span style="color:#33cc00;"><cn.nodemedia.cc.chenzhou.three.ui.view.CircleImageView android:id="@+id/imageview_contact_header" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginLeft="32dp" android:scaleType="fitXY" android:src="@drawable/test_round_image" /></span> <TextView android:id="@+id/tv_contact_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_weight="1" android:text="白巖鬆" android:textColor="#4D4D4D" android:textSize="16sp" /> <TextView android:id="@+id/tv_contact_job" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="42dp" android:text="職務" android:textColor="#4D4D4D" android:textSize="14sp" /> </LinearLayout> </LinearLayout>
然後顯示的結果是這樣的