package com.example.xfermodesdemo;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import android.os.Bundle;
import android.view.View;
public class PicturesActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
private Picture mPicture;
private Drawable mDrawable;
public SampleView(Context context) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
mPicture = new Picture();
/**
* Canvas android.graphics.Picture.beginRecording(int width, int
* height)
*
* To record a picture, call beginRecording() and then draw into the
* Canvas that is returned. Nothing we appear on screen, but all of
* the draw commands (e.g. drawRect(...)) will be recorded. To stop
* recording, call endRecording(). At this point the Canvas that was
* returned must no longer be referenced, and nothing should be
* drawn into it
*/
drawSomething(mPicture.beginRecording(200, 100));
/**
* void android.graphics.Picture.endRecording()
*
*
* Call endRecording when the picture is built. After this call, the
* picture may be drawn, but the canvas that was returned by
* beginRecording must not be referenced anymore. This is
* automatically called if Picture.draw() or Canvas.drawPicture() is
* called.
*/
mPicture.endRecording();
mDrawable = new PictureDrawable(mPicture);
}
static void drawSomething(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("Pictures", 60, 60, p);
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
canvas.drawPicture(mPicture);// 1.绘制图片
canvas.drawPicture(mPicture, new RectF(0, 100, getWidth(), 200));// 2.绘制拉伸的图片
mDrawable.setBounds(0, 200, getWidth(), 300);
mDrawable.draw(canvas);// 3.绘制图片
ByteArrayOutputStream os = new ByteArrayOutputStream();
mPicture.writeToStream(os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
canvas.translate(0, 300);
canvas.drawPicture(Picture.createFromStream(is));// 4。绘制图片
}
}
}
绘制picture
最新推荐文章于 2023-07-28 16:19:11 发布