summaryrefslogtreecommitdiff
path: root/sample
diff options
context:
space:
mode:
authorSam Blitzstein <sblitz@google.com>2013-10-16 16:12:11 -0700
committerSam Blitzstein <sblitz@google.com>2013-10-16 16:49:27 -0700
commitd2e1718fa8e1199b03dc0d069711b24e946aa858 (patch)
tree16168a56eb8b244c23b940b395e5071aad4d488e /sample
parent40662f4b39e795d9c64502b13036e7c37fa2d373 (diff)
downloadbitmap-d2e1718fa8e1199b03dc0d069711b24e946aa858.tar.gz
Add sample app to bitmap library.
Change-Id: Iec98361f576289447a274f51694d46974f1534ac
Diffstat (limited to 'sample')
-rw-r--r--sample/Android.mk28
-rw-r--r--sample/AndroidManifest.xml43
-rw-r--r--sample/res/drawable-mdpi/ic_launcher.pngbin0 -> 5237 bytes
-rw-r--r--sample/res/layout/activity_main.xml16
-rw-r--r--sample/res/values/dimens.xml6
-rw-r--r--sample/res/values/strings.xml6
-rw-r--r--sample/res/values/styles.xml20
-rw-r--r--sample/src/com/example/bitmapsample/BitmapRequestKeyImpl.java82
-rw-r--r--sample/src/com/example/bitmapsample/BitmapView.java44
-rw-r--r--sample/src/com/example/bitmapsample/MainActivity.java86
10 files changed, 331 insertions, 0 deletions
diff --git a/sample/Android.mk b/sample/Android.mk
new file mode 100644
index 0000000..a861182
--- /dev/null
+++ b/sample/Android.mk
@@ -0,0 +1,28 @@
+# 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.
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := com-example-bitmapcache
+
+LOCAL_SDK_VERSION := 16
+
+LOCAL_SRC_FILES := \
+ $(call all-java-files-under, src) \
+ $(call all-logtags-files-under, src)
+
+LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
+
+include $(BUILD_STATIC_JAVA_LIBRARY)
diff --git a/sample/AndroidManifest.xml b/sample/AndroidManifest.xml
new file mode 100644
index 0000000..178b666
--- /dev/null
+++ b/sample/AndroidManifest.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.bitmapcachesample"
+ android:versionCode="1"
+ android:versionName="1.0" >
+
+ <uses-sdk
+ android:minSdkVersion="15"
+ android:targetSdkVersion="16" />
+
+ <application
+ android:allowBackup="true"
+ android:icon="@drawable/ic_launcher"
+ android:label="@string/app_name"
+ android:theme="@style/AppTheme" android:debuggable="true">
+ <activity
+ android:name="com.example.bitmapsample.MainActivity"
+ android:label="@string/app_name" >
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ </application>
+
+ <uses-permission android:name="android.permission.INTERNET" />
+</manifest>
diff --git a/sample/res/drawable-mdpi/ic_launcher.png b/sample/res/drawable-mdpi/ic_launcher.png
new file mode 100644
index 0000000..359047d
--- /dev/null
+++ b/sample/res/drawable-mdpi/ic_launcher.png
Binary files differ
diff --git a/sample/res/layout/activity_main.xml b/sample/res/layout/activity_main.xml
new file mode 100644
index 0000000..2026d9a
--- /dev/null
+++ b/sample/res/layout/activity_main.xml
@@ -0,0 +1,16 @@
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:tools="http://schemas.android.com/tools"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:paddingBottom="@dimen/activity_vertical_margin"
+ android:paddingLeft="@dimen/activity_horizontal_margin"
+ android:paddingRight="@dimen/activity_horizontal_margin"
+ android:paddingTop="@dimen/activity_vertical_margin"
+ tools:context=".MainActivity" >
+
+ <ListView
+ android:id="@+id/list"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent" />
+
+</RelativeLayout>
diff --git a/sample/res/values/dimens.xml b/sample/res/values/dimens.xml
new file mode 100644
index 0000000..0bd0996
--- /dev/null
+++ b/sample/res/values/dimens.xml
@@ -0,0 +1,6 @@
+<resources>
+
+ <!-- Default screen margins, per the Android Design guidelines. -->
+ <dimen name="activity_horizontal_margin">16dp</dimen>
+ <dimen name="activity_vertical_margin">16dp</dimen>
+</resources>
diff --git a/sample/res/values/strings.xml b/sample/res/values/strings.xml
new file mode 100644
index 0000000..1f56393
--- /dev/null
+++ b/sample/res/values/strings.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+ <string name="app_name">BitmapTest</string>
+
+</resources>
diff --git a/sample/res/values/styles.xml b/sample/res/values/styles.xml
new file mode 100644
index 0000000..6ce89c7
--- /dev/null
+++ b/sample/res/values/styles.xml
@@ -0,0 +1,20 @@
+<resources>
+
+ <!--
+ Base application theme, dependent on API level. This theme is replaced
+ by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
+ -->
+ <style name="AppBaseTheme" parent="android:Theme.Light">
+ <!--
+ Theme customizations available in newer API levels can go in
+ res/values-vXX/styles.xml, while customizations related to
+ backward-compatibility can go here.
+ -->
+ </style>
+
+ <!-- Application theme. -->
+ <style name="AppTheme" parent="AppBaseTheme">
+ <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+ </style>
+
+</resources>
diff --git a/sample/src/com/example/bitmapsample/BitmapRequestKeyImpl.java b/sample/src/com/example/bitmapsample/BitmapRequestKeyImpl.java
new file mode 100644
index 0000000..a4705c5
--- /dev/null
+++ b/sample/src/com/example/bitmapsample/BitmapRequestKeyImpl.java
@@ -0,0 +1,82 @@
+/*
+ * 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.bitmapsample;
+
+import android.content.res.AssetFileDescriptor;
+
+import com.android.bitmap.RequestKey;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class BitmapRequestKeyImpl implements RequestKey {
+ public final String mUriString;
+ public final URL mUrl;
+
+ public BitmapRequestKeyImpl(String uriString) {
+ mUriString = uriString;
+ URL url = null;
+ try {
+ url = new URL(uriString);
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ }
+ mUrl = url;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o == null || !(o instanceof BitmapRequestKeyImpl)) {
+ return false;
+ }
+ final BitmapRequestKeyImpl other = (BitmapRequestKeyImpl) o;
+ return mUriString.equals(other.mUriString);
+ }
+
+ @Override
+ public int hashCode() {
+ int hash = 17;
+ hash += 31 * hash + mUriString.hashCode();
+ return hash;
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder sb = new StringBuilder("[");
+ sb.append(mUriString);
+ sb.append("]");
+ return sb.toString();
+ }
+
+ @Override
+ public AssetFileDescriptor createFd() throws IOException {
+ return null;
+ }
+
+ @Override
+ public InputStream createInputStream() throws IOException {
+ return mUrl.openStream();
+ }
+
+ @Override
+ public boolean hasOrientationExif() throws IOException {
+ return false;
+ }
+
+} \ No newline at end of file
diff --git a/sample/src/com/example/bitmapsample/BitmapView.java b/sample/src/com/example/bitmapsample/BitmapView.java
new file mode 100644
index 0000000..5ca7dcd
--- /dev/null
+++ b/sample/src/com/example/bitmapsample/BitmapView.java
@@ -0,0 +1,44 @@
+package com.example.bitmapsample;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.View;
+
+import com.android.bitmap.BitmapCache;
+import com.android.bitmap.drawable.BasicBitmapDrawable;
+
+public class BitmapView extends View {
+ private BasicBitmapDrawable mBitmapDrawable;
+ private float mDensity;
+
+ public BitmapView(Context c) {
+ this(c, null);
+ }
+
+ public BitmapView(Context c, AttributeSet attrs) {
+ super(c, attrs);
+ mDensity = getResources().getDisplayMetrics().density;
+ }
+
+ @Override
+ protected int getSuggestedMinimumHeight() {
+ return (int) (100 * mDensity);
+ }
+
+ @Override
+ protected void onSizeChanged(final int w, final int h, int oldw, int oldh) {
+ mBitmapDrawable.setDecodeDimensions(w, h);
+ }
+
+ public void setImage(String uriString) {
+ if (mBitmapDrawable != null) {
+ mBitmapDrawable.bind(new BitmapRequestKeyImpl(uriString));
+ }
+ }
+
+ public void initialize(BitmapCache cache) {
+ mBitmapDrawable = new BasicBitmapDrawable(getResources(), cache);
+ setBackground(mBitmapDrawable);
+ }
+
+} \ No newline at end of file
diff --git a/sample/src/com/example/bitmapsample/MainActivity.java b/sample/src/com/example/bitmapsample/MainActivity.java
new file mode 100644
index 0000000..6b612d4
--- /dev/null
+++ b/sample/src/com/example/bitmapsample/MainActivity.java
@@ -0,0 +1,86 @@
+package com.example.bitmapsample;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.ListView;
+
+import com.android.bitmap.BitmapCache;
+import com.android.bitmap.UnrefedBitmapCache;
+import com.android.bitmap.util.Trace;
+import com.example.bitmapcachesample.R;
+
+public class MainActivity extends Activity {
+ private ListView mListView;
+
+ private static final int TARGET_CACHE_SIZE_BYTES = 5 * 1024 * 1024;
+ private final BitmapCache mCache = new UnrefedBitmapCache(TARGET_CACHE_SIZE_BYTES, 0.1f, 0);
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_main);
+ Trace.init();
+ mListView = (ListView) findViewById(R.id.list);
+ mListView.setAdapter(new MyAdapter());
+ }
+
+ private class MyAdapter extends BaseAdapter {
+
+ private final String[] mItems;
+
+ private final String[] ITEMS = new String[]{
+ "https://www.google.com/images/srpr/logo4w.png",
+ "http://www.google.com/logos/2012/celibidache12-hp.jpg",
+ "http://www.google.com/logos/2012/clara_schuman-2012-hp.jpg",
+ "http://www.google.com/logos/2011/royalwedding11-hp.png",
+ "http://www.google.com/logos/2012/vets_day-12-hp.jpg",
+ "http://www.google.com/logos/2011/firstmaninspace11-hp-js.jpg",
+ "http://www.google.com/logos/2011/nat-sov-and-childrens-turkey-hp.png",
+ "http://www.google.com/logos/2012/First_Day_Of_School_Isreal-2012-hp.jpg",
+ "http://www.google.com/logos/2012/celibidache12-hp.jpg",
+ "http://www.google.com/logos/2012/korea12-hp.png"
+ };
+
+ private static final int COPIES = 50;
+
+ public MyAdapter() {
+ mItems = new String[ITEMS.length * COPIES];
+ for (int i = 0; i < COPIES; i++) {
+ for (int j = 0; j < ITEMS.length; j++) {
+ mItems[i * ITEMS.length + j] = ITEMS[j];
+ }
+ }
+ }
+
+ @Override
+ public int getCount() {
+ return mItems.length;
+ }
+
+ @Override
+ public Object getItem(int position) {
+ return mItems[position];
+ }
+
+ @Override
+ public long getItemId(int position) {
+ return position;
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ BitmapView v;
+ if (convertView != null) {
+ v = (BitmapView) convertView;
+ } else {
+ v = new BitmapView(MainActivity.this);
+ v.initialize(mCache);
+ }
+ v.setImage(mItems[position]);
+ return v;
+ }
+ }
+}