Android 不仅可以使用颜色填充图形,还可以使用shader对象指定的渲染效果来填充图形.
Shader 本身是一个抽象类.提供了如下实现类:
- BitmapShader, 使用位图平铺的渲染效果.
- ComposeShader, 使用组合渲染效果来填充图片.
- LinearGradient, 使用线性渐变来填充图形
- RadialGradient, 使用圆形渐变来填充图形
- SweepGradient 使用角度渐变来填充图形
主界面
package com.test.shadertest;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
/**
* 使用shader 来渲染图片
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Shader[] mShaders = new Shader[5]; //声明位图渲染 对象
private int[] colors; //声明颜色数组
MyView mMyView; //自定义View
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMyView = (MyView) findViewById(R.id.my_view);
//获取 Bitmap 实例化对象
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.water);
//设置渐变颜色的数组,按照 红绿蓝渐变
colors = new int[]{Color.RED, Color.GREEN, Color.BLUE};
//实例化 BitmapShader对象 x坐标方向重复图形,y坐标方向镜像图形
mShaders[0] = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR); // TileMode 可以翻译成平铺模式
//实例化线性渐变
mShaders[1] = new LinearGradient(0, 0, 100, 100, colors, null, Shader.TileMode.REPEAT);
//实例化圆形渐变
mShaders[2] = new RadialGradient(100, 100, 80, colors, null, Shader.TileMode.REPEAT);
//实例化角度渐变
mShaders[3] = new SweepGradient(160, 160, colors, null);
//实例化 组合渲染效果
mShaders[4] = new ComposeShader(mShaders[2], mShaders[0], PorterDuff.Mode.DARKEN);
Button btn1, btn2, btn3, btn4, btn5;
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn4 = (Button) findViewById(R.id.button4);
btn5 = (Button) findViewById(R.id.button5);
btn1.setOnClickListener(MainActivity.this);
btn2.setOnClickListener(MainActivity.this);
btn3.setOnClickListener(MainActivity.this);
btn4.setOnClickListener(MainActivity.this);
btn5.setOnClickListener(MainActivity.this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
mMyView.mPaint.setShader(mShaders[0]);
break;
case R.id.button2:
mMyView.mPaint.setShader(mShaders[1]);
break;
case R.id.button3:
mMyView.mPaint.setShader(mShaders[2]);
break;
case R.id.button4:
mMyView.mPaint.setShader(mShaders[3]);
break;
case R.id.button5:
mMyView.mPaint.setShader(mShaders[4]);
break;
}
//重绘界面
mMyView.invalidate();
}
}
自定义 view
package com.test.shadertest;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by Administrator on 2016/6/25.
*/
public class MyView extends View {
//声明画笔
Paint mPaint;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setColor(Color.RED);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//使用指定的 mpaint对象画矩形
canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.test.shadertest.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5"
>
<Button
android:text="位图"
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:background="#ff0"
android:textSize="12dp"
android:text="线性渐变"
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:textSize="12dp"
android:text="圆形渐变"
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:textSize="12dp"
android:text="角度渐变"
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<Button
android:textSize="12dp"
android:text="组合渲染"
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
<com.test.shadertest.MyView
android:id="@+id/my_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>