DpiTestActivity.java revision d24b8183b93e781080b2c16c487e60d51c12da31
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.os.Bundle;
21import android.graphics.BitmapFactory;
22import android.graphics.Bitmap;
23import android.graphics.Canvas;
24import android.graphics.drawable.BitmapDrawable;
25import android.graphics.drawable.Drawable;
26import android.widget.LinearLayout;
27import android.widget.TextView;
28import android.widget.ScrollView;
29import android.view.View;
30import android.content.Context;
31
32public class DpiTestActivity extends Activity {
33    @Override
34    protected void onCreate(Bundle savedInstanceState) {
35        super.onCreate(savedInstanceState);
36
37        LinearLayout root = new LinearLayout(this);
38        root.setOrientation(LinearLayout.VERTICAL);
39
40        LinearLayout layout = new LinearLayout(this);
41        addBitmapDrawable(layout, R.drawable.logo120dpi, true);
42        addBitmapDrawable(layout, R.drawable.logo160dpi, true);
43        addBitmapDrawable(layout, R.drawable.logo240dpi, true);
44        addLabelToRoot(root, "Prescaled bitmap in drawable");
45        addChildToRoot(root, layout);
46
47        layout = new LinearLayout(this);
48        addBitmapDrawable(layout, R.drawable.logo120dpi, false);
49        addBitmapDrawable(layout, R.drawable.logo160dpi, false);
50        addBitmapDrawable(layout, R.drawable.logo240dpi, false);
51        addLabelToRoot(root, "Autoscaled bitmap in drawable");
52        addChildToRoot(root, layout);
53
54        layout = new LinearLayout(this);
55        addResourceDrawable(layout, R.drawable.logo120dpi);
56        addResourceDrawable(layout, R.drawable.logo160dpi);
57        addResourceDrawable(layout, R.drawable.logo240dpi);
58        addLabelToRoot(root, "Prescaled resource drawable");
59        addChildToRoot(root, layout);
60
61        layout = new LinearLayout(this);
62        addCanvasBitmap(layout, R.drawable.logo120dpi, true);
63        addCanvasBitmap(layout, R.drawable.logo160dpi, true);
64        addCanvasBitmap(layout, R.drawable.logo240dpi, true);
65        addLabelToRoot(root, "Prescaled bitmap");
66        addChildToRoot(root, layout);
67
68        layout = new LinearLayout(this);
69        addCanvasBitmap(layout, R.drawable.logo120dpi, false);
70        addCanvasBitmap(layout, R.drawable.logo160dpi, false);
71        addCanvasBitmap(layout, R.drawable.logo240dpi, false);
72        addLabelToRoot(root, "Autoscaled bitmap");
73        addChildToRoot(root, layout);
74
75        setContentView(scrollWrap(root));
76    }
77
78    private View scrollWrap(View view) {
79        ScrollView scroller = new ScrollView(this);
80        scroller.addView(view, new ScrollView.LayoutParams(ScrollView.LayoutParams.FILL_PARENT,
81                ScrollView.LayoutParams.FILL_PARENT));
82        return scroller;
83    }
84
85    private void addLabelToRoot(LinearLayout root, String text) {
86        TextView label = new TextView(this);
87        label.setText(text);
88        root.addView(label, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
89                LinearLayout.LayoutParams.WRAP_CONTENT));
90    }
91
92    private void addChildToRoot(LinearLayout root, LinearLayout layout) {
93        root.addView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
94                LinearLayout.LayoutParams.WRAP_CONTENT));
95    }
96
97    private void addBitmapDrawable(LinearLayout layout, int resource, boolean scale) {
98        Bitmap bitmap;
99        bitmap = loadAndPrintDpi(resource, scale);
100
101        View view = new View(this);
102
103        final BitmapDrawable d = new BitmapDrawable(bitmap);
104        if (!scale) d.setDensityScale(getResources().getDisplayMetrics());
105        view.setBackgroundDrawable(d);
106
107        view.setLayoutParams(new LinearLayout.LayoutParams(d.getIntrinsicWidth(),
108                d.getIntrinsicHeight()));
109        layout.addView(view);
110    }
111
112    private void addResourceDrawable(LinearLayout layout, int resource) {
113        View view = new View(this);
114
115        final Drawable d = getResources().getDrawable(resource);
116        view.setBackgroundDrawable(d);
117
118        view.setLayoutParams(new LinearLayout.LayoutParams(d.getIntrinsicWidth(),
119                d.getIntrinsicHeight()));
120        layout.addView(view);
121    }
122
123    private void addCanvasBitmap(LinearLayout layout, int resource, boolean scale) {
124        Bitmap bitmap;
125        bitmap = loadAndPrintDpi(resource, scale);
126
127        ScaledBitmapView view = new ScaledBitmapView(this, bitmap);
128
129        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
130                LinearLayout.LayoutParams.WRAP_CONTENT));
131        layout.addView(view);
132    }
133
134    private Bitmap loadAndPrintDpi(int id, boolean scale) {
135        Bitmap bitmap;
136        if (scale) {
137            bitmap = BitmapFactory.decodeResource(getResources(), id);
138        } else {
139            BitmapFactory.Options opts = new BitmapFactory.Options();
140            opts.inScaled = false;
141            bitmap = BitmapFactory.decodeResource(getResources(), id, opts);
142        }
143        return bitmap;
144    }
145
146    private class ScaledBitmapView extends View {
147        private Bitmap mBitmap;
148
149        public ScaledBitmapView(Context context, Bitmap bitmap) {
150            super(context);
151            mBitmap = bitmap;
152        }
153
154        @Override
155        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
156            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
157            setMeasuredDimension(mBitmap.getScaledWidth(), mBitmap.getScaledHeight());
158        }
159
160        @Override
161        protected void onDraw(Canvas canvas) {
162            super.onDraw(canvas);
163
164            canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null);
165        }
166    }
167}
168