package com.Aina.Android;
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;
/**
* com.Aina.Android Pro_Shader
*
* @author Aina.huang E-mail: 674023920@qq.com
* @version 创建时间:2010 Jun 17, 2010 9:59:49 AM 类说明
*/
public class GameView extends View {
private Paint mPaint = null;
private Shader mBitmapShader = null;
private Shader mLinearGradient = null;
private Shader mRadialGradient = null;
private Shader mSweepGradient = null;
private Shader mComposeShader = null;
private Bitmap mBitmap = null;
private int imgwidth = 0;
private int imgheight = 0;
private ShapeDrawable mShapeDrawable = null;
public GameView(Context context) {
super(context);
mPaint = new Paint();
mBitmap = ((BitmapDrawable) this.getResources().getDrawable(
R.drawable.img)).getBitmap();// 获得资源
imgwidth = mBitmap.getWidth();
imgheight = mBitmap.getHeight();
mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP,
Shader.TileMode.MIRROR);
mLinearGradient = new LinearGradient(0, 0, 100, 100, new int[] {
Color.RED, Color.BLUE, Color.GREEN, Color.WHITE }, null,
Shader.TileMode.MIRROR);
mRadialGradient = new RadialGradient(100, 100, 50, new int[] {
Color.BLUE, Color.GREEN, Color.RED, Color.WHITE }, null,
Shader.TileMode.REPEAT);
mSweepGradient = new SweepGradient(50, 50, new int[] { Color.RED,
Color.GREEN, Color.BLUE, Color.WHITE }, null);
mComposeShader = new ComposeShader(mBitmapShader, mLinearGradient,
PorterDuff.Mode.DARKEN);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setAntiAlias(true);
// mPaint.setShader(mBitmapShader);
// canvas.drawBitmap(mBitmap, 10, 10, mPaint);
// mShapeDrawable = new ShapeDrawable(new OvalShape());
// Paint paint = mShapeDrawable.getPaint();
// paint.setAntiAlias(true);
// paint.setShader(mBitmapShader);
// mShapeDrawable.setBounds(10, 10, imgwidth, imgheight);
// mShapeDrawable.draw(canvas);
// mPaint.setShader(mLinearGradient);
// canvas.drawRect(0, 0, 100, 100, mPaint);
// mPaint.setShader(mRadialGradient);
// canvas.drawCircle(100, 100, 50, mPaint);
// mPaint.setShader(mSweepGradient);
// canvas.drawRect(0, 0, 100, 100, mPaint);
mPaint.setShader(mComposeShader);
canvas.drawRect(0, 0, imgwidth, imgheight, mPaint);
}
}
package com.Aina.Android;
import android.app.Activity;
import android.os.Bundle;
public class Test_Shader extends Activity {
/** Called when the activity is first created. */
private GameView gv = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gv = new GameView(this);
setContentView(gv);
}
}