• 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.app.Activity;
22 import android.content.Context;
23 import android.graphics.*;
24 import android.os.Bundle;
25 import android.view.KeyEvent;
26 import android.view.*;
27 
28 import java.io.ByteArrayOutputStream;
29 
30 public class CreateBitmap 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 final int WIDTH = 50;
39     private static final int HEIGHT = 50;
40     private static final int STRIDE = 64;   // must be >= WIDTH
41 
createColors()42     private static int[] createColors() {
43         int[] colors = new int[STRIDE * HEIGHT];
44         for (int y = 0; y < HEIGHT; y++) {
45             for (int x = 0; x < WIDTH; x++) {
46                 int r = x * 255 / (WIDTH - 1);
47                 int g = y * 255 / (HEIGHT - 1);
48                 int b = 255 - Math.min(r, g);
49                 int a = Math.max(r, g);
50                 colors[y * STRIDE + x] = (a << 24) | (r << 16) | (g << 8) | b;
51             }
52         }
53         return colors;
54     }
55 
56     private static class SampleView extends View {
57         private Bitmap[] mBitmaps;
58         private Bitmap[] mJPEG;
59         private Bitmap[] mPNG;
60         private int[]    mColors;
61         private Paint    mPaint;
62 
codec(Bitmap src, Bitmap.CompressFormat format, int quality)63         private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,
64                                     int quality) {
65             ByteArrayOutputStream os = new ByteArrayOutputStream();
66             src.compress(format, quality, os);
67 
68             byte[] array = os.toByteArray();
69             return BitmapFactory.decodeByteArray(array, 0, array.length);
70         }
71 
SampleView(Context context)72         public SampleView(Context context) {
73             super(context);
74             setFocusable(true);
75 
76             mColors = createColors();
77             int[] colors = mColors;
78 
79             mBitmaps = new Bitmap[6];
80             // these three are initialized with colors[]
81             mBitmaps[0] = Bitmap.createBitmap(colors, 0, STRIDE, WIDTH, HEIGHT,
82                                               Bitmap.Config.ARGB_8888);
83             mBitmaps[1] = Bitmap.createBitmap(colors, 0, STRIDE, WIDTH, HEIGHT,
84                                               Bitmap.Config.RGB_565);
85             mBitmaps[2] = Bitmap.createBitmap(colors, 0, STRIDE, WIDTH, HEIGHT,
86                                               Bitmap.Config.ARGB_4444);
87 
88             // these three will have their colors set later
89             mBitmaps[3] = Bitmap.createBitmap(WIDTH, HEIGHT,
90                                               Bitmap.Config.ARGB_8888);
91             mBitmaps[4] = Bitmap.createBitmap(WIDTH, HEIGHT,
92                                               Bitmap.Config.RGB_565);
93             mBitmaps[5] = Bitmap.createBitmap(WIDTH, HEIGHT,
94                                               Bitmap.Config.ARGB_4444);
95             for (int i = 3; i <= 5; i++) {
96                 mBitmaps[i].setPixels(colors, 0, STRIDE, 0, 0, WIDTH, HEIGHT);
97             }
98 
99             mPaint = new Paint();
100             mPaint.setDither(true);
101 
102             // now encode/decode using JPEG and PNG
103             mJPEG = new Bitmap[mBitmaps.length];
104             mPNG = new Bitmap[mBitmaps.length];
105             for (int i = 0; i < mBitmaps.length; i++) {
106                 mJPEG[i] = codec(mBitmaps[i], Bitmap.CompressFormat.JPEG, 80);
107                 mPNG[i] = codec(mBitmaps[i], Bitmap.CompressFormat.PNG, 0);
108             }
109         }
110 
onDraw(Canvas canvas)111         @Override protected void onDraw(Canvas canvas) {
112             canvas.drawColor(Color.WHITE);
113 
114             for (int i = 0; i < mBitmaps.length; i++) {
115                 canvas.drawBitmap(mBitmaps[i], 0, 0, null);
116                 canvas.drawBitmap(mJPEG[i], 80, 0, null);
117                 canvas.drawBitmap(mPNG[i], 160, 0, null);
118                 canvas.translate(0, mBitmaps[i].getHeight());
119             }
120 
121             // draw the color array directly, w/o craeting a bitmap object
122             canvas.drawBitmap(mColors, 0, STRIDE, 0, 0, WIDTH, HEIGHT,
123                               true, null);
124             canvas.translate(0, HEIGHT);
125             canvas.drawBitmap(mColors, 0, STRIDE, 0, 0, WIDTH, HEIGHT,
126                               false, mPaint);
127         }
128     }
129 }
130 
131