Home | History | Annotate | Download | only in ninepatch
      1 /*
      2  * Copyright (C) 2009 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.android.ninepatchlab;
     18 
     19 import com.android.ninepatchlab.R;
     20 
     21 import android.app.Activity;
     22 import android.content.Context;
     23 import android.content.res.Resources;
     24 import android.graphics.*;
     25 import android.graphics.drawable.*;
     26 import android.os.Bundle;
     27 import android.os.SystemClock;
     28 import android.view.KeyEvent;
     29 import android.view.*;
     30 
     31 public class NinePatchLab extends Activity {
     32     public NinePatchLab() {}
     33 
     34     Drawable[]  mButtons;
     35     Drawable[]  mBGs;
     36     float       mScale;
     37 
     38     boolean     mShowFPS = true;
     39     boolean     mDoDither = true;
     40     boolean     mDoFilter = true;
     41     int         mCurrBGIndex;
     42 
     43     private static final int FPS_COUNTER_LIMIT = 30;
     44     private int mFPSTime;
     45     private int mFPSCounter;
     46     private int mFPSAve;
     47 
     48     private View mView;
     49 
     50     private void updateTitle() {
     51         String title = "D=" + mDoDither + " F=" + mDoFilter;
     52         if (mShowFPS) {
     53             title += " FPS=" + mFPSAve;
     54         }
     55         setTitle(title);
     56     }
     57 
     58     private static Drawable make_custom_bg() {
     59         int[] colors = new int[] {
     60             //            0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,  0xFFFFFF00, 0xFFFF0000
     61             0xFFFF0000, 0xFF0000FF
     62         };
     63         return new GradientDrawable(GradientDrawable.Orientation.TR_BL,
     64                                     colors);
     65     }
     66 
     67     private static Drawable make_solid_bg() {
     68         return new ColorDrawable(0xFF008800);
     69     }
     70 
     71     private class NPView extends View {
     72 
     73         public NPView(Context context) {
     74             super(context);
     75             setFocusable(true);
     76 
     77             int[] bgs = new int[] {
     78                 R.drawable.bg_grad_blue,
     79                 R.drawable.bg_grad_green,
     80                 R.drawable.bg_grad_grey,
     81                 R.drawable.bg_grad_red,
     82                 R.drawable.bg_grad_yellow,
     83             };
     84             int[] ids = new int[] {
     85                 R.drawable.btn_dark_ticks_stretch_multiple,
     86                 R.drawable.btn_dark_ticks_stretch_single,
     87                 R.drawable.btn_transparent_ticks_stretch_multiple,
     88                 R.drawable.btn_transparent_ticks_stretch_single,
     89                 R.drawable.btn_light_ticks_stretch_multiple,
     90                 R.drawable.btn_light_ticks_stretch_single,
     91             };
     92 
     93             mButtons = new Drawable[ids.length];
     94             mBGs = new Drawable[bgs.length + 2];
     95 
     96             Resources res = context.getResources();
     97 
     98             for (int i = 0; i < ids.length; i++) {
     99                 mButtons[i] = res.getDrawable(ids[i]);
    100             }
    101             for (int i = 0; i < bgs.length; i++) {
    102                 mBGs[i] = res.getDrawable(bgs[i]);
    103             }
    104             mBGs[bgs.length] = make_custom_bg();
    105             mBGs[bgs.length+1] = make_solid_bg();
    106 
    107             mScale = res.getDisplayMetrics().density;
    108         }
    109 
    110         private static final int MARGIN_X = 16;
    111         private static final int MARGIN_Y = 8;
    112 
    113         private void setDrawableFlags(Drawable dr) {
    114             dr.setDither(mDoDither);
    115             dr.setFilterBitmap(mDoFilter);
    116         }
    117 
    118         protected void onDraw(Canvas canvas) {
    119             long now = 0;
    120             if (mShowFPS) {
    121                 now = SystemClock.uptimeMillis();
    122             }
    123 
    124             Drawable bg = mBGs[mCurrBGIndex];
    125             bg.setBounds(0, 0, getWidth(), getHeight());
    126             setDrawableFlags(bg);
    127             bg.draw(canvas);
    128 
    129             final int WIDTH = getWidth() - 2*MARGIN_X;
    130             final int HEIGHT = getHeight() - 2*MARGIN_Y;
    131             final int N = mButtons.length;
    132             final int gapSize = Math.round(mScale * 8);
    133             final int drHeight = (HEIGHT - (N - 1) * gapSize) / N;
    134             final int drWidth = WIDTH;
    135 
    136 //            canvas.drawColor(0xFF5F810C);
    137             canvas.translate(MARGIN_X, MARGIN_Y);
    138 
    139             for (Drawable dr : mButtons) {
    140                 dr.setBounds(0, 0, drWidth, drHeight);
    141                 setDrawableFlags(dr);
    142                 dr.draw(canvas);
    143                 canvas.translate(0, drHeight + gapSize);
    144             }
    145 
    146             if (mShowFPS) {
    147                 mFPSTime += (int)(SystemClock.uptimeMillis() - now);
    148                 mFPSCounter += 1;
    149                 if (mFPSCounter > FPS_COUNTER_LIMIT) {
    150                     mFPSAve = mFPSCounter * 1000 / mFPSTime;
    151                     updateTitle();
    152                     mFPSTime = 0;
    153                     mFPSCounter = 0;
    154                 }
    155                 invalidate();
    156             }
    157         }
    158     }
    159 
    160     private void toggleFPS() {
    161         mShowFPS = !mShowFPS;
    162         if (mShowFPS) {
    163             mFPSCounter = 0;
    164             mFPSTime = 0;
    165             mView.invalidate();
    166         }
    167     }
    168 
    169     @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
    170         switch (keyCode) {
    171             case KeyEvent.KEYCODE_DPAD_DOWN:
    172                 mDoFilter = !mDoFilter;
    173                 updateTitle();
    174                 mView.invalidate();
    175                 return true;
    176             case KeyEvent.KEYCODE_DPAD_UP:
    177                 mDoDither = !mDoDither;
    178                 updateTitle();
    179                 mView.invalidate();
    180                 return true;
    181             case KeyEvent.KEYCODE_DPAD_RIGHT:
    182                 mCurrBGIndex = (mCurrBGIndex + 1) % mBGs.length;
    183                 mView.invalidate();
    184                 return true;
    185             case KeyEvent.KEYCODE_DPAD_LEFT:
    186                 mCurrBGIndex -= 1;
    187                 if (mCurrBGIndex < 0) {
    188                     mCurrBGIndex = 0;
    189                 }
    190                 mView.invalidate();
    191                 return true;
    192             case KeyEvent.KEYCODE_VOLUME_UP:
    193                 toggleFPS();
    194                 return true;
    195             case KeyEvent.KEYCODE_U:
    196             case KeyEvent.KEYCODE_D:
    197             case KeyEvent.KEYCODE_VOLUME_DOWN:
    198                 return super.onKeyDown(keyCode, event);
    199         }
    200         return super.onKeyDown(keyCode, event);
    201     }
    202 
    203     public void onCreate(Bundle icicle) {
    204         super.onCreate(icicle);
    205         mView = new NPView(this);
    206         setContentView(mView);
    207     }
    208 
    209 }
    210 
    211