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