1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.graphics; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.graphics.*; 22 import android.graphics.drawable.Drawable; 23 import android.graphics.drawable.PictureDrawable; 24 import android.os.Bundle; 25 import android.view.KeyEvent; 26 import android.view.View; 27 28 import java.io.*; 29 30 public class Pictures extends GraphicsActivity { 31 32 @Override onCreate(Bundle savedInstanceState)33 protected void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 setContentView(new SampleView(this)); 36 } 37 38 private static class SampleView extends View { 39 private Picture mPicture; 40 private Drawable mDrawable; 41 drawSomething(Canvas canvas)42 static void drawSomething(Canvas canvas) { 43 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); 44 45 p.setColor(0x88FF0000); 46 canvas.drawCircle(50, 50, 40, p); 47 48 p.setColor(Color.GREEN); 49 p.setTextSize(30); 50 canvas.drawText("Pictures", 60, 60, p); 51 } 52 SampleView(Context context)53 public SampleView(Context context) { 54 super(context); 55 setFocusable(true); 56 setFocusableInTouchMode(true); 57 58 mPicture = new Picture(); 59 drawSomething(mPicture.beginRecording(200, 100)); 60 mPicture.endRecording(); 61 62 mDrawable = new PictureDrawable(mPicture); 63 } 64 onDraw(Canvas canvas)65 @Override protected void onDraw(Canvas canvas) { 66 canvas.drawColor(Color.WHITE); 67 68 canvas.drawPicture(mPicture); 69 70 canvas.drawPicture(mPicture, new RectF(0, 100, getWidth(), 200)); 71 72 mDrawable.setBounds(0, 200, getWidth(), 300); 73 mDrawable.draw(canvas); 74 75 ByteArrayOutputStream os = new ByteArrayOutputStream(); 76 mPicture.writeToStream(os); 77 InputStream is = new ByteArrayInputStream(os.toByteArray()); 78 canvas.translate(0, 300); 79 canvas.drawPicture(Picture.createFromStream(is)); 80 } 81 } 82 } 83 84