DensityActivity.java revision 3b0146057889eaf6eb1a6ebc86553a95885f074f
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.example.android.apis.graphics; 18 19//Need the following import to get access to the app resources, since this 20//class is in a sub-package. 21import com.example.android.apis.R; 22 23import android.app.Activity; 24import android.app.Application; 25import android.os.Bundle; 26import android.graphics.BitmapFactory; 27import android.graphics.Bitmap; 28import android.graphics.Canvas; 29import android.graphics.drawable.BitmapDrawable; 30import android.graphics.drawable.Drawable; 31import android.widget.LinearLayout; 32import android.widget.TextView; 33import android.widget.ScrollView; 34import android.view.LayoutInflater; 35import android.view.View; 36import android.content.Context; 37import android.content.pm.ApplicationInfo; 38import android.content.pm.PackageManager; 39import android.util.DisplayMetrics; 40import android.util.Log; 41 42/** 43 * This activity demonstrates various ways density can cause the scaling of 44 * bitmaps and drawables. 45 */ 46public class DensityActivity extends Activity { 47 @Override 48 protected void onCreate(Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 51 final LayoutInflater li = (LayoutInflater)getSystemService( 52 LAYOUT_INFLATER_SERVICE); 53 54 this.setTitle(R.string.density_title); 55 LinearLayout root = new LinearLayout(this); 56 root.setOrientation(LinearLayout.VERTICAL); 57 58 LinearLayout layout = new LinearLayout(this); 59 addBitmapDrawable(layout, R.drawable.logo120dpi, true); 60 addBitmapDrawable(layout, R.drawable.logo160dpi, true); 61 addBitmapDrawable(layout, R.drawable.logo240dpi, true); 62 addLabelToRoot(root, "Prescaled bitmap in drawable"); 63 addChildToRoot(root, layout); 64 65 layout = new LinearLayout(this); 66 addBitmapDrawable(layout, R.drawable.logo120dpi, false); 67 addBitmapDrawable(layout, R.drawable.logo160dpi, false); 68 addBitmapDrawable(layout, R.drawable.logo240dpi, false); 69 addLabelToRoot(root, "Autoscaled bitmap in drawable"); 70 addChildToRoot(root, layout); 71 72 layout = new LinearLayout(this); 73 addResourceDrawable(layout, R.drawable.logo120dpi); 74 addResourceDrawable(layout, R.drawable.logo160dpi); 75 addResourceDrawable(layout, R.drawable.logo240dpi); 76 addLabelToRoot(root, "Prescaled resource drawable"); 77 addChildToRoot(root, layout); 78 79 layout = (LinearLayout)li.inflate(R.layout.density_image_views, null); 80 addLabelToRoot(root, "Inflated layout"); 81 addChildToRoot(root, layout); 82 83 layout = (LinearLayout)li.inflate(R.layout.density_styled_image_views, null); 84 addLabelToRoot(root, "Inflated styled layout"); 85 addChildToRoot(root, layout); 86 87 layout = new LinearLayout(this); 88 addCanvasBitmap(layout, R.drawable.logo120dpi, true); 89 addCanvasBitmap(layout, R.drawable.logo160dpi, true); 90 addCanvasBitmap(layout, R.drawable.logo240dpi, true); 91 addLabelToRoot(root, "Prescaled bitmap"); 92 addChildToRoot(root, layout); 93 94 layout = new LinearLayout(this); 95 addCanvasBitmap(layout, R.drawable.logo120dpi, false); 96 addCanvasBitmap(layout, R.drawable.logo160dpi, false); 97 addCanvasBitmap(layout, R.drawable.logo240dpi, false); 98 addLabelToRoot(root, "Autoscaled bitmap"); 99 addChildToRoot(root, layout); 100 101 layout = new LinearLayout(this); 102 addResourceDrawable(layout, R.drawable.logonodpi120); 103 addResourceDrawable(layout, R.drawable.logonodpi160); 104 addResourceDrawable(layout, R.drawable.logonodpi240); 105 addLabelToRoot(root, "No-dpi resource drawable"); 106 addChildToRoot(root, layout); 107 108 layout = new LinearLayout(this); 109 addNinePatchResourceDrawable(layout, R.drawable.smlnpatch120dpi); 110 addNinePatchResourceDrawable(layout, R.drawable.smlnpatch160dpi); 111 addNinePatchResourceDrawable(layout, R.drawable.smlnpatch240dpi); 112 addLabelToRoot(root, "Prescaled 9-patch resource drawable"); 113 addChildToRoot(root, layout); 114 115 setContentView(scrollWrap(root)); 116 } 117 118 private View scrollWrap(View view) { 119 ScrollView scroller = new ScrollView(this); 120 scroller.addView(view, new ScrollView.LayoutParams(ScrollView.LayoutParams.FILL_PARENT, 121 ScrollView.LayoutParams.FILL_PARENT)); 122 return scroller; 123 } 124 125 private void addLabelToRoot(LinearLayout root, String text) { 126 TextView label = new TextView(this); 127 label.setText(text); 128 root.addView(label, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 129 LinearLayout.LayoutParams.WRAP_CONTENT)); 130 } 131 132 private void addChildToRoot(LinearLayout root, LinearLayout layout) { 133 root.addView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 134 LinearLayout.LayoutParams.WRAP_CONTENT)); 135 } 136 137 private void addBitmapDrawable(LinearLayout layout, int resource, boolean scale) { 138 Bitmap bitmap; 139 bitmap = loadAndPrintDpi(resource, scale); 140 141 View view = new View(this); 142 143 final BitmapDrawable d = new BitmapDrawable(getResources(), bitmap); 144 if (!scale) d.setTargetDensity(getResources().getDisplayMetrics()); 145 view.setBackgroundDrawable(d); 146 147 view.setLayoutParams(new LinearLayout.LayoutParams(d.getIntrinsicWidth(), 148 d.getIntrinsicHeight())); 149 layout.addView(view); 150 } 151 152 private void addResourceDrawable(LinearLayout layout, int resource) { 153 View view = new View(this); 154 155 final Drawable d = getResources().getDrawable(resource); 156 view.setBackgroundDrawable(d); 157 158 view.setLayoutParams(new LinearLayout.LayoutParams(d.getIntrinsicWidth(), 159 d.getIntrinsicHeight())); 160 layout.addView(view); 161 } 162 163 private void addCanvasBitmap(LinearLayout layout, int resource, boolean scale) { 164 Bitmap bitmap; 165 bitmap = loadAndPrintDpi(resource, scale); 166 167 ScaledBitmapView view = new ScaledBitmapView(this, bitmap); 168 169 view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 170 LinearLayout.LayoutParams.WRAP_CONTENT)); 171 layout.addView(view); 172 } 173 174 private void addNinePatchResourceDrawable(LinearLayout layout, int resource) { 175 View view = new View(this); 176 177 final Drawable d = getResources().getDrawable(resource); 178 view.setBackgroundDrawable(d); 179 180 Log.i("foo", "9-patch #" + Integer.toHexString(resource) 181 + " w=" + d.getIntrinsicWidth() + " h=" + d.getIntrinsicHeight()); 182 view.setLayoutParams(new LinearLayout.LayoutParams( 183 d.getIntrinsicWidth()*2, d.getIntrinsicHeight()*2)); 184 layout.addView(view); 185 } 186 187 private Bitmap loadAndPrintDpi(int id, boolean scale) { 188 Bitmap bitmap; 189 if (scale) { 190 bitmap = BitmapFactory.decodeResource(getResources(), id); 191 } else { 192 BitmapFactory.Options opts = new BitmapFactory.Options(); 193 opts.inScaled = false; 194 bitmap = BitmapFactory.decodeResource(getResources(), id, opts); 195 } 196 return bitmap; 197 } 198 199 private class ScaledBitmapView extends View { 200 private Bitmap mBitmap; 201 202 public ScaledBitmapView(Context context, Bitmap bitmap) { 203 super(context); 204 mBitmap = bitmap; 205 } 206 207 @Override 208 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 209 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 210 final DisplayMetrics metrics = getResources().getDisplayMetrics(); 211 setMeasuredDimension( 212 mBitmap.getScaledWidth(metrics), 213 mBitmap.getScaledHeight(metrics)); 214 } 215 216 @Override 217 protected void onDraw(Canvas canvas) { 218 super.onDraw(canvas); 219 220 canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null); 221 } 222 } 223} 224