Notice: 函数 WP_Scripts::localize 的调用方法不正确$l10n 参数必须是一个数组。若要将任意数据传递给脚本,请改用 wp_add_inline_script() 函数。 请查阅调试 WordPress来获取更多信息。 (这个消息是在 5.7.0 版本添加的。) in /data/www/appblog/wp-includes/functions.php on line 6131

Android颜色渐变(gradient)实现

XML

shape_gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="-90"
        android:startColor="@color/colorPrimaryDark"
        android:endColor="@color/colorPrimary"
        />
</shape>

LinearGradient

/**
 @param x0           起点X坐标
 @param y0           起点Y坐标
 @param x1           终点X坐标
 @param y1           终点Y坐标
 @param  colors      所有颜色渐变集合
 @param  positions   可以设置均匀渐变,也可以按照自定义比例进行渐变,还可以为null。假设1为整个渐变的长度,我们设置的所有颜色(假设有4种颜色),都以同等的权重(渐变长度比例0.25:0.25:0.25:0.25)进行颜色渐变。
 @param  tile        着色器的不同模式
*/
public LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[],
           TileMode tile)
public class MyView extends View {

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //获取View的宽高
        int width = getWidth();
        int height = getHeight();

        int colorStart = getResources().getColor(R.color.colorStart);
        int colorCenter = getResources().getColor(R.color.colorCenter);
        int colorEnd = getResources().getColor(R.color.colorEnd);

        Paint paint = new Paint();
        LinearGradient backGradient = new LinearGradient(0, 0, 0, height, new int[]{colorStart, colorCenter ,colorEnd}, null, Shader.TileMode.CLAMP);
        paint.setShader(backGradient);
        canvas.drawRect(0, 0, width, height, paint);
    }
}
上一篇 Android基于模糊滤镜(MaskFilter)的阴影边框渐变效果
下一篇 Android RadioGroup流式布局及动态添加RadioButton