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.content.Context;
20 import android.graphics.*;
21 import android.graphics.drawable.Drawable;
22 import android.graphics.drawable.PictureDrawable;
23 import android.os.Bundle;
24 import android.view.View;
25 
26 import java.io.*;
27 
28 public class Pictures extends GraphicsActivity {
29 
30     @Override
onCreate(Bundle savedInstanceState)31     protected void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         setContentView(new SampleView(this));
34     }
35 
36     private static class SampleView extends View {
37         private Picture mPicture;
38         private Drawable mDrawable;
39 
drawSomething(Canvas canvas)40         static void drawSomething(Canvas canvas) {
41             Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
42 
43             p.setColor(0x88FF0000);
44             canvas.drawCircle(50, 50, 40, p);
45 
46             p.setColor(Color.GREEN);
47             p.setTextSize(30);
48             canvas.drawText("Pictures", 60, 60, p);
49         }
50 
SampleView(Context context)51         public SampleView(Context context) {
52             super(context);
53             setFocusable(true);
54             setFocusableInTouchMode(true);
55 
56             mPicture = new Picture();
57             drawSomething(mPicture.beginRecording(200, 100));
58             mPicture.endRecording();
59 
60             mDrawable = new PictureDrawable(mPicture);
61         }
62 
onDraw(Canvas canvas)63         @Override protected void onDraw(Canvas canvas) {
64             canvas.drawColor(Color.WHITE);
65 
66             canvas.drawPicture(mPicture);
67 
68             canvas.drawPicture(mPicture, new RectF(0, 100, getWidth(), 200));
69 
70             mDrawable.setBounds(0, 200, getWidth(), 300);
71             mDrawable.draw(canvas);
72 
73             ByteArrayOutputStream os = new ByteArrayOutputStream();
74             mPicture.writeToStream(os);
75             InputStream is = new ByteArrayInputStream(os.toByteArray());
76             canvas.translate(0, 300);
77             canvas.drawPicture(Picture.createFromStream(is));
78         }
79     }
80 }
81 
82