具体的看一下博文:Android学习笔记进阶15之Shader渲染
- 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.Color;
- import android.graphics.ComposeShader;
- import android.graphics.LinearGradient;
- import android.graphics.Paint;
- import android.graphics.PorterDuff;
- import android.graphics.RadialGradient;
- import android.graphics.Shader;
- import android.graphics.SweepGradient;
- 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 LinearGradient linearGradient = null;
- private ComposeShader composeShader = null;
- private SweepGradient sweepGradient = null;
- private RadialGradient radialGradient = null;
- 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.v))
- .getBitmap();
- BitmapWidth = bitmap.getWidth();
- BitmapHeight = bitmap.getHeight();
- // 构造渲染器LinearGradient
- linearGradient = new LinearGradient(0, 0, 100, 100, new int[] {
- Color.RED, Color.GREEN, Color.BLUE, Color.WHITE }, null,
- Shader.TileMode.REPEAT);
- paint = new Paint();
- }
- @Override
- protected void onDraw(Canvas canvas)
- {
- super.onDraw(canvas);
- paint.setShader(linearGradient);
- canvas.drawRect(20, 50, 320, 156, paint);
- }
- }