android實現(xiàn)圓角矩形背景的方法
本文實例講述了android實現(xiàn)圓角矩形背景的方法。分享給大家供大家參考。具體如下:
1. java代碼如下:
import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.graphics.drawable.shapes.RoundRectShape; import android.view.MotionEvent; public class RoundRectDradable extends Drawable{ private static final float DEFAULT_RADIUS = 6.f; private Paint mPaint = new Paint(); private RoundRectShape mShape; private float[] mOuter; private int mColor; private int mPressColor; private float mTopLeftRadius = DEFAULT_RADIUS; private float mTopRightRadius = DEFAULT_RADIUS; private float mBottomLeftRadius = DEFAULT_RADIUS; private float mBottomRightRadius = DEFAULT_RADIUS; public RoundRectDradable() { mColor = Color.WHITE; mPressColor = Color.WHITE; mPaint.setColor(mColor); mPaint.setAntiAlias(true); } public float getTopLeftRadius() { return mTopLeftRadius; } public void setTopLeftRadius(float topLeftRadius) { this.mTopLeftRadius = topLeftRadius; } public float getTopRightRadius() { return mTopRightRadius; } public void setTopRightRadius(float topRightRadius) { this.mTopRightRadius = topRightRadius; } public float getBottomLeftRadius() { return mBottomLeftRadius; } public void setBottomLeftRadius(float bottomLeftRadius) { this.mBottomLeftRadius = bottomLeftRadius; } public float getBottomRightRadius() { return mBottomRightRadius; } public void setBottomRightRadius(float bottomRightRadius) { this.mBottomRightRadius = bottomRightRadius; } public int getPressColor() { return mPressColor; } public void setPressColor(int pressColor) { this.mPressColor = pressColor; } @Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); refreshShape(); mShape.resize(bounds.right - bounds.left, bounds.bottom - bounds.top); } private void refreshShape(){ mOuter = new float[]{mTopLeftRadius, mTopLeftRadius , mTopRightRadius, mTopRightRadius , mBottomLeftRadius, mBottomLeftRadius , mBottomRightRadius, mBottomLeftRadius}; mShape = new RoundRectShape(mOuter, null, null); } public void setColor(int color){ mColor = color; mPaint.setColor(color); } @Override public void draw(Canvas canvas) { mShape.draw(canvas, mPaint); } @Override public void setAlpha(int alpha) { mPaint.setAlpha(alpha); } @Override public void setColorFilter(ColorFilter cf) { mPaint.setColorFilter(cf); } @Override public int getOpacity() { return mPaint.getAlpha(); } }
2. java代碼如下:
import android.graphics.Rect; import android.graphics.drawable.StateListDrawable; public class StateRoundRectDrawable extends StateListDrawable{ private static final float DEFAULT_RADIUS = 6.f; private float mTopLeftRadius = DEFAULT_RADIUS; private float mTopRightRadius = DEFAULT_RADIUS; private float mBottomLeftRadius = DEFAULT_RADIUS; private float mBottomRightRadius = DEFAULT_RADIUS; private int mNormalColor; private int mPressedColor; private RoundRectDradable mNormalDradable; private RoundRectDradable mPressedDradable; public StateRoundRectDrawable(int normalCorlor, int pressColor) { this.mNormalColor = normalCorlor; this.mPressedColor = pressColor; } @Override protected void onBoundsChange(Rect bounds) { if(mNormalDradable == null){ mNormalDradable = new RoundRectDradable(); mNormalDradable.setTopLeftRadius(mTopLeftRadius); mNormalDradable.setTopRightRadius(mTopRightRadius); mNormalDradable.setBottomLeftRadius(mBottomLeftRadius); mNormalDradable.setBottomRightRadius(mBottomRightRadius); mNormalDradable.setColor(mNormalColor); mNormalDradable.onBoundsChange(bounds); } if(mPressedDradable == null){ mPressedDradable = new RoundRectDradable(); mPressedDradable.setTopLeftRadius(mTopLeftRadius); mPressedDradable.setTopRightRadius(mTopRightRadius); mPressedDradable.setBottomLeftRadius(mBottomLeftRadius); mPressedDradable.setBottomRightRadius(mBottomRightRadius); mPressedDradable.setColor(mPressedColor); mPressedDradable.onBoundsChange(bounds); } this.addState(new int[]{-android.R.attr.state_pressed}, mNormalDradable); this.addState(new int[]{android.R.attr.state_pressed}, mPressedDradable); } public float getTopLeftRadius() { return mTopLeftRadius; } public void setTopLeftRadius(float topLeftRadius) { this.mTopLeftRadius = topLeftRadius; } public float getTopRightRadius() { return mTopRightRadius; } public void setTopRightRadius(float topRightRadius) { this.mTopRightRadius = topRightRadius; } public float getBottomLeftRadius() { return mBottomLeftRadius; } public void setBottomLeftRadius(float bottomLeftRadius) { this.mBottomLeftRadius = bottomLeftRadius; } public float getBottomRightRadius() { return mBottomRightRadius; } public void setBottomRightRadius(float bottomRightRadius) { this.mBottomRightRadius = bottomRightRadius; } public int getNormalColor() { return mNormalColor; } public void setNormalColor(int normalColor) { this.mNormalColor = normalColor; } public int getPressedColor() { return mPressedColor; } public void setPressedColor(int pressedColor) { this.mPressedColor = pressedColor; } }
希望本文所述對大家的Android程序設(shè)計有所幫助。
- Android實現(xiàn)圓角矩形和圓形ImageView的方式
- Android自定義ViewGroup實現(xiàn)帶箭頭的圓角矩形菜單
- Android編程之canvas繪制各種圖形(點,直線,弧,圓,橢圓,文字,矩形,多邊形,曲線,圓角矩形)
- Android實現(xiàn)空心圓角矩形按鈕的實例代碼
- Android開發(fā)使用自定義View將圓角矩形繪制在Canvas上的方法
- Android編程實現(xiàn)帶漸變效果的圓角矩形示例
- Android開發(fā)基于Drawable實現(xiàn)圓角矩形的方法
- Android開發(fā)之圓角矩形創(chuàng)建工具RoundRect類定義與用法分析
- Android實現(xiàn)自定義ImageView的圓角矩形圖片效果
相關(guān)文章
Android的ListView多選刪除操作實現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android的ListView多選刪除操作實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05Android webview手動校驗https證書(by 星空武哥)
有些時候由于Android系統(tǒng)的bug或者其他的原因,導(dǎo)致我們的webview不能驗證通過我們的https證書,最明顯的例子就是華為手機(jī)mate7升級到Android7.0后,手機(jī)有些網(wǎng)站打不開了,而更新了webview的補(bǔ)丁后就沒問題了2017-09-09詳解Android TableLayout中stretchColumns、shrinkColumns的用法
這篇文章主要介紹了Android TableLayout中stretchColumns、shrinkColumns用法的相關(guān)資料,需要的朋友可以參考下2017-03-03Android中獲取sha1證書指紋數(shù)據(jù)的方法
大家都知道在Android開發(fā)中,經(jīng)常要獲取sha1證書指紋,所以這篇文章主要介紹在Android中如何使用命令獲取sha1證書指紋數(shù)據(jù)的方法,有需要的可以參考借鑒。2016-09-09Android開發(fā)實現(xiàn)ImageView加載攝像頭拍攝的大圖功能
這篇文章主要介紹了Android開發(fā)實現(xiàn)ImageView加載攝像頭拍攝的大圖功能,涉及Android基于ImageView的攝像頭拍攝圖片加載、保存及權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11android開發(fā)之方形圓角listview代碼分享
我寫這篇文章受到了kiritor的專欄發(fā)表的博文Android UI控件之ListView實現(xiàn)圓角效果的啟發(fā)。2013-06-06Kotlin創(chuàng)建一個好用的協(xié)程作用域
這篇文章主要介紹了Kotlin創(chuàng)建一個好用的協(xié)程作用域,kotlin中使用協(xié)程,是一定要跟協(xié)程作用域一起配合使用的,否則可能協(xié)程的生命周期無法被準(zhǔn)確控制,造成內(nèi)存泄漏或其他問題2022-07-07Android 中RecycleView實現(xiàn)item的點擊事件
這篇文章主要介紹了Android 中RecycleView實現(xiàn)item的點擊事件的相關(guān)資料,需要的朋友可以參考下2017-03-03android自動生成dimens適配文件的圖文教程詳解(無需Java工具類)
這篇文章主要介紹了android自動生成dimens適配文件,無需Java工具類,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03