绘制picture

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。绘制图片
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值