diff options
author | Sam Blitzstein <sblitz@google.com> | 2013-10-16 16:12:11 -0700 |
---|---|---|
committer | Sam Blitzstein <sblitz@google.com> | 2013-10-16 16:49:27 -0700 |
commit | d2e1718fa8e1199b03dc0d069711b24e946aa858 (patch) | |
tree | 16168a56eb8b244c23b940b395e5071aad4d488e /sample/src | |
parent | 40662f4b39e795d9c64502b13036e7c37fa2d373 (diff) | |
download | bitmap-d2e1718fa8e1199b03dc0d069711b24e946aa858.tar.gz |
Add sample app to bitmap library.
Change-Id: Iec98361f576289447a274f51694d46974f1534ac
Diffstat (limited to 'sample/src')
3 files changed, 212 insertions, 0 deletions
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; + } + } +} |