调用:    categoryIV = (TextView) this.findViewById(R.id.categoryIV_text);
        StateRoundRectDrawable mRoundRectDradable = new              StateRoundRectDrawable(getResources().getColor(R.color.white), getResources().getColor(R.color.blue));
        mRoundRectDradable.setBottomLeftRadius(15);
        mRoundRectDradable.setTopRightRadius(15);
        categoryIV.setBackgroundDrawable(mRoundRectDradable);




类: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;

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

         , mBottomRightRadius, mBottomLeftRadius
                , mBottomLeftRadius, 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: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;
    }
    
}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android 手机厂商推送服务调研
  2. Android——文件存储之外部存储(含源码下
  3. 谈Android中对ListView,RecycleView应用的
  4. Android 进阶/面试 重难点
  5. Android文本样式——上
  6. 文件搜索引擎FileSearch
  7. Android 中的线程
  8. Android绘图机制(一) View类
  9. 阿里Android开发规范:文件与数据库
  10. 在android中通过java层程序调用命令行的