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