BitmapShader图片渲染


import org.apache.http.client.CircularRedirectException;

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;

/**
 * @version 2012-8-14 上午10:45:28
 **/
public class BitGameView extends View implements Runnable {
    Bitmap mBitmap = null;
    int bitwidth = 0;
    int bitheight = 0;
    Paint mPaint = null;
    // bitmap渲染
    Shader mBitmapShader = null;

    ShapeDrawable mShapeDrawable = null;

    public BitGameView(Context context) {
        super(context);
        // 装载资源
        mBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.abc))
                .getBitmap();
        // 得到宽高
        bitwidth = mBitmap.getWidth();
        bitheight = mBitmap.getHeight();
        // 创建BitmapShader对象 已何种方式重复
        mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP,
                Shader.TileMode.CLAMP);
        mPaint = new Paint();
        new Thread(this).start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 自定义为椭圆形
        mShapeDrawable = new ShapeDrawable(new OvalShape());
        // RectShape矩形
        // ArcShape弧形
        // PathShape任意多边形
        // RoundRectShape圆角矩形
        // OvalShape椭圆

        // 设置要绘制的椭圆形的图片
        mShapeDrawable.getPaint().setShader(mBitmapShader);
        // 设置显示区域
        mShapeDrawable.setBounds(0, 0, bitwidth * 2, bitheight * 2);
        // 绘制图片
        mShapeDrawable.draw(canvas);
    }

    @Override
    public void run() {
        while(!Thread.currentThread().isInterrupted()) {
            try {
                Thread.sleep(100);
            }
            catch(Exception e) {
                Thread.currentThread().interrupt();
            }
            postInvalidate();
        }
    }
}

你可能感兴趣的:(thread,exception,null,Class,shader)