Android自定義帶圓角的ImageView
最近有一個(gè)實(shí)現(xiàn)一個(gè)帶有圓角的ImageView的需求,在網(wǎng)上找了找三方,雖然Demo都是正確的,但是移植過(guò)來(lái)就不可以了,因?yàn)檎?qǐng)求鏈接的時(shí)候用的是xUtils中Bitmap來(lái)進(jìn)行解析的,這樣就總是會(huì)報(bào)類型轉(zhuǎn)換異常的錯(cuò)誤。
就這樣只能自己定義一個(gè)了.
Demo:
package com.yizooo.yizooo.ui;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
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;
import com.lidroid.xutils.bitmap.core.AsyncDrawable;
/**
* Created by 雪寶寶 on 2016/3/27.
* 自定義圓角工具
*/
public class RoundImageView extends ImageView {
private Paint paint;
public RoundImageView(Context context) {
this(context,null);
}
public RoundImageView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint = new Paint();
}
/**
* 繪制圓角矩形圖片
*/
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
Bitmap bitmap = null;
if (null != drawable && drawable instanceof BitmapDrawable ) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
bitmap = bitmapDrawable.getBitmap();
//Bitmap bitmap =( (BitmapDrawable)drawable).getBitmap();
Bitmap b = getRoundBitmap(bitmap, 10);
final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight());
final Rect rectDest = new Rect(0,0,getWidth(),getHeight());
paint.reset();
canvas.drawBitmap(b, rectSrc, rectDest, paint);
}//防止出現(xiàn)類型轉(zhuǎn)換異常
else if(this.getDrawable() instanceof AsyncDrawable){
bitmap = Bitmap
.createBitmap(
getWidth(),
getHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas1 = new Canvas(bitmap);
// canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, getWidth(),
getHeight());
drawable.draw(canvas1);
}
else {
super.onDraw(canvas);
}
}
/**
* 獲取圓角矩形圖片方法
* @param bitmap
* @param roundPx,一般設(shè)置成14
* @return Bitmap
* @author caizhiming
*/
private Bitmap getRoundBitmap(Bitmap bitmap, int roundPx) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
int x = bitmap.getWidth();
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<com.yizooo.yizooo.ui.RoundImageView
android:id="@+id/item_frag_news_icon"
android:layout_width="@dimen/dp_47"
android:layout_height="@dimen/dp_50"
android:scaleType="fitXY"
android:src="@mipmap/fuwutongzhi"
android:layout_margin="@dimen/dp_10"
/>
</RelativeLayout>
最終的效果圖就不發(fā)照片了,各位朋友嘗試一下就可以看出效果圖了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Studio配置反混淆的實(shí)現(xiàn)
這篇文章主要介紹了Android Studio如何混淆的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
android 控件同時(shí)監(jiān)聽單擊和雙擊實(shí)例
這篇文章主要介紹了android 控件同時(shí)監(jiān)聽單擊和雙擊實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Android TextView實(shí)現(xiàn)垂直滾動(dòng)效果的方法
這篇文章主要介紹了Android TextView實(shí)現(xiàn)垂直滾動(dòng)效果的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了Android TextView控件垂直滾動(dòng)效果的相關(guān)屬性功能與設(shè)置技巧,需要的朋友可以參考下2016-10-10
Android popupwindow簡(jiǎn)單使用方法介紹
這篇文章主要為大家詳細(xì)介紹了Android popupwindow簡(jiǎn)單使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android中ImageView無(wú)法居中的問(wèn)題解決方法
做UI布局,尤其是遇到比較復(fù)雜的多重LinearLayout嵌套,常常會(huì)被一些比較小的問(wèn)題困擾上半天,比如今天在使用ImageView的時(shí)候,想讓其居中顯示,可是無(wú)論怎樣設(shè)置layout_gravity屬性,都無(wú)法達(dá)到效果2013-06-06
VS Code開發(fā)React-Native及Flutter 開啟無(wú)線局域網(wǎng)安卓真機(jī)調(diào)試問(wèn)題
這篇文章主要介紹了VS Code開發(fā)React-Native,F(xiàn)lutter 開啟無(wú)線局域網(wǎng)安卓真機(jī)調(diào)試,需要的朋友可以參考下2020-04-04
詳解Android ContentProvider的基本原理和使用
ContentProvider(內(nèi)容提供者)是 Android 的四大組件之一,管理 Android 以結(jié)構(gòu)化方式存放的數(shù)據(jù),以相對(duì)安全的方式封裝數(shù)據(jù)(表)并且提供簡(jiǎn)易的處理機(jī)制和統(tǒng)一的訪問(wèn)接口供其他程序調(diào)用2021-06-06

