package shader.yb;
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;
/**
* @author yubin
* @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();
}
}
}
BitmapShader图片渲染
最新推荐文章于 2025-11-24 16:02:22 发布
本文介绍了一种使用Android的BitmapShader将普通图片转换为圆形图片的方法。通过继承View并实现Runnable接口,文章展示了如何利用BitmapShader结合ShapeDrawable创建圆形图片,并在自定义View中绘制出来。此外,还提供了线程更新绘图内容的示例。
2341

被折叠的 条评论
为什么被折叠?



