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

原创 2016年05月31日 17:31:28

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;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Android Shape实现选择器样式

一,Button选择器 二,CheckBox选择器 三,列表item选择器 ...

用CSS伪选择器实现脚本控制的按钮功能

记得之前看到一句话“为了性能考虑,能用CSS实现的特效和功能,尽量不要使用JavaScript脚本”,因为JS的特效基本都是针对DOM节点的操作,只要DOM发生变化,就肯定会引起回流,回流是比较消耗资...

自绘按钮实现颜色选择器

作者:邓学彬 下载源代码 一.前言 很多时候,我们需要让用户在软件上选择颜色,那么一个“颜色选择器”就肯定需要了,本例程就是使用普通的按钮(Button)控件来实现颜色选择器。 首先来看一...

shape 选择器 和selector

先看一下运行的效果 其主要用法: 在res 首先新建一个color的文件夹,在其创建一个filebutton.xml的文件 在drawable下创建一个shape_button.xml的文件 ...

android 状态选择器和shape的运用

android 状态选择器和shape的运用 在Android开发中,会去用到状态选择器和Shape这个东东去实现一些特殊的功能,比如按钮按下状态,按钮的背景颜色渐变,图形变形化等。 一、状态...

Android 常用控件(TextView、EditText)及Shape外形资源、Selector选择器

基础控件,是应用的基本组件

Android Shape资源文件和Selector选择器使用

使用示例: //AlphaAnimation 动画   构造器参数:起始alpha值的动画      结束alpha值的动画 AlphaAnimation alphaAnimation = new ...

Android:背景选择器selector及shape详解

刚学Android的时候,所有的界面都很丑,后来学了背景选择器,就大大改善下这种情况……不过写起来挺烦的,因为在as或者eclipse中,有些内容没有提示,不能自动补全,这就让经常根据提示敲代码的我有...

开关按钮选择器switchbutton

  • 2016年05月30日 09:16
  • 5.8MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:代码 实现按钮 seletor shape 选择器等
举报原因:
原因补充:

(最多只允许输入30个字)