关闭

代码 实现按钮 seletor shape 选择器等

标签: 代码实现选择器selectorshape懒人
448人阅读 评论(1) 收藏 举报
分类:

package com.fangwf.shapeviewcode.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.fangwf.shapeviewcode.widget.OvalDrawable;


public class ShapeBackgroundUtil {
    /**
     * 设置自定义颜色的drawable背景
     *
     * @param context
     * @param view
     * @param drawableId drawable-shape-背景
     * @param color      自定颜色
     */
    public static void setCustomColorShape(Context context, View view, int drawableId, String color) {
        setCustomColorShape(context, view, drawableId, Color.parseColor(color));
    }

    public static void setCustomColorShape(Context context, View view, int drawableId, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(ContextCompat.getDrawable(context, drawableId));
        } else {
            view.setBackgroundDrawable(ContextCompat.getDrawable(context, drawableId));
        }
        GradientDrawable myGrad = (GradientDrawable) view.getBackground();
        myGrad.setColor(color);
    }

    /**
     * 设置自定义颜色的全圆角长形按钮背景
     *
     * @param view
     * @param color
     */
    public static void setCustomColorOvalBackground(View view, String color) {
        setCustomColorOvalBackground(view, Color.parseColor(color));
    }

    public static void setCustomColorOvalBackground(View view, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(new OvalDrawable(color));
        } else {
            view.setBackgroundDrawable(new OvalDrawable(color));
        }
    }

    public static void setMultiColorOvalBackground(View view, String defaultColor, String activeColor) {
        setMultiColorOvalBackground(view, Color.parseColor(defaultColor), Color.parseColor(activeColor));
    }

    public static void setMultiColorOvalBackground(View view, int defaultColor, int activeColor) {
        StateListDrawable drawable = new StateListDrawable();
        Drawable drawable1 = new OvalDrawable(defaultColor);
        Drawable drawable2 = new OvalDrawable(activeColor);
        drawable.addState(new int[]{-android.R.attr.state_enabled}, drawable2);
        drawable.addState(new int[]{android.R.attr.state_selected}, drawable2);
        drawable.addState(new int[]{android.R.attr.state_active}, drawable2);
        drawable.addState(new int[]{android.R.attr.state_checked}, drawable2);
        drawable.addState(new int[]{android.R.attr.state_pressed}, drawable2);
        drawable.addState(new int[]{android.R.attr.state_focused}, drawable2);
        drawable.addState(new int[]{}, drawable1);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(drawable);
        } else {
            view.setBackgroundDrawable(drawable);
        }
    }

    public static void setMultiColorBackground(Context context, View view, int drawableId, String defaultColor, String activeColor) {
        setMultiColorBackground(context, view, drawableId, Color.parseColor(defaultColor), Color.parseColor(activeColor));
    }

    public static void setMultiColorBackground(Context context, View view, int drawableId, int defaultColor, int activeColor) {
        StateListDrawable drawable = new StateListDrawable();
        GradientDrawable drawable1 = (GradientDrawable) ContextCompat.getDrawable(context, drawableId);
        GradientDrawable drawable2 = (GradientDrawable) ContextCompat.getDrawable(context, drawableId);
        drawable1.setColor(defaultColor);
        drawable2.setColor(activeColor);
//        drawable.addState(new int[]{android.R.attr.state_enabled}, drawable2);
        drawable.addState(new int[]{android.R.attr.state_active}, drawable2);
        drawable.addState(new int[]{android.R.attr.state_checked}, drawable2);
        drawable.addState(new int[]{android.R.attr.state_pressed}, drawable2);
        drawable.addState(new int[]{android.R.attr.state_selected}, drawable2);
        drawable.addState(new int[]{android.R.attr.state_focused}, drawable2);
        drawable.addState(new int[]{}, drawable1);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(drawable);
        } else {
            view.setBackgroundDrawable(drawable);
        }
    }
}
package com.fangwf.shapeviewcode.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import com.fangwf.shapeviewcode.R;
import com.fangwf.shapeviewcode.util.ShapeBackgroundUtil;


public class MainActivity extends Activity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ShapeBackgroundUtil.setMultiColorBackground(this, findViewById(R.id.button), R.drawable.shape_radius, "#e26119", "#df3816");
        ShapeBackgroundUtil.setMultiColorOvalBackground(findViewById(R.id.button2), "#ea517d", "#df3816");
        ShapeBackgroundUtil.setCustomColorOvalBackground(findViewById(R.id.button3), "#0d3fb3");
        ShapeBackgroundUtil.setCustomColorShape(this,findViewById(R.id.button4),R.drawable.shape_radius,"#e26119");

        findViewById(R.id.button).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

    }
}

R.drawable.shape_radius
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"/>

    <solid
        android:color="#00000000"/>

</shape>  


package com.fangwf.shapeviewcode.widget;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;


public class OvalDrawable extends Drawable {
    public OvalDrawable() {
        this(0);
    }

    public OvalDrawable(int color) {
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
    }

    private boolean mRebuildShader = true;
    private final Paint mPaint = new Paint();
    private RectF leftRect, rightRect, cententRect;

    @Override
    public void draw(Canvas canvas) {
        if (mRebuildShader) {
            Rect bounds = getBounds();
            int r = bounds.bottom - bounds.top;
            leftRect = new RectF(bounds.left, bounds.top, bounds.left + r, bounds.top + r);
            rightRect = new RectF(bounds.right - r, bounds.top, bounds.right, bounds.top + r);
            cententRect = new RectF(bounds.left + r / 2, bounds.top, bounds.right - r / 2, bounds.bottom);
        }

        canvas.drawArc(leftRect, 90, 180, true, mPaint);
        canvas.drawRect(cententRect, mPaint);
        canvas.drawArc(rightRect, -90, 180, true, mPaint);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        mRebuildShader = true;
    }

    public void setColor(int color) {
        mPaint.setColor(color);
    }

    public int getColor() {
        return mPaint.getColor();
    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    public int getAlpha() {
        return mPaint.getAlpha();
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
    }

    @Override
    public int getOpacity() {
        return 0;
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5171次
    • 积分:152
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章分类