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