DpiTestActivity.java revision 11ea33471e1a14a8594f0b2cd012d86340dd3bd8
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;
37import android.util.Log;
38
39public class DpiTestActivity extends Activity {
40    public DpiTestActivity() {
41        super();
42        init(false);
43    }
44
45    public DpiTestActivity(boolean noCompat) {
46        super();
47        init(noCompat);
48    }
49
50    public void init(boolean noCompat) {
51        try {
52            // This is all a dirty hack.  Don't think a real application should
53            // be doing it.
54            Application app = ActivityThread.currentActivityThread().getApplication();
55            ApplicationInfo ai = app.getPackageManager().getApplicationInfo(
56                    "com.google.android.test.dpi", 0);
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                    | ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES;
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        layout = new LinearLayout(this);
121        addNinePatchResourceDrawable(layout, R.drawable.smlnpatch120dpi);
122        addNinePatchResourceDrawable(layout, R.drawable.smlnpatch160dpi);
123        addNinePatchResourceDrawable(layout, R.drawable.smlnpatch240dpi);
124        addLabelToRoot(root, "Prescaled 9-patch resource drawable");
125        addChildToRoot(root, layout);
126
127        setContentView(scrollWrap(root));
128    }
129
130    private View scrollWrap(View view) {
131        ScrollView scroller = new ScrollView(this);
132        scroller.addView(view, new ScrollView.LayoutParams(ScrollView.LayoutParams.FILL_PARENT,
133                ScrollView.LayoutParams.FILL_PARENT));
134        return scroller;
135    }
136
137    private void addLabelToRoot(LinearLayout root, String text) {
138        TextView label = new TextView(this);
139        label.setText(text);
140        root.addView(label, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
141                LinearLayout.LayoutParams.WRAP_CONTENT));
142    }
143
144    private void addChildToRoot(LinearLayout root, LinearLayout layout) {
145        root.addView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
146                LinearLayout.LayoutParams.WRAP_CONTENT));
147    }
148
149    private void addBitmapDrawable(LinearLayout layout, int resource, boolean scale) {
150        Bitmap bitmap;
151        bitmap = loadAndPrintDpi(resource, scale);
152
153        View view = new View(this);
154
155        final BitmapDrawable d = new BitmapDrawable(getResources(), bitmap);
156        if (!scale) d.setTargetDensity(getResources().getDisplayMetrics());
157        view.setBackgroundDrawable(d);
158
159        view.setLayoutParams(new LinearLayout.LayoutParams(d.getIntrinsicWidth(),
160                d.getIntrinsicHeight()));
161        layout.addView(view);
162    }
163
164    private void addResourceDrawable(LinearLayout layout, int resource) {
165        View view = new View(this);
166
167        final Drawable d = getResources().getDrawable(resource);
168        view.setBackgroundDrawable(d);
169
170        view.setLayoutParams(new LinearLayout.LayoutParams(d.getIntrinsicWidth(),
171                d.getIntrinsicHeight()));
172        layout.addView(view);
173    }
174
175    private void addCanvasBitmap(LinearLayout layout, int resource, boolean scale) {
176        Bitmap bitmap;
177        bitmap = loadAndPrintDpi(resource, scale);
178
179        ScaledBitmapView view = new ScaledBitmapView(this, bitmap);
180
181        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
182                LinearLayout.LayoutParams.WRAP_CONTENT));
183        layout.addView(view);
184    }
185
186    private void addNinePatchResourceDrawable(LinearLayout layout, int resource) {
187        View view = new View(this);
188
189        final Drawable d = getResources().getDrawable(resource);
190        view.setBackgroundDrawable(d);
191
192        Log.i("foo", "9-patch #" + Integer.toHexString(resource)
193                + " w=" + d.getIntrinsicWidth() + " h=" + d.getIntrinsicHeight());
194        view.setLayoutParams(new LinearLayout.LayoutParams(
195                d.getIntrinsicWidth()*2, d.getIntrinsicHeight()*2));
196        layout.addView(view);
197    }
198
199    private Bitmap loadAndPrintDpi(int id, boolean scale) {
200        Bitmap bitmap;
201        if (scale) {
202            bitmap = BitmapFactory.decodeResource(getResources(), id);
203        } else {
204            BitmapFactory.Options opts = new BitmapFactory.Options();
205            opts.inScaled = false;
206            bitmap = BitmapFactory.decodeResource(getResources(), id, opts);
207        }
208        return bitmap;
209    }
210
211    private class ScaledBitmapView extends View {
212        private Bitmap mBitmap;
213
214        public ScaledBitmapView(Context context, Bitmap bitmap) {
215            super(context);
216            mBitmap = bitmap;
217        }
218
219        @Override
220        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
221            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
222            final DisplayMetrics metrics = getResources().getDisplayMetrics();
223            setMeasuredDimension(
224                    mBitmap.getScaledWidth(metrics),
225                    mBitmap.getScaledHeight(metrics));
226        }
227
228        @Override
229        protected void onDraw(Canvas canvas) {
230            super.onDraw(canvas);
231
232            canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null);
233        }
234    }
235}
236