Android图片写成圆形ImageView

来源:互联网 发布:开淘宝店物流怎么弄 编辑:程序博客网 时间:2024/05/16 01:15
//定义圆形ImageView类,直接调用即可public class RoundImageView extends android.support.v7.widget.AppCompatImageView {    public RoundImageView(Context context) {        super(context);        // TODO Auto-generated constructor stub    }    public RoundImageView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public RoundImageView(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(), Bitmap.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(PorterDuff.Mode.SRC_IN));        canvas.drawBitmap(sbmp, rect, rect, paint);        return output;    }}
<LinearLayout    android:id="@+id/linearLayout_DaShenZhuYe_2"    android:layout_width="match_parent"    android:layout_height="72dp"    android:layout_marginLeft="15dp"    android:layout_marginRight="15dp"    android:background="#FFFFFF"    android:orientation="horizontal">    <TextView        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:gravity="center_vertical"        android:text="粉丝贡献榜"        android:textSize="15dp" />    <RelativeLayout        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:gravity="center_vertical">        <com.example.qd.mydemo.RoundImageView            android:id="@+id/right_login_head1"            style="@style/image_dashen_photo"            android:layout_alignParentTop="true"            android:layout_toLeftOf="@+id/right_login_head2"            android:layout_toStartOf="@+id/right_login_head2"            android:src="@drawable/back" />        <com.example.qd.mydemo.RoundImageView            android:id="@+id/right_login_head2"            style="@style/image_dashen_photo"            android:layout_alignParentTop="true"            android:layout_toLeftOf="@+id/right_login_head3"            android:layout_toStartOf="@+id/right_login_head3"            android:src="@drawable/back" />        <com.example.qd.mydemo.RoundImageView            android:id="@+id/right_login_head3"            style="@style/image_dashen_photo"            android:layout_marginRight="5dp"            android:layout_toLeftOf="@+id/imageView_DaShenZhuYe"            android:layout_toStartOf="@+id/imageView_DaShenZhuYe"            android:src="@drawable/back" />        <TextView            android:id="@+id/imageView_DaShenZhuYe"            android:layout_width="30dp"            android:layout_height="30dp"            android:layout_alignParentRight="true"            android:layout_marginRight="5dp"            android:gravity="center"            android:text="更多"            android:textColor="#000000"            android:textSize="10dp" />    </RelativeLayout></LinearLayout>
 
原创粉丝点击