使用 Shader 填充颜色
来源:互联网 发布:淘宝天猫贴吧论坛 编辑:程序博客网 时间:2024/05/22 16:56
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>
0 0
- 使用 Shader 填充颜色
- 使用Shader填充图形
- 使用Shader填充图形
- 使用线性渐变颜色填充矩形
- 初学Android,图形图像之使用Shader填充图像(三十三)
- Shader-颜色
- Shader画笔填充
- Android Shader填充图形
- 使用RGB值填充窗口背景 设置字体颜色
- IOS中使用CGContext画图填充渐变颜色图层
- 使用代码修改shape的填充颜色solid
- python下使用cv2.drawContours填充轮廓颜色
- 使用Visio画矢量图 线条组合图形填充颜色
- 关于python 和C++使用cv画矩形并填充颜色同时填充文字
- <Shader>模型颜色动态渐变,lerp的灵活使用
- 填充窗口颜色
- 填充颜色彩条
- DataGridView填充单元格颜色
- Android热修复(动态加载)方案汇总
- SQL 聚集索引和非聚集索引
- Java HashSet特点:不重复(对象的哈希代码一样)、无序
- 删除链表中重复的结点
- 链式存储队列
- 使用 Shader 填充颜色
- 运动目标检测——研究现状
- git自学笔记
- Android使用http协议与服务器通信
- Mac系统下,Hadoop 2.6.2 + Zookeeper 3.4.6 完全分布式配置
- 除法
- 函数指针实战一
- Extjs与json数据格式的混合使用
- 基于Redis实现分布式锁