canvas教程

Android学习笔记进阶16之BitmapShader

字号+ 作者:H5之家 来源:H5之家 2015-10-25 14:13 我要评论( )

最近在做媒体盒子的项目,接触到音视频的编解码,于是开启FFmpeg的研究之旅。记得有大牛说过show me your data ,那么就从数据结构开始吧。

<1>简介

具体的看一下博文:Android学习笔记进阶15之Shader渲染

 

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 重复方式不一样,他是以镜像方式平铺。

还是不太明白?那看一下效果图吧!

                                       REPEAT                                                                                                                       MIRROR

<2>具体实现

 

package xiaosi.BitmapShader; import android.app.Activity; import android.os.Bundle; public class BitmapShaderActivity extends Activity { /** Called when the activity is first created. */ private BitmapShaders bitmapShaders = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); bitmapShaders = new BitmapShaders(this); setContentView(bitmapShaders); } }


 

BitmapShaders.java

package xiaosi.BitmapShader; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Shader; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.OvalShape; import android.view.View; public class BitmapShaders extends View { private BitmapShader bitmapShader = null; private Bitmap bitmap = null; private Paint paint = null; private ShapeDrawable shapeDrawable = null; private int BitmapWidth = 0; private int BitmapHeight = 0; public BitmapShaders(Context context) { super(context); //得到图像 bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.h)).getBitmap(); BitmapWidth = bitmap.getWidth(); BitmapHeight = bitmap.getHeight(); //构造渲染器BitmapShader bitmapShader = new BitmapShader(bitmap,Shader.TileMode.MIRROR,Shader.TileMode.REPEAT); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //将图片裁剪为椭圆形 //构建ShapeDrawable对象并定义形状为椭圆 shapeDrawable = new ShapeDrawable(new OvalShape()); //得到画笔并设置渲染器 shapeDrawable.getPaint().setShader(bitmapShader); //设置显示区域 shapeDrawable.setBounds(20, 20,BitmapWidth-60,BitmapHeight-60); //绘制shapeDrawable shapeDrawable.draw(canvas); } }


 

 

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

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • HTML5新特性详解(三)

    HTML5新特性详解(三)

    2017-04-30 16:03

  • 学习慕课网canvas倒计时实例笔记

    学习慕课网canvas倒计时实例笔记

    2017-04-30 14:01

  • 从一个画板demo学习canvas

    从一个画板demo学习canvas

    2017-04-30 13:00

  • canvas的神奇用法 javascript技巧笔记 CFEI.NET

    canvas的神奇用法 javascript技巧笔记 CFEI.NET

    2017-04-30 12:00

网友点评
r