Home | History | Annotate | Download | only in dpi
      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.google.android.test.dpi;
     18 
     19 import android.app.Activity;
     20 import android.app.ActivityThread;
     21 import android.app.Application;
     22 import android.os.Bundle;
     23 import android.graphics.BitmapFactory;
     24 import android.graphics.Bitmap;
     25 import android.graphics.Canvas;
     26 import android.graphics.drawable.BitmapDrawable;
     27 import android.graphics.drawable.Drawable;
     28 import android.widget.LinearLayout;
     29 import android.widget.TextView;
     30 import android.widget.ScrollView;
     31 import android.view.LayoutInflater;
     32 import android.view.View;
     33 import android.content.Context;
     34 import android.content.pm.ApplicationInfo;
     35 import android.content.pm.PackageManager;
     36 import android.content.res.CompatibilityInfo;
     37 import android.util.DisplayMetrics;
     38 import android.util.Log;
     39 
     40 public class DpiTestActivity extends Activity {
     41     public DpiTestActivity() {
     42         super();
     43         init(false);
     44     }
     45 
     46     public DpiTestActivity(boolean noCompat) {
     47         super();
     48         init(noCompat);
     49     }
     50 
     51     public void init(boolean noCompat) {
     52         try {
     53             // This is all a dirty hack.  Don't think a real application should
     54             // be doing it.
     55             Application app = ActivityThread.currentActivityThread().getApplication();
     56             ApplicationInfo ai = app.getPackageManager().getApplicationInfo(
     57                     "com.google.android.test.dpi", 0);
     58             if (noCompat) {
     59                 ai.flags |= ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS
     60                     | ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS
     61                     | ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS
     62                     | ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS
     63                     | ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS
     64                     | ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES;
     65                 app.getResources().setCompatibilityInfo(new CompatibilityInfo(ai,
     66                         getResources().getConfiguration().screenLayout,
     67                         getResources().getConfiguration().smallestScreenWidthDp, false));
     68             }
     69         } catch (PackageManager.NameNotFoundException e) {
     70             throw new RuntimeException("ouch", e);
     71         }
     72     }
     73 
     74     @Override
     75     protected void onCreate(Bundle savedInstanceState) {
     76         super.onCreate(savedInstanceState);
     77 
     78         final LayoutInflater li = (LayoutInflater)getSystemService(
     79                 LAYOUT_INFLATER_SERVICE);
     80 
     81         this.setTitle(R.string.act_title);
     82         LinearLayout root = new LinearLayout(this);
     83         root.setOrientation(LinearLayout.VERTICAL);
     84 
     85         LinearLayout layout = new LinearLayout(this);
     86         addBitmapDrawable(layout, R.drawable.logo120dpi, true);
     87         addBitmapDrawable(layout, R.drawable.logo160dpi, true);
     88         addBitmapDrawable(layout, R.drawable.logo240dpi, true);
     89         addLabelToRoot(root, "Prescaled bitmap in drawable");
     90         addChildToRoot(root, layout);
     91 
     92         layout = new LinearLayout(this);
     93         addBitmapDrawable(layout, R.drawable.logo120dpi, false);
     94         addBitmapDrawable(layout, R.drawable.logo160dpi, false);
     95         addBitmapDrawable(layout, R.drawable.logo240dpi, false);
     96         addLabelToRoot(root, "Autoscaled bitmap in drawable");
     97         addChildToRoot(root, layout);
     98 
     99         layout = new LinearLayout(this);
    100         addResourceDrawable(layout, R.drawable.logo120dpi);
    101         addResourceDrawable(layout, R.drawable.logo160dpi);
    102         addResourceDrawable(layout, R.drawable.logo240dpi);
    103         addLabelToRoot(root, "Prescaled resource drawable");
    104         addChildToRoot(root, layout);
    105 
    106         layout = (LinearLayout)li.inflate(R.layout.image_views, null);
    107         addLabelToRoot(root, "Inflated layout");
    108         addChildToRoot(root, layout);
    109 
    110         layout = (LinearLayout)li.inflate(R.layout.styled_image_views, null);
    111         addLabelToRoot(root, "Inflated styled layout");
    112         addChildToRoot(root, layout);
    113 
    114         layout = new LinearLayout(this);
    115         addCanvasBitmap(layout, R.drawable.logo120dpi, true);
    116         addCanvasBitmap(layout, R.drawable.logo160dpi, true);
    117         addCanvasBitmap(layout, R.drawable.logo240dpi, true);
    118         addLabelToRoot(root, "Prescaled bitmap");
    119         addChildToRoot(root, layout);
    120 
    121         layout = new LinearLayout(this);
    122         addCanvasBitmap(layout, R.drawable.logo120dpi, false);
    123         addCanvasBitmap(layout, R.drawable.logo160dpi, false);
    124         addCanvasBitmap(layout, R.drawable.logo240dpi, false);
    125         addLabelToRoot(root, "Autoscaled bitmap");
    126         addChildToRoot(root, layout);
    127 
    128         layout = new LinearLayout(this);
    129         addResourceDrawable(layout, R.drawable.logonodpi120);
    130         addResourceDrawable(layout, R.drawable.logonodpi160);
    131         addResourceDrawable(layout, R.drawable.logonodpi240);
    132         addLabelToRoot(root, "No-dpi resource drawable");
    133         addChildToRoot(root, layout);
    134 
    135         layout = new LinearLayout(this);
    136         addNinePatchResourceDrawable(layout, R.drawable.smlnpatch120dpi);
    137         addNinePatchResourceDrawable(layout, R.drawable.smlnpatch160dpi);
    138         addNinePatchResourceDrawable(layout, R.drawable.smlnpatch240dpi);
    139         addLabelToRoot(root, "Prescaled 9-patch resource drawable");
    140         addChildToRoot(root, layout);
    141 
    142         setContentView(scrollWrap(root));
    143     }
    144 
    145     private View scrollWrap(View view) {
    146         ScrollView scroller = new ScrollView(this);
    147         scroller.addView(view, new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT,
    148                 ScrollView.LayoutParams.MATCH_PARENT));
    149         return scroller;
    150     }
    151 
    152     private void addLabelToRoot(LinearLayout root, String text) {
    153         TextView label = new TextView(this);
    154         label.setText(text);
    155         root.addView(label, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
    156                 LinearLayout.LayoutParams.WRAP_CONTENT));
    157     }
    158 
    159     private void addChildToRoot(LinearLayout root, LinearLayout layout) {
    160         root.addView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
    161                 LinearLayout.LayoutParams.WRAP_CONTENT));
    162     }
    163 
    164     private void addBitmapDrawable(LinearLayout layout, int resource, boolean scale) {
    165         Bitmap bitmap;
    166         bitmap = loadAndPrintDpi(resource, scale);
    167 
    168         View view = new View(this);
    169 
    170         final BitmapDrawable d = new BitmapDrawable(getResources(), bitmap);
    171         if (!scale) d.setTargetDensity(getResources().getDisplayMetrics());
    172         view.setBackgroundDrawable(d);
    173 
    174         view.setLayoutParams(new LinearLayout.LayoutParams(d.getIntrinsicWidth(),
    175                 d.getIntrinsicHeight()));
    176         layout.addView(view);
    177     }
    178 
    179     private void addResourceDrawable(LinearLayout layout, int resource) {
    180         View view = new View(this);
    181 
    182         final Drawable d = getResources().getDrawable(resource);
    183         view.setBackgroundDrawable(d);
    184 
    185         view.setLayoutParams(new LinearLayout.LayoutParams(d.getIntrinsicWidth(),
    186                 d.getIntrinsicHeight()));
    187         layout.addView(view);
    188     }
    189 
    190     private void addCanvasBitmap(LinearLayout layout, int resource, boolean scale) {
    191         Bitmap bitmap;
    192         bitmap = loadAndPrintDpi(resource, scale);
    193 
    194         ScaledBitmapView view = new ScaledBitmapView(this, bitmap);
    195 
    196         view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
    197                 LinearLayout.LayoutParams.WRAP_CONTENT));
    198         layout.addView(view);
    199     }
    200 
    201     private void addNinePatchResourceDrawable(LinearLayout layout, int resource) {
    202         View view = new View(this);
    203 
    204         final Drawable d = getResources().getDrawable(resource);
    205         view.setBackgroundDrawable(d);
    206 
    207         Log.i("foo", "9-patch #" + Integer.toHexString(resource)
    208                 + " w=" + d.getIntrinsicWidth() + " h=" + d.getIntrinsicHeight());
    209         view.setLayoutParams(new LinearLayout.LayoutParams(
    210                 d.getIntrinsicWidth()*2, d.getIntrinsicHeight()*2));
    211         layout.addView(view);
    212     }
    213 
    214     private Bitmap loadAndPrintDpi(int id, boolean scale) {
    215         Bitmap bitmap;
    216         if (scale) {
    217             bitmap = BitmapFactory.decodeResource(getResources(), id);
    218         } else {
    219             BitmapFactory.Options opts = new BitmapFactory.Options();
    220             opts.inScaled = false;
    221             bitmap = BitmapFactory.decodeResource(getResources(), id, opts);
    222         }
    223         return bitmap;
    224     }
    225 
    226     private class ScaledBitmapView extends View {
    227         private Bitmap mBitmap;
    228 
    229         public ScaledBitmapView(Context context, Bitmap bitmap) {
    230             super(context);
    231             mBitmap = bitmap;
    232         }
    233 
    234         @Override
    235         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    236             super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    237             final DisplayMetrics metrics = getResources().getDisplayMetrics();
    238             setMeasuredDimension(
    239                     mBitmap.getScaledWidth(metrics),
    240                     mBitmap.getScaledHeight(metrics));
    241         }
    242 
    243         @Override
    244         protected void onDraw(Canvas canvas) {
    245             super.onDraw(canvas);
    246 
    247             canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null);
    248         }
    249     }
    250 }
    251