为android按钮添加渐变,可绘制

为android按钮添加渐变,可绘制,android,Android,我想创建一个包含渐变和图像的按钮,如图所示: 图像的背景应为渐变色。遵循此操作 您可以创建可动态绘制的渐变。。下课使用 import android.graphics.drawable.GradientDrawable; public class SomeDrawable extends GradientDrawable { public SomeDrawable(int pStartColor, int pCenterColor, int pEndColor, int pStrokeWi

我想创建一个包含渐变和图像的按钮,如图所示:

图像的背景应为渐变色。

遵循此操作

您可以创建可动态绘制的渐变。。下课使用

import android.graphics.drawable.GradientDrawable;

public class SomeDrawable extends GradientDrawable {

public SomeDrawable(int pStartColor, int pCenterColor, int pEndColor, int pStrokeWidth, int pStrokeColor, float cornerRadius) {
    super(Orientation.BOTTOM_TOP,new int[]{pStartColor,pCenterColor,pEndColor});
    setStroke(pStrokeWidth,pStrokeColor);
    setShape(GradientDrawable.RECTANGLE);
    setCornerRadius(cornerRadius);
}
}

并按如下方式使用该类

SomeDrawable drawable = new SomeDrawable(Color.parseColor("Start Color Code"),Color.parseColor("Center Color Code"),Color.parseColor("End Color Code"),1,Color.BLACK,00);
yourLayout.setBackgroundDrawable(drawable);
您还可以为该视图设置图像资源。


<ImageView
            android:id="@+id/ivdpfirst"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/sidebutton"
            android:src="@drawable/ic_me1" />
drawable文件夹中的sidebutton.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item><shape>
        <gradient android:angle="270" android:endColor="#000000" android:startColor="#396AA1" />

        <stroke android:width="1dp" android:color="#000000" />

        <corners android:radius="5dp" />

        <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
    </shape></item>

</selector>


[您可以从这里得到答案][1]只需执行以下步骤,它将帮助您……:)[1] :可以,但也需要放置图标。