您的位置:首页 > 其它

BitmapShader图片渲染

2012-08-14 16:28 267 查看
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();
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: