標籤:
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
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重複方式不一樣,他是以鏡像方式平鋪。
首先看一下:
還是直接上代碼:
MainActivity:
[java] view plain copy
- package com.tony.shader;
-
- import android.os.Bundle;
- import android.app.Activity;
-
- public class MainActivity extends Activity {
-
- private BitmapShaderView shaderView;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- shaderView = new BitmapShaderView(this);
- setContentView(shaderView);
-
- }
-
- }
BitmapShaderView:
[java] view plain copy
- package com.tony.shader;
-
- 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.util.AttributeSet;
- import android.view.View;
-
- public class BitmapShaderView 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 BitmapShaderView(Context context) {
- super(context);
-
- // 得到映像
- bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.cat))
- .getBitmap();
- BitmapWidth = bitmap.getWidth();
- BitmapHeight = bitmap.getHeight();
- // 構造渲染器BitmapShader
- bitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR,Shader.TileMode.REPEAT);
- }
-
- public BitmapShaderView(Context context,AttributeSet set) {
- super(context, set);
- }
-
-
- @Override
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- super.onDraw(canvas);
- //將圖片裁剪為橢圓形
- //構建ShapeDrawable對象並定義形狀為橢圓
- shapeDrawable = new ShapeDrawable(new OvalShape());
- //得到畫筆並設定渲染器
- shapeDrawable.getPaint().setShader(bitmapShader);
- //設定顯示地區
- shapeDrawable.setBounds(20, 20,BitmapWidth-140,BitmapHeight);
- //繪製shapeDrawable
- shapeDrawable.draw(canvas);
- }
-
- }
Android 顏色渲染(四) BitmapShader位元影像渲染