summaryrefslogtreecommitdiffstats
path: root/samples/devbytes/graphics/BitmapAllocation
diff options
context:
space:
mode:
authorChet Haase <chet@google.com>2013-01-10 16:19:38 -0800
committerChet Haase <chet@google.com>2013-01-14 15:21:15 -0800
commit74497f33361e3e9f0cb33d3111b5198444af5794 (patch)
treefdd5424398128a61c1c1bf0c150c76fd89919b3c /samples/devbytes/graphics/BitmapAllocation
parent7ca2ee364e57d481a212042cb50da48f8c3db374 (diff)
downloadandroid_development-74497f33361e3e9f0cb33d3111b5198444af5794.tar.gz
android_development-74497f33361e3e9f0cb33d3111b5198444af5794.tar.bz2
android_development-74497f33361e3e9f0cb33d3111b5198444af5794.zip
First set of demo apps for DevBytes videos
Change-Id: I4ae984cf776604cede32b98db6b1eb3cb6c59c7e
Diffstat (limited to 'samples/devbytes/graphics/BitmapAllocation')
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/AndroidManifest.xml39
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/drawable-hdpi/ic_launcher.pngbin0 -> 9397 bytes
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/drawable-mdpi/ic_launcher.pngbin0 -> 5237 bytes
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/a.jpgbin0 -> 575481 bytes
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/b.jpgbin0 -> 1150868 bytes
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/c.jpgbin0 -> 939779 bytes
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/d.jpgbin0 -> 532535 bytes
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/e.jpgbin0 -> 493997 bytes
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/f.jpgbin0 -> 344799 bytes
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/drawable-xhdpi/ic_launcher.pngbin0 -> 14383 bytes
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/layout/activity_bitmap_allocation.xml38
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/values-v11/styles.xml19
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/values-v14/styles.xml19
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/values/strings.xml21
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/res/values/styles.xml19
-rw-r--r--samples/devbytes/graphics/BitmapAllocation/src/com/example/android/bitmapallocation/BitmapAllocation.java99
16 files changed, 254 insertions, 0 deletions
diff --git a/samples/devbytes/graphics/BitmapAllocation/AndroidManifest.xml b/samples/devbytes/graphics/BitmapAllocation/AndroidManifest.xml
new file mode 100644
index 000000000..5ffcb29b2
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/AndroidManifest.xml
@@ -0,0 +1,39 @@
+<!-- Copyright (C) 2013 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.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.example.android.bitmapallocation"
+ android:versionCode="1"
+ android:versionName="1.0" >
+
+ <uses-sdk
+ android:minSdkVersion="15"
+ android:targetSdkVersion="15" />
+
+ <application
+ android:icon="@drawable/ic_launcher"
+ android:label="@string/app_name"
+ android:theme="@style/AppTheme" >
+ <activity
+ android:name="com.example.android.bitmapallocation.BitmapAllocation"
+ android:label="@string/title_activity_bitmap_allocation" >
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ </application>
+
+</manifest>
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/drawable-hdpi/ic_launcher.png b/samples/devbytes/graphics/BitmapAllocation/res/drawable-hdpi/ic_launcher.png
new file mode 100644
index 000000000..96a442e5b
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/drawable-hdpi/ic_launcher.png
Binary files differ
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/drawable-mdpi/ic_launcher.png b/samples/devbytes/graphics/BitmapAllocation/res/drawable-mdpi/ic_launcher.png
new file mode 100644
index 000000000..359047dfa
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/drawable-mdpi/ic_launcher.png
Binary files differ
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/a.jpg b/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/a.jpg
new file mode 100644
index 000000000..23dc04a8e
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/a.jpg
Binary files differ
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/b.jpg b/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/b.jpg
new file mode 100644
index 000000000..b6beca25f
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/b.jpg
Binary files differ
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/c.jpg b/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/c.jpg
new file mode 100644
index 000000000..d60855497
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/c.jpg
Binary files differ
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/d.jpg b/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/d.jpg
new file mode 100644
index 000000000..f1ff9f5a5
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/d.jpg
Binary files differ
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/e.jpg b/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/e.jpg
new file mode 100644
index 000000000..580a0d17d
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/e.jpg
Binary files differ
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/f.jpg b/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/f.jpg
new file mode 100644
index 000000000..bc14d1217
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/drawable-nodpi/f.jpg
Binary files differ
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/drawable-xhdpi/ic_launcher.png b/samples/devbytes/graphics/BitmapAllocation/res/drawable-xhdpi/ic_launcher.png
new file mode 100644
index 000000000..71c6d760f
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/drawable-xhdpi/ic_launcher.png
Binary files differ
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/layout/activity_bitmap_allocation.xml b/samples/devbytes/graphics/BitmapAllocation/res/layout/activity_bitmap_allocation.xml
new file mode 100644
index 000000000..458178b53
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/layout/activity_bitmap_allocation.xml
@@ -0,0 +1,38 @@
+<!-- Copyright (C) 2013 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"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:orientation="vertical"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent" >
+
+ <CheckBox
+ android:id="@+id/checkbox"
+ android:text="@string/reuse_bitmap"
+ android:checked="false"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content" />
+ <TextView
+ android:id="@+id/loadDuration"
+ android:textSize="30sp"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content" />
+ <ImageView
+ android:id="@+id/imageview"
+ android:adjustViewBounds="true"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content" />
+
+</LinearLayout>
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/values-v11/styles.xml b/samples/devbytes/graphics/BitmapAllocation/res/values-v11/styles.xml
new file mode 100644
index 000000000..c16b77bf8
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/values-v11/styles.xml
@@ -0,0 +1,19 @@
+<!-- Copyright (C) 2013 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>
+
+ <style name="AppTheme" parent="android:Theme.Holo.Light" />
+
+</resources>
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/values-v14/styles.xml b/samples/devbytes/graphics/BitmapAllocation/res/values-v14/styles.xml
new file mode 100644
index 000000000..0f1a1ef9c
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/values-v14/styles.xml
@@ -0,0 +1,19 @@
+<!-- Copyright (C) 2013 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>
+
+ <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" />
+
+</resources>
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/values/strings.xml b/samples/devbytes/graphics/BitmapAllocation/res/values/strings.xml
new file mode 100644
index 000000000..bf598f978
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/values/strings.xml
@@ -0,0 +1,21 @@
+<!-- Copyright (C) 2013 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="app_name">BitmapAllocation</string>
+ <string name="title_activity_bitmap_allocation">BitmapAllocation</string>
+ <string name="reuse_bitmap">Reuse Bitmap</string>
+
+</resources>
diff --git a/samples/devbytes/graphics/BitmapAllocation/res/values/styles.xml b/samples/devbytes/graphics/BitmapAllocation/res/values/styles.xml
new file mode 100644
index 000000000..de01fbd2f
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/res/values/styles.xml
@@ -0,0 +1,19 @@
+<!-- Copyright (C) 2013 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>
+
+ <style name="AppTheme" parent="android:Theme.Light" />
+
+</resources>
diff --git a/samples/devbytes/graphics/BitmapAllocation/src/com/example/android/bitmapallocation/BitmapAllocation.java b/samples/devbytes/graphics/BitmapAllocation/src/com/example/android/bitmapallocation/BitmapAllocation.java
new file mode 100644
index 000000000..cc6b1e00a
--- /dev/null
+++ b/samples/devbytes/graphics/BitmapAllocation/src/com/example/android/bitmapallocation/BitmapAllocation.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2013 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.bitmapallocation;
+
+import android.app.Activity;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.CheckBox;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+/**
+ * This example shows how to speed up bitmap loading and reduce garbage collection
+ * by reusing existing bitmaps.
+ *
+ * Watch the associated video for this demo on the DevBytes channel of developer.android.com,
+ * or on YouTube at https://www.youtube.com/watch?v=rsQet4nBVi8.
+ */
+public class BitmapAllocation extends Activity {
+
+ // There are some assumptions in this demo app that don't carry over well to the real world:
+ // it assumes that all bitmaps are the same size and that loading all bitmaps as the activity
+ // starts is good enough. A real application would be take a more flexible and robust
+ // approach. But these assumptions are good enough for the purposes of this tutorial,
+ // which is about reusing existing bitmaps of the same size.
+
+ int mCurrentIndex = 0;
+ Bitmap mCurrentBitmap = null;
+ BitmapFactory.Options mBitmapOptions;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_bitmap_allocation);
+
+ final int[] imageIDs = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
+ R.drawable.e, R.drawable.f};
+
+ final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
+ final TextView durationTextview = (TextView) findViewById(R.id.loadDuration);
+ final ImageView imageview = (ImageView) findViewById(R.id.imageview);
+
+ // Create bitmap to be re-used, based on the size of one of the bitmaps
+ mBitmapOptions = new BitmapFactory.Options();
+ mBitmapOptions.inJustDecodeBounds = true;
+ BitmapFactory.decodeResource(getResources(), R.drawable.a, mBitmapOptions);
+ mCurrentBitmap = Bitmap.createBitmap(mBitmapOptions.outWidth,
+ mBitmapOptions.outHeight, Bitmap.Config.ARGB_8888);
+ mBitmapOptions.inJustDecodeBounds = false;
+ mBitmapOptions.inBitmap = mCurrentBitmap;
+ mBitmapOptions.inSampleSize = 1;
+ BitmapFactory.decodeResource(getResources(), R.drawable.a, mBitmapOptions);
+ imageview.setImageBitmap(mCurrentBitmap);
+
+ // When the user clicks on the image, load the next one in the list
+ imageview.setOnClickListener(new View.OnClickListener() {
+
+ @Override
+ public void onClick(View v) {
+ mCurrentIndex = (mCurrentIndex + 1) % imageIDs.length;
+ BitmapFactory.Options bitmapOptions = null;
+ if (checkbox.isChecked()) {
+ // Re-use the bitmap by using BitmapOptions.inBitmap
+ bitmapOptions = mBitmapOptions;
+ bitmapOptions.inBitmap = mCurrentBitmap;
+ }
+ long startTime = System.currentTimeMillis();
+ mCurrentBitmap = BitmapFactory.decodeResource(getResources(),
+ imageIDs[mCurrentIndex], bitmapOptions);
+ imageview.setImageBitmap(mCurrentBitmap);
+
+ // One way you can see the difference between reusing and not is through the
+ // timing reported here. But you can also see a huge impact in the garbage
+ // collector if you look at logcat with and without reuse. Avoiding garbage
+ // collection when possible, especially for large items like bitmaps,
+ // is always a good idea.
+ durationTextview.setText("Load took " +
+ (System.currentTimeMillis() - startTime));
+ }
+ });
+ }
+
+}