首页 技术 正文
技术 2022年11月17日
0 收藏 449 点赞 3,960 浏览 2211 个字

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android 颜色处理(四) BitmapShader位图渲染

Android 颜色渲染(四) BitmapShader位图渲染

public   BitmapShader(Bitmap bitmap,Shader.TileMode tileX,Shader.TileMode tileY)

调用这个方法来产生一个画有一个位图的渲染器(Shader)。

bitmap   在渲染器内使用的位图

tileX      The tiling mode for x to draw the bitmap in.   在位图上X方向渲染器平铺模式

tileY     The tiling mode for y to draw the bitmap in.    在位图上Y方向渲染器平铺模式

TileMode:

CLAMP  :如果渲染器超出原始边界范围,会复制范围内边缘染色。

REPEAT :横向和纵向的重复渲染器图片,平铺。

MIRROR :横向和纵向的重复渲染器图片,这个和REPEAT重复方式不一样,他是以镜像方式平铺。

首先看一下效果图:

Android 颜色渲染(四) BitmapShader位图渲染

还是直接上代码:

MainActivity:

  1. package com.tony.shader;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. public class MainActivity extends Activity {
  5. private BitmapShaderView shaderView;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. shaderView = new BitmapShaderView(this);
  10. setContentView(shaderView);
  11. }
  12. }

BitmapShaderView:

    1. package com.tony.shader;
    2. import android.content.Context;
    3. import android.graphics.Bitmap;
    4. import android.graphics.BitmapShader;
    5. import android.graphics.Canvas;
    6. import android.graphics.Paint;
    7. import android.graphics.Shader;
    8. import android.graphics.drawable.BitmapDrawable;
    9. import android.graphics.drawable.ShapeDrawable;
    10. import android.graphics.drawable.shapes.OvalShape;
    11. import android.util.AttributeSet;
    12. import android.view.View;
    13. public class BitmapShaderView extends View {
    14. private BitmapShader bitmapShader = null;
    15. private Bitmap bitmap = null;
    16. private Paint paint = null;
    17. private ShapeDrawable shapeDrawable = null;
    18. private int BitmapWidth = 0;
    19. private int BitmapHeight = 0;
    20. public BitmapShaderView(Context context) {
    21. super(context);
    22. // 得到图像
    23. bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.cat))
    24. .getBitmap();
    25. BitmapWidth = bitmap.getWidth();
    26. BitmapHeight = bitmap.getHeight();
    27. // 构造渲染器BitmapShader
    28. bitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR,Shader.TileMode.REPEAT);
    29. }
    30. public BitmapShaderView(Context context,AttributeSet set) {
    31. super(context, set);
    32. }
    33. @Override
    34. protected void onDraw(Canvas canvas) {
    35. // TODO Auto-generated method stub
    36. super.onDraw(canvas);
    37. //将图片裁剪为椭圆形
    38. //构建ShapeDrawable对象并定义形状为椭圆
    39. shapeDrawable = new ShapeDrawable(new OvalShape());
    40. //得到画笔并设置渲染器
    41. shapeDrawable.getPaint().setShader(bitmapShader);
    42. //设置显示区域
    43. shapeDrawable.setBounds(20, 20,BitmapWidth-140,BitmapHeight);
    44. //绘制shapeDrawable
    45. shapeDrawable.draw(canvas);
    46. }
    47. }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,997
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,355
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,138
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848