您的位置:首页 > 其它

绘制picture

2015-11-19 16:27 190 查看
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。绘制图片
}
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: