Android開發(fā)使用Drawable繪制圓角與圓形圖案功能示例
本文實(shí)例講述了Android開發(fā)使用Drawable繪制圓角與圓形圖案功能。分享給大家供大家參考,具體如下:
1. 創(chuàng)建類RoundCircleDrawable繼承Drawable
/** * 圓角矩形 * @Project App_View * @Package com.android.view.drawable * @author chenlin * @version 1.0 * @Date 2016年4月21日 * @Note TODO */ public class RoundCircleDrawable extends Drawable{ private Paint mPaint;//畫筆 private int mWidth;//圖片寬與長(zhǎng)度的最小值 private int mRadius;//半徑 private int mRound;//圓角 private RectF mRectF;//矩形 private Bitmap mBitmap;//圖片 private Type mType = Type.TYPE_ROUND;//默認(rèn)是矩形 //設(shè)置類型 enum Type{ TYPE_ROUND, TYPE_CICLE; } public RoundCircleDrawable(Bitmap bitmap){ this.mBitmap = bitmap; //初始化畫筆 mPaint = new Paint(); mPaint.setAntiAlias(true); BitmapShader shader = new BitmapShader(mBitmap, TileMode.CLAMP, TileMode.CLAMP); mPaint.setShader(shader); mWidth = Math.min(mBitmap.getWidth(), mBitmap.getHeight()); mRadius = mWidth / 2; } /** * 向外提供設(shè)置圖片類型的方法 * @param type */ public void setType(Type type){ this.mType = type; } /** * 暴露給外面設(shè)置圓角的大小 * * @param round */ public void setRound(int round) { this.mRound = round; } @Override public void setBounds(int left, int top, int right, int bottom) { super.setBounds(left, top, right, bottom); mRectF = new RectF(left, top, right, bottom); } @Override public void draw(Canvas canvas) { if (mType == Type.TYPE_ROUND) { canvas.drawRoundRect(mRectF, mRound, mRound, mPaint); }else { canvas.drawCircle(mWidth / 2, mWidth / 2, mRadius, mPaint); } } @Override public int getIntrinsicWidth() { if (mType == Type.TYPE_CICLE) { return mWidth; }else { return mBitmap.getWidth(); } } @Override public int getIntrinsicHeight() { if (mType == Type.TYPE_CICLE) { return mWidth; }else { return mBitmap.getHeight(); } } @Override public void setAlpha(int alpha) { mPaint.setAlpha(alpha); } @Override public void setColorFilter(ColorFilter cf) { mPaint.setColorFilter(cf); } @Override public int getOpacity() { return PixelFormat.TRANSLUCENT; } }
2. 實(shí)現(xiàn)方法
public class RoundActivity extends Activity { private ImageView mImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_round_drawable); mImageView = (ImageView) findViewById(R.id.iv_round); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aa); //RoundImageDrawable drawable = new RoundImageDrawable(bitmap); //drawable.setRound(30); RoundCircleDrawable drawable = new RoundCircleDrawable(bitmap); drawable.setRound(50); mImageView.setImageDrawable(drawable); } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)圓形圖片或者圓角圖片
- Android將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法
- Android自定義Drawable實(shí)現(xiàn)圓形和圓角
- Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼
- Android自定義控件之圓形、圓角ImageView
- Android實(shí)現(xiàn)圓角矩形和圓形ImageView的方式
- Android自定義view實(shí)現(xiàn)圓形、圓角和橢圓圖片(BitmapShader圖形渲染)
- Android自定義控件之圓形/圓角的實(shí)現(xiàn)代碼
- android 實(shí)現(xiàn)圓角圖片解決方案
- Android基于Fresco實(shí)現(xiàn)圓角和圓形圖片
相關(guān)文章
Android實(shí)現(xiàn)自動(dòng)匹配關(guān)鍵字并且標(biāo)紅功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自動(dòng)匹配關(guān)鍵字并且標(biāo)紅功能,單關(guān)鍵字和多關(guān)鍵字進(jìn)行匹配,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Kotlin?Dispatchers協(xié)程調(diào)度器源碼深入分析
Kotlin協(xié)程不是什么空中閣樓,Kotlin源代碼會(huì)被編譯成class字節(jié)碼文件,最終會(huì)運(yùn)行到虛擬機(jī)中。所以從本質(zhì)上講,Kotlin和Java是類似的,都是可以編譯產(chǎn)生class的語(yǔ)言,但最終還是會(huì)受到虛擬機(jī)的限制,它們的代碼最終會(huì)在虛擬機(jī)上的某個(gè)線程上被執(zhí)行2022-11-11Android中Activity常用功能設(shè)置小結(jié)(包括全屏、橫豎屏等)
這篇文章主要介紹了Android中Activity常用功能設(shè)置小結(jié)(包括全屏、橫豎屏等),以簡(jiǎn)單實(shí)例形式分析了Android實(shí)現(xiàn)全屏、豎屏及一直顯示等的技巧與注意事項(xiàng),需要的朋友可以參考下2015-10-10Android開發(fā)之使用150行代碼實(shí)現(xiàn)滑動(dòng)返回效果
本文給大家分享Android開發(fā)之使用150行代碼實(shí)現(xiàn)滑動(dòng)返回效果的代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-05-05Android開發(fā)基礎(chǔ)使用ProgressBar加載進(jìn)度條示例
這篇文章主要介紹了安卓開發(fā)基礎(chǔ)使用ProgressBar加載進(jìn)度條示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02