使用Shader填充图形
来源:互联网 发布:杨幂的鼻子知乎 编辑:程序博客网 时间:2024/05/16 04:43
Shader本身是一个抽象类,它提供了如下实现类:
BitmapShader:使用位图平铺的渲染效果。
LinearGradient:使用线性渐变来填充图形。
RadialGradient:使用圆形渐变来填充图形。
SweepGradient:使用角度渐变来填充图形。
ComposeShader:使用组合渲染效果来填充图形。
下面通过一个实例来演示不同Shader的渲染效果:Activity:
package comlovo.testshader;import android.app.Activity;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.Shader.TileMode;import android.graphics.SweepGradient;import android.os.Bundle;import android.view.View;import com.lovo.view.MyView;public class MainActivity extends Activity {// 声明位图渲染对象private Shader[] shaders = new Shader[5];// 声明颜色数组private int[] colors;MyView myView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myView = (MyView) findViewById(R.id.activity_main_my_view);// 获得Bitmap实例Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);// 设置渐变的颜色组colors = new int[] { Color.RED, Color.GREEN, Color.BLUE };// 实例化BitmapShader,x坐标方向重复图形,y坐标方向镜像图形shaders[0] = new BitmapShader(bm, TileMode.REPEAT, TileMode.MIRROR);// 实例化LinearGradientshaders[1] = new LinearGradient(0, 0, 100, 100, colors, null,TileMode.REPEAT);// 实例化RadialGradientshaders[2] = new RadialGradient(100, 100, 80, colors, null,TileMode.REPEAT);// 实例化SweepGradientshaders[3] = new SweepGradient(160, 160, colors, null);// 实例化ComposeShadershaders[4] = new ComposeShader(shaders[1], shaders[2],PorterDuff.Mode.DARKEN);}public void click(View v) {switch (v.getId()) {case R.id.activity_main_btn1:myView.paint.setShader(shaders[0]);break;case R.id.activity_main_btn2:myView.paint.setShader(shaders[1]);break;case R.id.activity_main_btn3:myView.paint.setShader(shaders[2]);break;case R.id.activity_main_btn4:myView.paint.setShader(shaders[3]);break;case R.id.activity_main_btn5:myView.paint.setShader(shaders[4]);break;}// 重绘界面myView.invalidate();}}
自定义组件类:
package com.lovo.view;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;public class MyView extends View {public Paint paint;public MyView(Context context, AttributeSet attrs) {super(context, attrs);// 设置画笔的颜色paint = new Paint();paint.setColor(Color.RED);// 设置画笔风格paint.setStyle(Paint.Style.FILL);paint.setStrokeWidth(3);// 去锯齿paint.setAntiAlias(true);}@Overrideprotected void onDraw(Canvas canvas) {canvas.drawColor(Color.WHITE);canvas.drawRect(150, 200, 400, 450, paint);}}
布局XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/activity_main_btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="位图" /> <Button android:id="@+id/activity_main_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="线性渐变" /> <Button android:id="@+id/activity_main_btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="圆形渐变" /> <Button android:id="@+id/activity_main_btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="角度渐变" /> <Button android:id="@+id/activity_main_btn5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="混合" /> </LinearLayout> <com.lovo.view.MyView android:id="@+id/activity_main_my_view" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
- 使用Shader填充图形
- 使用Shader填充图形
- Android Shader填充图形
- 使用 Shader 填充颜色
- Android 使用Paint的setShader填充图形
- Python使用matplotlib填充图形指定区域
- 初学Android,图形图像之使用Shader填充图像(三十三)
- Shader画笔填充
- c#使用Region对图形区域构造和填充
- 使用Visio画矢量图 线条组合图形填充颜色
- 封闭图形填充
- 图形填充程序
- VC 图形填充
- VC 图形填充
- flex填充图形,渐变
- 笔记3:填充图形
- MATLAB填充图形
- 图形和Shader技术
- 使用双缓冲技术实现简单画图板
- 使用keychain存储用户敏感信息
- 黑马程序员----GUI
- lua对table进行选择排序
- Lua中对table的复制
- 使用Shader填充图形
- LeetCode 9 - Palindrome Number
- VMware vSphere服务器虚拟化实验十四 vCenter Operatioin Manager
- 形态学图像处理
- Android的Bitmap类-实现图片的分割、缩放、旋转
- 哈弗曼编码算法(输入若干字符 及 出现概率,输出对应的二进制编码)
- 《游戏脚本的设计与开发》-(战棋部分)2.1 快速显示一张战场地图
- 最近要做的文档
- JP Morgan Chase & Co. Interview Prepration(1)