android實(shí)現(xiàn)圓角矩形背景的方法
更新時(shí)間:2015年10月12日 12:10:38 作者:千里山南
這篇文章主要介紹了android實(shí)現(xiàn)圓角矩形背景的方法,以實(shí)例形式分析了Android編程實(shí)現(xiàn)圓角矩形背景的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了android實(shí)現(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;
}
}
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- Android實(shí)現(xiàn)圓角矩形和圓形ImageView的方式
- Android自定義ViewGroup實(shí)現(xiàn)帶箭頭的圓角矩形菜單
- Android編程之canvas繪制各種圖形(點(diǎn),直線,弧,圓,橢圓,文字,矩形,多邊形,曲線,圓角矩形)
- Android實(shí)現(xiàn)空心圓角矩形按鈕的實(shí)例代碼
- Android開(kāi)發(fā)使用自定義View將圓角矩形繪制在Canvas上的方法
- Android編程實(shí)現(xiàn)帶漸變效果的圓角矩形示例
- Android開(kāi)發(fā)基于Drawable實(shí)現(xiàn)圓角矩形的方法
- Android開(kāi)發(fā)之圓角矩形創(chuàng)建工具RoundRect類定義與用法分析
- Android實(shí)現(xiàn)自定義ImageView的圓角矩形圖片效果
相關(guān)文章
Android EditText限制輸入字符的方法總結(jié)
這篇文章主要介紹了 Android EditText限制輸入字符的方法總結(jié)的相關(guān)資料,這里提供了五種方法來(lái)實(shí)現(xiàn)并進(jìn)行比較,需要的朋友可以參考下2017-07-07
Android自定義ActionProvider ToolBar實(shí)現(xiàn)Menu小紅點(diǎn)
這篇文章主要介紹了Android自定義ActionProvider ToolBar實(shí)現(xiàn)Menu小紅點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android實(shí)現(xiàn)手機(jī)游戲隱藏虛擬按鍵
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手機(jī)游戲隱藏虛擬按鍵,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Kotlin實(shí)現(xiàn)圖片選擇器的關(guān)鍵技術(shù)點(diǎn)總結(jié)
這篇文章主要給大家介紹了關(guān)于Kotlin實(shí)現(xiàn)圖片選擇器的一些關(guān)鍵技術(shù)點(diǎn),這是一個(gè)我在學(xué)習(xí)Kotlin過(guò)程中的一個(gè)練手項(xiàng)目,非常適合學(xué)習(xí)Kotlin的時(shí)候參考,需要的朋友可以參考下2021-09-09
Ubuntu Android源碼以及內(nèi)核下載與編譯
本文主要介紹Android源碼的下載和編譯,這里整理了相關(guān)資料及如何下載和編譯的詳細(xì)步驟,有需要的小伙伴可以參考下2016-09-09

