Android Paint.setSharer渐变色,shape渐变色、shape文件新建步骤、xml旋转,设置四角弯曲度corners

package com.fpt.drawgradview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.shapes.Shape;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

public class DrawGradView extends View {
    private Paint paint;
    public DrawGradView(Context context) {
        super(context);
        initData();
    }

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

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

    public DrawGradView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initData();
    }
    public void initData(){
        paint=new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(40);
//        paint.setColor(Color.BLUE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawLine(100,50,100,400,paint);
        LinearGradient linearGradient=new LinearGradient(150,50,150,300,new int[]{
                Color.rgb(255,189,22),
                Color.rgb(221,43,6),
                Color.rgb(0,25,233),
                Color.rgb(0,232,210)},
        new float[]{0,.3F,.6F,.9F},Shader.TileMode.CLAMP);
        //new float[]{},中的数据表示相对位置,将150,50,150,300,划分10个单位,.3,.6,.9表示它的绝对位置。300到400,将直接画出rgb(0,232,210)
        paint.setShader(linearGradient);
        canvas.drawLine(150,50,150,400,paint);
    }
}

LinearGradient 的3种模式:

CLAMP:当图片小于绘制尺寸时要进行边界拉伸来填充

REPEAT:当图片小于绘制尺寸时重复平铺

MIRROR:当图片小于绘制尺寸时镜像平铺



    
    
    

新建shape文件步骤如下:

1、在model层,鼠标右击,选择new------>resource;
2、如下图,资源选择drawable,然后element(元素的意思)中打出一个“s”,就有提示的shape了,选择、新建之;

Android Paint.setSharer渐变色,shape渐变色、shape文件新建步骤、xml旋转,设置四角弯曲度corners_第1张图片

旋转




    
        
    

    
        
    







    
    

你可能感兴趣的:(Android Paint.setSharer渐变色,shape渐变色、shape文件新建步骤、xml旋转,设置四角弯曲度corners)