diff options
| author | Dianne Hackborn <hackbod@google.com> | 2009-08-11 18:11:50 -0700 |
|---|---|---|
| committer | Android Git Automerger <android-git-automerger@android.com> | 2009-08-11 18:11:50 -0700 |
| commit | b99b11dec9b6dd87ed66c98feb333fcc8549a8a5 (patch) | |
| tree | 0df966eb3473412234caf752b5133ec63ffe3df3 /samples | |
| parent | 1803b393ec38e1896b8b24e34eb28a2abfa82ab8 (diff) | |
| parent | 46b133a2baf4d4c35801a24893d6a72f1ab82e7f (diff) | |
| download | android_development-b99b11dec9b6dd87ed66c98feb333fcc8549a8a5.tar.gz android_development-b99b11dec9b6dd87ed66c98feb333fcc8549a8a5.tar.bz2 android_development-b99b11dec9b6dd87ed66c98feb333fcc8549a8a5.zip | |
am 46b133a2: Merge change 20690 into donut
Merge commit '46b133a2baf4d4c35801a24893d6a72f1ab82e7f'
* commit '46b133a2baf4d4c35801a24893d6a72f1ab82e7f':
Add density demo.
Diffstat (limited to 'samples')
35 files changed, 521 insertions, 0 deletions
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml index 3df3af247..1e5de0592 100644 --- a/samples/ApiDemos/AndroidManifest.xml +++ b/samples/ApiDemos/AndroidManifest.xml @@ -1643,6 +1643,13 @@ </intent-filter> </activity-alias> + <activity android:name=".graphics.DensityActivity" android:label="Graphics/Density"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.SAMPLE_CODE" /> + </intent-filter> + </activity> + <!-- ************************************* --> <!-- MEDIA SAMPLES --> <!-- ************************************* --> diff --git a/samples/ApiDemos/res/drawable-hdpi/logo240dpi.png b/samples/ApiDemos/res/drawable-hdpi/logo240dpi.png Binary files differnew file mode 100644 index 000000000..4d717a861 --- /dev/null +++ b/samples/ApiDemos/res/drawable-hdpi/logo240dpi.png diff --git a/samples/ApiDemos/res/drawable-hdpi/npatch240dpi.9.png b/samples/ApiDemos/res/drawable-hdpi/npatch240dpi.9.png Binary files differnew file mode 100644 index 000000000..a362b0ffe --- /dev/null +++ b/samples/ApiDemos/res/drawable-hdpi/npatch240dpi.9.png diff --git a/samples/ApiDemos/res/drawable-hdpi/reslogo240dpi.png b/samples/ApiDemos/res/drawable-hdpi/reslogo240dpi.png Binary files differnew file mode 100644 index 000000000..4d717a861 --- /dev/null +++ b/samples/ApiDemos/res/drawable-hdpi/reslogo240dpi.png diff --git a/samples/ApiDemos/res/drawable-hdpi/smlnpatch240dpi.9.png b/samples/ApiDemos/res/drawable-hdpi/smlnpatch240dpi.9.png Binary files differnew file mode 100644 index 000000000..84bdcb06b --- /dev/null +++ b/samples/ApiDemos/res/drawable-hdpi/smlnpatch240dpi.9.png diff --git a/samples/ApiDemos/res/drawable-hdpi/stylogo240dpi.png b/samples/ApiDemos/res/drawable-hdpi/stylogo240dpi.png Binary files differnew file mode 100644 index 000000000..4d717a861 --- /dev/null +++ b/samples/ApiDemos/res/drawable-hdpi/stylogo240dpi.png diff --git a/samples/ApiDemos/res/drawable-ldpi/logo120dpi.png b/samples/ApiDemos/res/drawable-ldpi/logo120dpi.png Binary files differnew file mode 100644 index 000000000..46bbd5bd3 --- /dev/null +++ b/samples/ApiDemos/res/drawable-ldpi/logo120dpi.png diff --git a/samples/ApiDemos/res/drawable-ldpi/npatch120dpi.9.png b/samples/ApiDemos/res/drawable-ldpi/npatch120dpi.9.png Binary files differnew file mode 100644 index 000000000..0d8115bcf --- /dev/null +++ b/samples/ApiDemos/res/drawable-ldpi/npatch120dpi.9.png diff --git a/samples/ApiDemos/res/drawable-ldpi/reslogo120dpi.png b/samples/ApiDemos/res/drawable-ldpi/reslogo120dpi.png Binary files differnew file mode 100644 index 000000000..46bbd5bd3 --- /dev/null +++ b/samples/ApiDemos/res/drawable-ldpi/reslogo120dpi.png diff --git a/samples/ApiDemos/res/drawable-ldpi/smlnpatch120dpi.9.png b/samples/ApiDemos/res/drawable-ldpi/smlnpatch120dpi.9.png Binary files differnew file mode 100644 index 000000000..de8d60764 --- /dev/null +++ b/samples/ApiDemos/res/drawable-ldpi/smlnpatch120dpi.9.png diff --git a/samples/ApiDemos/res/drawable-ldpi/stylogo120dpi.png b/samples/ApiDemos/res/drawable-ldpi/stylogo120dpi.png Binary files differnew file mode 100644 index 000000000..46bbd5bd3 --- /dev/null +++ b/samples/ApiDemos/res/drawable-ldpi/stylogo120dpi.png diff --git a/samples/ApiDemos/res/drawable-nodpi/logonodpi120.png b/samples/ApiDemos/res/drawable-nodpi/logonodpi120.png Binary files differnew file mode 100644 index 000000000..46bbd5bd3 --- /dev/null +++ b/samples/ApiDemos/res/drawable-nodpi/logonodpi120.png diff --git a/samples/ApiDemos/res/drawable-nodpi/logonodpi160.png b/samples/ApiDemos/res/drawable-nodpi/logonodpi160.png Binary files differnew file mode 100644 index 000000000..c23b2ce83 --- /dev/null +++ b/samples/ApiDemos/res/drawable-nodpi/logonodpi160.png diff --git a/samples/ApiDemos/res/drawable-nodpi/logonodpi240.png b/samples/ApiDemos/res/drawable-nodpi/logonodpi240.png Binary files differnew file mode 100644 index 000000000..4d717a861 --- /dev/null +++ b/samples/ApiDemos/res/drawable-nodpi/logonodpi240.png diff --git a/samples/ApiDemos/res/drawable/logo160dpi.png b/samples/ApiDemos/res/drawable/logo160dpi.png Binary files differnew file mode 100644 index 000000000..c23b2ce83 --- /dev/null +++ b/samples/ApiDemos/res/drawable/logo160dpi.png diff --git a/samples/ApiDemos/res/drawable/npatch160dpi.9.png b/samples/ApiDemos/res/drawable/npatch160dpi.9.png Binary files differnew file mode 100644 index 000000000..44d89a9ed --- /dev/null +++ b/samples/ApiDemos/res/drawable/npatch160dpi.9.png diff --git a/samples/ApiDemos/res/drawable/reslogo160dpi.png b/samples/ApiDemos/res/drawable/reslogo160dpi.png Binary files differnew file mode 100644 index 000000000..c23b2ce83 --- /dev/null +++ b/samples/ApiDemos/res/drawable/reslogo160dpi.png diff --git a/samples/ApiDemos/res/drawable/smlnpatch160dpi.9.png b/samples/ApiDemos/res/drawable/smlnpatch160dpi.9.png Binary files differnew file mode 100644 index 000000000..76c4ae8f0 --- /dev/null +++ b/samples/ApiDemos/res/drawable/smlnpatch160dpi.9.png diff --git a/samples/ApiDemos/res/drawable/stylogo160dpi.png b/samples/ApiDemos/res/drawable/stylogo160dpi.png Binary files differnew file mode 100644 index 000000000..c23b2ce83 --- /dev/null +++ b/samples/ApiDemos/res/drawable/stylogo160dpi.png diff --git a/samples/ApiDemos/res/layout/density_image_views.xml b/samples/ApiDemos/res/layout/density_image_views.xml new file mode 100644 index 000000000..6a91497f7 --- /dev/null +++ b/samples/ApiDemos/res/layout/density_image_views.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2009 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="horizontal" + android:layout_width="wrap_content" + android:layout_height="wrap_content"> + + <ImageView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:src="@drawable/reslogo120dpi" /> + + <ImageView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:src="@drawable/reslogo160dpi" /> + + <ImageView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:src="@drawable/reslogo240dpi" /> + +</LinearLayout> diff --git a/samples/ApiDemos/res/layout/density_styled_image_views.xml b/samples/ApiDemos/res/layout/density_styled_image_views.xml new file mode 100644 index 000000000..86c63bf23 --- /dev/null +++ b/samples/ApiDemos/res/layout/density_styled_image_views.xml @@ -0,0 +1,26 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2009 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="horizontal" + android:layout_width="wrap_content" + android:layout_height="wrap_content"> + + <ImageView style="@style/ImageView120dpi" /> + <ImageView style="@style/ImageView160dpi" /> + <ImageView style="@style/ImageView240dpi" /> + +</LinearLayout> diff --git a/samples/ApiDemos/res/values-large-long/strings.xml b/samples/ApiDemos/res/values-large-long/strings.xml new file mode 100644 index 000000000..7c392d8b7 --- /dev/null +++ b/samples/ApiDemos/res/values-large-long/strings.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2007 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<resources> + <string name="density_title">Density: Large Long</string> +</resources> diff --git a/samples/ApiDemos/res/values-large-notlong/strings.xml b/samples/ApiDemos/res/values-large-notlong/strings.xml new file mode 100644 index 000000000..40328eab4 --- /dev/null +++ b/samples/ApiDemos/res/values-large-notlong/strings.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2007 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<resources> + <string name="density_title">Density: Large NotLong</string> +</resources> diff --git a/samples/ApiDemos/res/values-large/strings.xml b/samples/ApiDemos/res/values-large/strings.xml new file mode 100644 index 000000000..c9fb18993 --- /dev/null +++ b/samples/ApiDemos/res/values-large/strings.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2007 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<resources> + <string name="density_title">Density: Large</string> +</resources> diff --git a/samples/ApiDemos/res/values-long/strings.xml b/samples/ApiDemos/res/values-long/strings.xml new file mode 100644 index 000000000..5d15048e4 --- /dev/null +++ b/samples/ApiDemos/res/values-long/strings.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2007 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<resources> + <string name="density_title">Density: Long</string> +</resources> diff --git a/samples/ApiDemos/res/values-normal-long/strings.xml b/samples/ApiDemos/res/values-normal-long/strings.xml new file mode 100644 index 000000000..01a048763 --- /dev/null +++ b/samples/ApiDemos/res/values-normal-long/strings.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2007 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<resources> + <string name="density_title">Density: Normal Long</string> +</resources> diff --git a/samples/ApiDemos/res/values-normal-notlong/strings.xml b/samples/ApiDemos/res/values-normal-notlong/strings.xml new file mode 100644 index 000000000..bea3f8dcb --- /dev/null +++ b/samples/ApiDemos/res/values-normal-notlong/strings.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2007 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<resources> + <string name="density_title">Density: Normal NotLong</string> +</resources> diff --git a/samples/ApiDemos/res/values-normal/strings.xml b/samples/ApiDemos/res/values-normal/strings.xml new file mode 100644 index 000000000..16ff46fc8 --- /dev/null +++ b/samples/ApiDemos/res/values-normal/strings.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2007 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<resources> + <string name="density_title">Density: Normal</string> +</resources> diff --git a/samples/ApiDemos/res/values-notlong/strings.xml b/samples/ApiDemos/res/values-notlong/strings.xml new file mode 100644 index 000000000..a3e78dbb6 --- /dev/null +++ b/samples/ApiDemos/res/values-notlong/strings.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2007 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<resources> + <string name="density_title">Density: NotLong</string> +</resources> diff --git a/samples/ApiDemos/res/values-small-long/strings.xml b/samples/ApiDemos/res/values-small-long/strings.xml new file mode 100644 index 000000000..8526f61db --- /dev/null +++ b/samples/ApiDemos/res/values-small-long/strings.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2007 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<resources> + <string name="density_title">Density: Small Long</string> +</resources> diff --git a/samples/ApiDemos/res/values-small-notlong/strings.xml b/samples/ApiDemos/res/values-small-notlong/strings.xml new file mode 100644 index 000000000..bc8a67687 --- /dev/null +++ b/samples/ApiDemos/res/values-small-notlong/strings.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2007 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<resources> + <string name="density_title">Density: Small NotLong</string> +</resources> diff --git a/samples/ApiDemos/res/values-small/strings.xml b/samples/ApiDemos/res/values-small/strings.xml new file mode 100644 index 000000000..56db73056 --- /dev/null +++ b/samples/ApiDemos/res/values-small/strings.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2007 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<resources> + <string name="density_title">Density: Small</string> +</resources> diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml index 16228d3dd..fa4e61ced 100644 --- a/samples/ApiDemos/res/values/strings.xml +++ b/samples/ApiDemos/res/values/strings.xml @@ -425,6 +425,8 @@ <string name="hide_me">Hide Me!</string> + <string name="density_title">Density: Unknown Screen</string> + <!-- ============================ --> <!-- media examples strings --> <!-- ============================ --> diff --git a/samples/ApiDemos/res/values/styles.xml b/samples/ApiDemos/res/values/styles.xml index 156b78ec3..8cc831287 100644 --- a/samples/ApiDemos/res/values/styles.xml +++ b/samples/ApiDemos/res/values/styles.xml @@ -70,4 +70,21 @@ <item name="android:textStyle">normal</item> </style> + <style name="ImageView120dpi"> + <item name="android:src">@drawable/stylogo120dpi</item> + <item name="android:layout_width">wrap_content</item> + <item name="android:layout_height">wrap_content</item> + </style> + + <style name="ImageView160dpi"> + <item name="android:src">@drawable/stylogo160dpi</item> + <item name="android:layout_width">wrap_content</item> + <item name="android:layout_height">wrap_content</item> + </style> + + <style name="ImageView240dpi"> + <item name="android:src">@drawable/stylogo240dpi</item> + <item name="android:layout_width">wrap_content</item> + <item name="android:layout_height">wrap_content</item> + </style> </resources> diff --git a/samples/ApiDemos/src/com/example/android/apis/graphics/DensityActivity.java b/samples/ApiDemos/src/com/example/android/apis/graphics/DensityActivity.java new file mode 100644 index 000000000..10c42a716 --- /dev/null +++ b/samples/ApiDemos/src/com/example/android/apis/graphics/DensityActivity.java @@ -0,0 +1,223 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.apis.graphics; + +//Need the following import to get access to the app resources, since this +//class is in a sub-package. +import com.example.android.apis.R; + +import android.app.Activity; +import android.app.Application; +import android.os.Bundle; +import android.graphics.BitmapFactory; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.widget.LinearLayout; +import android.widget.TextView; +import android.widget.ScrollView; +import android.view.LayoutInflater; +import android.view.View; +import android.content.Context; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.util.DisplayMetrics; +import android.util.Log; + +/** + * This activity demonstrates various ways density can cause the scaling of + * bitmaps and drawables. + */ +public class DensityActivity extends Activity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + final LayoutInflater li = (LayoutInflater)getSystemService( + LAYOUT_INFLATER_SERVICE); + + this.setTitle(R.string.density_title); + LinearLayout root = new LinearLayout(this); + root.setOrientation(LinearLayout.VERTICAL); + + LinearLayout layout = new LinearLayout(this); + addBitmapDrawable(layout, R.drawable.logo120dpi, true); + addBitmapDrawable(layout, R.drawable.logo160dpi, true); + addBitmapDrawable(layout, R.drawable.logo240dpi, true); + addLabelToRoot(root, "Prescaled bitmap in drawable"); + addChildToRoot(root, layout); + + layout = new LinearLayout(this); + addBitmapDrawable(layout, R.drawable.logo120dpi, false); + addBitmapDrawable(layout, R.drawable.logo160dpi, false); + addBitmapDrawable(layout, R.drawable.logo240dpi, false); + addLabelToRoot(root, "Autoscaled bitmap in drawable"); + addChildToRoot(root, layout); + + layout = new LinearLayout(this); + addResourceDrawable(layout, R.drawable.logo120dpi); + addResourceDrawable(layout, R.drawable.logo160dpi); + addResourceDrawable(layout, R.drawable.logo240dpi); + addLabelToRoot(root, "Prescaled resource drawable"); + addChildToRoot(root, layout); + + layout = (LinearLayout)li.inflate(R.layout.density_image_views, null); + addLabelToRoot(root, "Inflated layout"); + addChildToRoot(root, layout); + + layout = (LinearLayout)li.inflate(R.layout.density_styled_image_views, null); + addLabelToRoot(root, "Inflated styled layout"); + addChildToRoot(root, layout); + + layout = new LinearLayout(this); + addCanvasBitmap(layout, R.drawable.logo120dpi, true); + addCanvasBitmap(layout, R.drawable.logo160dpi, true); + addCanvasBitmap(layout, R.drawable.logo240dpi, true); + addLabelToRoot(root, "Prescaled bitmap"); + addChildToRoot(root, layout); + + layout = new LinearLayout(this); + addCanvasBitmap(layout, R.drawable.logo120dpi, false); + addCanvasBitmap(layout, R.drawable.logo160dpi, false); + addCanvasBitmap(layout, R.drawable.logo240dpi, false); + addLabelToRoot(root, "Autoscaled bitmap"); + addChildToRoot(root, layout); + + layout = new LinearLayout(this); + addResourceDrawable(layout, R.drawable.logonodpi120); + addResourceDrawable(layout, R.drawable.logonodpi160); + addResourceDrawable(layout, R.drawable.logonodpi240); + addLabelToRoot(root, "No-dpi resource drawable"); + addChildToRoot(root, layout); + + layout = new LinearLayout(this); + addNinePatchResourceDrawable(layout, R.drawable.smlnpatch120dpi); + addNinePatchResourceDrawable(layout, R.drawable.smlnpatch160dpi); + addNinePatchResourceDrawable(layout, R.drawable.smlnpatch240dpi); + addLabelToRoot(root, "Prescaled 9-patch resource drawable"); + addChildToRoot(root, layout); + + setContentView(scrollWrap(root)); + } + + private View scrollWrap(View view) { + ScrollView scroller = new ScrollView(this); + scroller.addView(view, new ScrollView.LayoutParams(ScrollView.LayoutParams.FILL_PARENT, + ScrollView.LayoutParams.FILL_PARENT)); + return scroller; + } + + private void addLabelToRoot(LinearLayout root, String text) { + TextView label = new TextView(this); + label.setText(text); + root.addView(label, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, + LinearLayout.LayoutParams.WRAP_CONTENT)); + } + + private void addChildToRoot(LinearLayout root, LinearLayout layout) { + root.addView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, + LinearLayout.LayoutParams.WRAP_CONTENT)); + } + + private void addBitmapDrawable(LinearLayout layout, int resource, boolean scale) { + Bitmap bitmap; + bitmap = loadAndPrintDpi(resource, scale); + + View view = new View(this); + + final BitmapDrawable d = new BitmapDrawable(getResources(), bitmap); + if (!scale) d.setTargetDensity(getResources().getDisplayMetrics()); + view.setBackgroundDrawable(d); + + view.setLayoutParams(new LinearLayout.LayoutParams(d.getIntrinsicWidth(), + d.getIntrinsicHeight())); + layout.addView(view); + } + + private void addResourceDrawable(LinearLayout layout, int resource) { + View view = new View(this); + + final Drawable d = getResources().getDrawable(resource); + view.setBackgroundDrawable(d); + + view.setLayoutParams(new LinearLayout.LayoutParams(d.getIntrinsicWidth(), + d.getIntrinsicHeight())); + layout.addView(view); + } + + private void addCanvasBitmap(LinearLayout layout, int resource, boolean scale) { + Bitmap bitmap; + bitmap = loadAndPrintDpi(resource, scale); + + ScaledBitmapView view = new ScaledBitmapView(this, bitmap); + + view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, + LinearLayout.LayoutParams.WRAP_CONTENT)); + layout.addView(view); + } + + private void addNinePatchResourceDrawable(LinearLayout layout, int resource) { + View view = new View(this); + + final Drawable d = getResources().getDrawable(resource); + view.setBackgroundDrawable(d); + + Log.i("foo", "9-patch #" + Integer.toHexString(resource) + + " w=" + d.getIntrinsicWidth() + " h=" + d.getIntrinsicHeight()); + view.setLayoutParams(new LinearLayout.LayoutParams( + d.getIntrinsicWidth()*2, d.getIntrinsicHeight()*2)); + layout.addView(view); + } + + private Bitmap loadAndPrintDpi(int id, boolean scale) { + Bitmap bitmap; + if (scale) { + bitmap = BitmapFactory.decodeResource(getResources(), id); + } else { + BitmapFactory.Options opts = new BitmapFactory.Options(); + opts.inScaled = false; + bitmap = BitmapFactory.decodeResource(getResources(), id, opts); + } + return bitmap; + } + + private class ScaledBitmapView extends View { + private Bitmap mBitmap; + + public ScaledBitmapView(Context context, Bitmap bitmap) { + super(context); + mBitmap = bitmap; + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + final DisplayMetrics metrics = getResources().getDisplayMetrics(); + setMeasuredDimension( + mBitmap.getScaledWidth(metrics), + mBitmap.getScaledHeight(metrics)); + } + + @Override + protected void onDraw(Canvas canvas) { + super.onDraw(canvas); + + canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null); + } + } +} |
