绘制picture

转载 2015年11月19日 16:27:31
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控件时出现的问题

做了一个简单的对话框,上面有一个picture控件,为了让picture控件上的内容在窗口最小化或者拖出窗口范围之后,仍然得以保存,并且防止闪烁,采用了双缓冲绘图的方式。 重载了OnPaint函数 ...

LTG9258matlab-picture.rar

  • 2015年05月22日 08:50
  • 573KB
  • 下载

网摘》用Picture、API绘图

Picture1.Autoredraw=True 画好的图就不会被抹掉。只有设置为true,才可以在图片框内绘制曲线   Q:根据一组坐标在picture上面画一条线(没有规律),用picture...

picture控件显示图片

  • 2016年05月10日 17:36
  • 3KB
  • 下载

XFA Picture Clause

  • 2015年04月20日 14:24
  • 47KB
  • 下载

MFC中基础控件的使用之Picture Control

MFC中基础控件的使用之Picture Control

visualc_picture-disposal

  • 2013年04月27日 13:16
  • 6.16MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:绘制picture
举报原因:
原因补充:

(最多只允许输入30个字)