• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 com.example.android.apis.R;
20 
21 import android.content.Context;
22 import android.graphics.*;
23 import android.graphics.drawable.*;
24 import android.os.Bundle;
25 import android.view.*;
26 
27 import java.io.InputStream;
28 import java.io.ByteArrayOutputStream;
29 
30 public class BitmapDecode 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 Bitmap mBitmap;
40         private Bitmap mBitmap2;
41         private Bitmap mBitmap3;
42         private Bitmap mBitmap4;
43         private Drawable mDrawable;
44 
45         private Movie mMovie;
46         private long mMovieStart;
47 
48         //Set to false to use decodeByteArray
49         private static final boolean DECODE_STREAM = true;
50 
streamToBytes(InputStream is)51         private static byte[] streamToBytes(InputStream is) {
52             ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
53             byte[] buffer = new byte[1024];
54             int len;
55             try {
56                 while ((len = is.read(buffer)) >= 0) {
57                     os.write(buffer, 0, len);
58                 }
59             } catch (java.io.IOException e) {
60             }
61             return os.toByteArray();
62         }
63 
SampleView(Context context)64         public SampleView(Context context) {
65             super(context);
66             setFocusable(true);
67 
68             java.io.InputStream is;
69             is = context.getResources().openRawResource(R.drawable.beach);
70 
71             BitmapFactory.Options opts = new BitmapFactory.Options();
72             Bitmap bm;
73 
74             opts.inJustDecodeBounds = true;
75             bm = BitmapFactory.decodeStream(is, null, opts);
76 
77             // now opts.outWidth and opts.outHeight are the dimension of the
78             // bitmap, even though bm is null
79 
80             opts.inJustDecodeBounds = false;    // this will request the bm
81             opts.inSampleSize = 4;             // scaled down by 4
82             bm = BitmapFactory.decodeStream(is, null, opts);
83 
84             mBitmap = bm;
85 
86             // decode an image with transparency
87             is = context.getResources().openRawResource(R.drawable.frog);
88             mBitmap2 = BitmapFactory.decodeStream(is);
89 
90             // create a deep copy of it using getPixels() into different configs
91             int w = mBitmap2.getWidth();
92             int h = mBitmap2.getHeight();
93             int[] pixels = new int[w*h];
94             mBitmap2.getPixels(pixels, 0, w, 0, 0, w, h);
95             mBitmap3 = Bitmap.createBitmap(pixels, 0, w, w, h,
96                                            Bitmap.Config.ARGB_8888);
97             mBitmap4 = Bitmap.createBitmap(pixels, 0, w, w, h,
98                                            Bitmap.Config.ARGB_4444);
99 
100             mDrawable = context.getResources().getDrawable(R.drawable.button);
101             mDrawable.setBounds(150, 20, 300, 100);
102 
103             is = context.getResources().openRawResource(R.drawable.animated_gif);
104 
105             if (DECODE_STREAM) {
106                 mMovie = Movie.decodeStream(is);
107             } else {
108                 byte[] array = streamToBytes(is);
109                 mMovie = Movie.decodeByteArray(array, 0, array.length);
110             }
111         }
112 
113         @Override
onDraw(Canvas canvas)114         protected void onDraw(Canvas canvas) {
115             canvas.drawColor(0xFFCCCCCC);
116 
117             Paint p = new Paint();
118             p.setAntiAlias(true);
119 
120             canvas.drawBitmap(mBitmap, 10, 10, null);
121             canvas.drawBitmap(mBitmap2, 10, 170, null);
122             canvas.drawBitmap(mBitmap3, 110, 170, null);
123             canvas.drawBitmap(mBitmap4, 210, 170, null);
124 
125             mDrawable.draw(canvas);
126 
127             long now = android.os.SystemClock.uptimeMillis();
128             if (mMovieStart == 0) {   // first time
129                 mMovieStart = now;
130             }
131             if (mMovie != null) {
132                 int dur = mMovie.duration();
133                 if (dur == 0) {
134                     dur = 1000;
135                 }
136                 int relTime = (int)((now - mMovieStart) % dur);
137                 mMovie.setTime(relTime);
138                 mMovie.draw(canvas, getWidth() - mMovie.width(),
139                             getHeight() - mMovie.height());
140                 invalidate();
141             }
142         }
143     }
144 }
145