android t渐变立体按钮,Android 多色渐变按钮

多渐变色自定义view

public class MulticolorGradeView extends View {

private float round;

private RectF mBackGroundRect;

private LinearGradient backGradient;

//默认画笔

private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

private Paint mPaintText = new Paint();

int[] colorlist;

public MulticolorGradeView(Context context, AttributeSet attrs) {

super(context, attrs);

//按钮多渐变色添加

int color1 = Color.parseColor("#8600FF");

int color2 = Color.parseColor("#FF009A");

int color3 = Color.parseColor("#FF0099");

int color4 = Color.parseColor("#FF7846");

int color5 = Color.parseColor("#FF992E");

int color6 = Color.parseColor("#FFDB00");

//也可通过attr进行xml文件内的自定义设置

if (attrs != null) {

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyGradient);

color1 = typedArray.getColor(R.styleable.MyGradient_GradientcolorStart, color1);

color2 = typedArray.getColor(R.styleable.MyGradient_GradientcolorafterStart, color2);

color3 = typedArray.getColor(R.styleable.MyGradient_GradientcolorbeforeEnd, color3);

color4 = typedArray.getColor(R.styleable.MyGradient_GradientcolorEnd, color4);

round = typedArray.getDimension(R.styleable.MyGradient_Gradientround, dip2px(context, 10));

}

//设置抗锯齿

mPaint.setAntiAlias(true);

//设置防抖动

mPaint.setDither(true);

mPaint.setStyle(Paint.Style.FILL);

mPaintText.setAntiAlias(true);

mPaintText.setDither(true);

colorlist = new int[]{color1, color2, color3, color4, color5, color6};

}

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

mBackGroundRect = new RectF(0, 0, w, h);

backGradient = new LinearGradient(0, 0, w, 0, colorlist, null, Shader.TileMode.CLAMP);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

mPaint.setShader(backGradient);

//绘制背景 圆角矩形

if (mBackGroundRect != null) {

canvas.drawRoundRect(mBackGroundRect, round, round, mPaint);

}

}

public static int dip2px(Context context, float dpValue) {

float scale = context.getResources().getDisplayMetrics().density;

return (int) (dpValue * scale + 0.5f);

}

}

style

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中,创建按钮渐变色可以通过自定义布局和使用GradientDrawable或AppCompat drawable。以下是简单的步骤: 1. **使用GradientDrawable**: - 创建一个XML文件,如`button_gradient.xml`: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="#FF0000" <!-- 前端颜色 --> android:endColor="#00FF00" <!-- 后端颜色 --> android:type="linear" <!-- 或者 "radial"/"sweep" --> android:angle="270" <!-- 渐变角度,默认0 --> android:centerX="50%" <!-- 渐变中心点的X值 --> android:centerY="50%" <!-- 渐变中心点的Y值 --> android:shape="rectangle" <!-- 或者 "oval" --> /> <corners android:radius="8dp"/> <!-- 角度圆角 --> <size android:width="wrap_content" android:height="wrap_content"/> <solid android:color="@android:color/white" /> <!-- 填充颜色 --> </shape> ``` - 在你的XML布局中引用这个drawable: ```xml <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_gradient"/> ``` 2. **使用AppCompat drawable**: - 使用`@drawable/selector`来实现状态改变时的渐变效果,比如按下的效果: ```xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_gradient_pressed"/> <item android:drawable="@drawable/button_gradient_normal"/> </selector> ``` - 分别创建`button_gradient_pressed.xml`和`button_gradient_normal.xml`两个文件,替换颜色和样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值