/*
* 这个deomon演示了Picture的用法,Picture与Bitmap类似, Picture可以记录在Canvas上每个绘制
* 操作(通过beginRecording返回的Canvas),然后回放每个绘图操作。同时也支持将Picture中的内容写到
* Stream中,并支持从Stream恢复Picture。
* Picture与Bitmap的区别在于Picture 只是记录绘图操作而不是绘制后的像素结果,因此存储大小比同样大小
* 的Bitmap要小得多。同时,使用Picture存取绘图操作然后回放到屏幕的操作比直接再使用同样的绘图操作在屏
* 幕上渲染的速度要快的多。
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private class SampleView extends View{
private Picture mPicture;
private Drawable mDrawable;
//绘制图形
private void doSomething(Canvas canvas){
Paint p=new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(0x88ff0000);
canvas.drawCircle(50, 50, 40, p);
p.setColor(Color.GREEN);
p.setTextSize(30);
canvas.drawText("Picture", 60, 60, p);
}
public SampleView(Context context) {
super(context);
setFocusable(true);
//记录绘图操作
mPicture=new Picture();
doSomething(mPicture.beginRecording(200, 100));
mPicture.endRecording();
mDrawable=new PictureDrawable(mPicture);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
//直接将picture绘制到屏幕上
canvas.drawPicture(mPicture);
//绘制图形以适应目标区域的大小,实现绽放功能
//getWith获取到屏幕的宽度,将图形拉升getWith/200倍
canvas.drawPicture(mPicture, new RectF(0,100,getWidth(),200));
//System.out.println("With:"+getWidth());
//使用Drawable的方式绘制图形
mDrawable.setBounds(0,200,getWidth(),300);
mDrawable.draw(canvas);
//使用stream的方式绘制图形
ByteArrayOutputStream os=new ByteArrayOutputStream();
mPicture.writeToStream(os);
ByteArrayInputStream is=new ByteArrayInputStream(os.toByteArray());
canvas.translate(0, 300);
canvas.drawPicture(Picture.createFromStream(is));
}
}
}
Android Api Demos登顶之路(八十二)Graphics-->Picture
最新推荐文章于 2022-10-21 19:21:21 发布