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();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值