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