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 xmlns:android="http://schemas.android.com/apk/res/android">
<!--
android:startColor="#aa000000" 渐变起始色值
android:centerColor="" 渐变中间色值
android:endColor="#ffffffff" 渐变结束颜色
android:angle="45" 渐变的方向 默认为0 从左向右 ,90时从下向上 必须为45的整数倍
android:type="radial" 渐变类型 有三种 线性linear 放射渐变radial 扫描线性渐变sweep
android:centerX="0.5" 渐变中心相对X坐标只有渐变类型为放射渐变时有效
android:centerY="0.5" 渐变中心相对Y坐标只有渐变类型为放射渐变时有效
android:gradientRadius="100" 渐变半径 非线性放射有效
-->
<gradient
android:startColor="@color/batter_start_blue_color"
android:endColor="@color/batter_end_blue_color"
android:angle="270"
/>
<corners android:radius="8dp"/>
</shape>
新建shape文件步骤如下:
1、在model层,鼠标右击,选择new------>resource;
2、如下图,资源选择drawable,然后element(元素的意思)中打出一个“s”,就有提示的shape了,选择、新建之;
旋转
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<rotate android:drawable="@drawable/round_spinner_fade"
android:fromDegrees="0.0"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:toDegrees="1080.0"/>
</item>
<item >
<rotate android:drawable="@drawable/round_spinner_fade"
android:fromDegrees="720"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:toDegrees="0.0"/>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white"></solid>
<corners android:bottomLeftRadius="12dp"
android:topLeftRadius="12dp"/>
</shape>
private void setGradientColor(TextView view) {
int[] colors = {Color.parseColor("#F3FF17"), Color.parseColor("#FF9A02")};
float[] position = {0f, 1.0f};
LinearGradient mLinearGradient = new LinearGradient(0, 0,
view.getPaint().getTextSize() * view.getText().length(), 0, colors, position, Shader.TileMode.CLAMP);
view.getPaint().setShader(mLinearGradient);
view.invalidate();
LinearGradient mLinearGradient2 = new LinearGradient(0, 0,
view.getPaint().getTextSize() * view.getText().length(), 0, colors, position, Shader.TileMode.CLAMP);
view.getPaint().setShader(mLinearGradient2);
view.invalidate();
}