import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.view.Gravity;
import android.widget.Button;
/*
* @FileName:StyleButton.java
* @Version:V1.0
* @Date: 2014-5-7 Create
* @author: edsheng
*/
public class StyleButton extends Button
{
public static int[] mNormalState = new int[] {};
public static int[] mPressState = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };
public static int[] mDisableState = new int[] { -android.R.attr.state_enabled };
public static int[] mSelectedState = new int[] { android.R.attr.state_selected, android.R.attr.state_enabled };
private int mRadius = 0; //默认的圆角半径
//默认文字和背景颜色
private int mBgNormalColor = Color.RED;
private int mBgPressedColor = Color.GREEN;
private int mTextNormalColor = Color.WHITE;
private int mTextPressedColor = Color.GRAY;
public StyleButton(Context context)
{
super(context);
initUI();
}
private void initUI()
{
setGravity(Gravity.CENTER);
buildDraweableState();
buildColorDrawableState();
}
/**
* 构建图片drawble
*/
private void buildColorDrawableState()
{
ColorStateList colorStateList = new ColorStateList(new int[][] { mPressState, mNormalState },
new int[] { mTextPressedColor, mTextNormalColor });
setTextColor(colorStateList);
}
/**
* 构建背景Drawble
*/
private void buildDraweableState()
{
float outRectr[] = new float[] { mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius };
//创建状态管理器
StateListDrawable drawable = new StateListDrawable();
/**
* 注意StateListDrawable的构造方法我们这里使用的
* 是第一参数它是一个float的数组保存的是圆角的半径,它是按照top-left顺时针保存的八个值
*/
//创建圆弧形状
RoundRectShape rectShape = new RoundRectShape(outRectr, null, null);
//创建drawable
ShapeDrawable pressedDrawable = new ShapeDrawable(rectShape);
//设置我们按钮背景的颜色
pressedDrawable.getPaint().setColor(mBgPressedColor);
//添加到状态管理里面
drawable.addState(mPressState, pressedDrawable);
// ShapeDrawable disableDrawable = new ShapeDrawable(rectShape);
// disableDrawable.getPaint().setColor(prssedClor);
// disableDrawable.getPaint().setAlpha(125);
// drawable.addState(mDisableState, disableDrawable);
ShapeDrawable normalDrawable = new ShapeDrawable(rectShape);
normalDrawable.getPaint().setColor(mBgNormalColor);
drawable.addState(mNormalState, normalDrawable);
//设置我们的背景,就是xml里面的selector
setBackgroundDrawable(drawable);
}
/**
* 设置圆角矩形
*
* @param radius
*/
public void setRadius(int radius)
{
this.mRadius = radius;
buildDraweableState();
}
/**
* 设置按钮背景颜色
*
* @param normalColor
* @param prssedClor
*/
public void setBgNormalPressedcolor(int normalColor, int prssedClor)
{
mBgNormalColor = normalColor;
mBgPressedColor = prssedClor;
buildDraweableState();
}
/**
* 设置按钮文字颜色
*
* @param normalColor
* @param pressedColor
*/
public void setTextNormalPressedcolor(int normalColor, int pressedColor)
{
mTextPressedColor = pressedColor;
mTextNormalColor = normalColor;
buildColorDrawableState();
}
}requestWindowFeature(Window.FEATURE_NO_TITLE);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LayoutParams(200, 200);
layoutParams.bottomMargin = 20;
LayoutParams commomlayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
commomlayoutParams.bottomMargin = 20;
StyleButton button = new StyleButton(this);
button.setText("按钮");
linearLayout.addView(button, layoutParams);
StyleButton button2 = new StyleButton(this);
button2.setText("按钮");
button2.setRadius(16);
linearLayout.addView(button2, commomlayoutParams);
StyleButton button3 = new StyleButton(this);
button3.setText("按钮");
button3.setRadius(32);
button3.setTextNormalPressedcolor(Color.CYAN, Color.WHITE);
button3.setBgNormalPressedcolor(Color.BLUE, Color.CYAN);
linearLayout.addView(button3, commomlayoutParams);
StyleButton button4 = new StyleButton(this);
button4.setText("按钮");
button4.setRadius(80);
button4.setBgNormalPressedcolor(Color.GRAY, Color.CYAN);
button4.setTextNormalPressedcolor(Color.RED, Color.WHITE);
linearLayout.addView(button4, commomlayoutParams);
StyleButton button5 = new StyleButton(this);
button5 = new StyleButton(this);
button5.setText("按钮");
button5.setRadius(50);
button5.setTextNormalPressedcolor(Color.BLACK, Color.BLUE);
button5.setBgNormalPressedcolor(Color.WHITE, Color.CYAN);
linearLayout.addView(button5, commomlayoutParams);
StyleButton button6 = new StyleButton(this);
button6.setText("按钮");
button6.setRadius(50);
button6.setTextNormalPressedcolor(Color.BLACK, Color.CYAN);
button6.setBgNormalPressedcolor(Color.CYAN, Color.BLUE);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.leftMargin = 80;
params.rightMargin = 80;
linearLayout.addView(button6, params);
StyleButton button7 = new StyleButton(this);
button7.setText("按钮");
button7.setRadius(80);
linearLayout.addView(button7, layoutParams);
setContentView(linearLayout);是不是使用起来方便快捷呢?不管是换颜色还是换圆角半径这样我们就自定义好了,自己的一个圆角按钮。好了。拿去愉快的使用吧。android自定义控件系列教程----真正的圆角button来了
原文地址:http://blog.csdn.net/codebob/article/details/42916321