Skip to content
Browse files

Adding gallery shouldDelay support.

  • Loading branch information...
1 parent 61871bf commit 3e46a4a459ae6644e731109c828e6618561ed764 @androidquery committed Apr 1, 2012
View
1 .classpath
@@ -7,5 +7,6 @@
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="lib" path="auth-lib/signpost-commonshttp4-1.2.1.1.jar"/>
<classpathentry kind="lib" path="auth-lib/signpost-core-1.2.1.1.jar"/>
+ <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
View
1 demo/AndroidManifest.xml
@@ -45,6 +45,7 @@
<activity android:name=".test.image.ImageLoadingList4Activity" android:theme="@style/android:Theme.Light"></activity>
<activity android:name=".test.image.ImageLoadingListOptionsActivity" android:theme="@style/android:Theme.Light"></activity>
<activity android:name=".test.image.ImageLoadingGridActivity" android:theme="@style/android:Theme.Light"></activity>
+ <activity android:name=".test.image.ImageLoadingGalleryActivity" android:theme="@style/android:Theme.Light"></activity>
<activity android:name=".test.image.ImageLoadingAspectRatioActivity" android:theme="@style/android:Theme.Light"></activity>
<activity android:name=".test.image.ImageZoomActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:configChanges="orientation|keyboardHidden" ></activity>
<activity android:name=".test.async.AjaxAuthActivity" android:theme="@style/android:Theme.Light"></activity>
View
36 demo/res/layout/gallery_item.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="100dip"
+ android:layout_height="75dip"
+ >
+
+ <ProgressBar
+ android:layout_width="15dip"
+ android:layout_height="15dip"
+ android:id="@+id/progress"
+ android:layout_centerInParent="true"
+ />
+
+
+ <ImageView
+ android:id="@+id/tb"
+ android:layout_width="100dip"
+ android:layout_height="75dip"
+ style="@style/GalleryItem"
+ />
+
+ <TextView
+ android:id="@+id/text"
+ android:layout_width="100dip"
+ android:layout_height="75dip"
+ android:padding="15dip"
+ android:text="Dummy TextDummy TextDummy TextDummy TextDummy Text"
+ android:gravity="center"
+ android:textSize="8sp"
+ android:maxLines="4"
+ android:textColor="#FFFFFFFF"
+ />
+
+
+</RelativeLayout>
View
67 demo/res/layout/image_gallery_activity.xml
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<ScrollView
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+>
+
+ <LinearLayout
+ android:orientation="vertical"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ >
+
+ <TextView
+ android:id="@+id/name"
+ android:layout_margin="10dip"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:text="Name"
+ android:textStyle="bold"
+ android:textSize="14sp"
+ />
+
+
+ <HorizontalScrollView
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:layout_margin="10dip"
+ >
+
+ <TextView
+ android:id="@+id/code"
+ android:padding="5dip"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Code Here"
+ android:scrollbars="horizontal|vertical"
+ android:background="#eeeeee"
+ android:textColor="#000000"
+ />
+
+
+ </HorizontalScrollView>
+
+
+ <ProgressBar
+ android:id="@+id/progress"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center_horizontal"
+ android:visibility="gone"
+ />
+
+
+ <Gallery
+ android:id="@+id/gallery"
+ android:layout_width="fill_parent"
+ android:layout_height="200dip"
+ />
+
+
+
+ </LinearLayout>
+
+
+</ScrollView>
View
5 demo/res/values/arrays.xml
@@ -103,6 +103,8 @@
<item>Grid Items</item>
+ <item>Gallery Items</item>
+
<item>Google Chart</item>
<item>Custom Callback</item>
@@ -153,6 +155,7 @@
<item>com.androidquery.test.image.ImageLoadingListOptionsActivity:image_options</item>
<item>com.androidquery.test.image.ImageLoadingGridActivity:image_grid</item>
+ <item>com.androidquery.test.image.ImageLoadingGalleryActivity:image_gallery:new</item>
<item>com.androidquery.test.image.ImageLoadingActivity:image_chart:new</item>
@@ -265,6 +268,7 @@
<item>image_options</item>
<item>image_grid</item>
+ <item>image_gallery</item>
<item>image_chart</item>
@@ -365,6 +369,7 @@
<item>List Item Image Options Demo</item>
<item>Loading Image in Grid View with Aspect Ratio</item>
+ <item>Loading Image in Gallery View with Delay Load</item>
<item>Loading Google Chart Images with 2000+ length url</item>
View
8 demo/res/values/styles.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+ <style name="GalleryItem">
+ <item name="android:background">?android:attr/galleryItemBackground</item>
+ </style>
+
+</resources>
View
276 demo/src/com/androidquery/test/image/ImageLoadingGalleryActivity.java
@@ -0,0 +1,276 @@
+package com.androidquery.test.image;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import android.graphics.Bitmap;
+import android.os.Bundle;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.Adapter;
+import android.widget.AdapterView;
+import android.widget.AdapterView.OnItemSelectedListener;
+import android.widget.ArrayAdapter;
+import android.widget.Gallery;
+import android.widget.ListAdapter;
+import android.widget.SlidingDrawer;
+
+import com.androidquery.AQuery;
+import com.androidquery.R;
+import com.androidquery.callback.AjaxStatus;
+import com.androidquery.callback.BitmapAjaxCallback;
+import com.androidquery.test.RunSourceActivity;
+import com.androidquery.test.image.ImageLoadingList4Activity.Photo;
+import com.androidquery.util.AQUtility;
+import com.androidquery.util.XmlDom;
+
+public class ImageLoadingGalleryActivity extends RunSourceActivity {
+
+ protected AQuery listAq;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+
+ super.onCreate(savedInstanceState);
+
+ work();
+ }
+
+ protected int getContainer(){
+ return R.layout.image_gallery_activity;
+ }
+
+
+ public void work(){
+
+
+ AQUtility.cleanCacheAsync(this, 0, 0);
+ BitmapAjaxCallback.clearCache();
+
+ String url = "https://picasaweb.google.com/data/feed/base/featured?max-results=48";
+ aq.progress(R.id.progress).ajax(url, XmlDom.class, this, "renderPhotos");
+
+
+
+
+ }
+
+ private List<Photo> convertAll(XmlDom xml){
+
+ List<XmlDom> entries = xml.children("entry");
+
+ List<Photo> result = new ArrayList<Photo>();
+
+ for(XmlDom entry: entries){
+ result.add(convert(entry));
+ }
+
+ return result;
+ }
+
+ private Photo convert(XmlDom xml){
+
+ String url = xml.child("content").attr("src");
+ String title = xml.child("title").text();
+ String author = xml.child("author").text("name");
+
+ String tb = url;
+ List<XmlDom> tbs = xml.tags("media:thumbnail");
+
+ if(tbs.size() > 0){
+ tb = tbs.get(0).attr("url");
+ //tb = tbs.get(tbs.size() - 1).attr("url");
+ }
+
+ tb = tb.replaceAll("https:", "http:");
+
+ Photo photo = new Photo();
+ photo.url = url;
+ photo.tb = tb;
+ photo.title = title;
+ photo.author = author;
+
+ return photo;
+ }
+
+ private boolean init = false;
+ private int selected = 0;
+
+ private boolean shouldDelayG(int position, View convertView, ViewGroup parent, String url){
+
+ if(url == null) return false;
+
+ boolean hit = BitmapAjaxCallback.getMemoryCached(url, 0) != null;
+ if(hit){
+ return false;
+ }
+
+ Gallery gallery = (Gallery) parent;
+
+ if(!init){
+
+ gallery.setCallbackDuringFling(false);
+
+ gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
+
+ @Override
+ public void onItemSelected(AdapterView<?> adapterView, View convertView, int pos, long id) {
+
+ AQUtility.debug("on select", pos);
+
+ if(selected != pos){
+
+ Adapter adapter = adapterView.getAdapter();
+
+ selected = pos;
+
+ int count = adapterView.getChildCount();
+
+ AQUtility.debug("redrawing", count);
+
+ int first = adapterView.getFirstVisiblePosition();
+
+ for(int i = 0; i < count; i++){
+ View v = adapterView.getChildAt(i);
+
+ int drawPos = first + i;
+
+ Integer lastDrawn = (Integer) v.getTag(AQuery.TAG_LAYOUT);
+
+ if(lastDrawn != null && lastDrawn.intValue() == drawPos){
+ AQUtility.debug("skip", drawPos);
+ }else{
+ AQUtility.debug("redraw", drawPos);
+ adapter.getView(drawPos, v, adapterView);
+ }
+ }
+
+ /*
+ int count = view.getChildCount();
+
+ ListAdapter la = view.getAdapter();
+
+ for(int i = 0; i < count; i++) {
+ View convertView = (View) view.getChildAt(i);
+ if(convertView.getTag(AQuery.TAG_SCROLL_LISTENER) != null){
+ la.getView(first + i, convertView, view);
+ convertView.setTag(AQuery.TAG_SCROLL_LISTENER, null);
+ }
+ }
+ */
+
+
+ //adapter.getView(pos, convertView, adapterView);
+
+ }
+
+
+ }
+
+ @Override
+ public void onNothingSelected(AdapterView<?> arg0) {
+ // TODO Auto-generated method stub
+
+ AQUtility.debug("on nothing");
+ }
+
+
+ });
+ init = true;
+ }
+
+ int first = gallery.getFirstVisiblePosition();
+ int last = gallery.getLastVisiblePosition();
+
+ int diff = last - first;
+ int delta = (diff / 2) + 1;
+
+ int from = selected - delta;
+ int to = selected + delta;
+
+ if(from < 0){
+ //shift window back to positive region
+ to = to - from;
+ from = 0;
+ }
+
+ if((position >= from && position <= to)){
+
+ AQUtility.debug("yes", position + ":" + from + "." + to);
+ convertView.setTag(AQuery.TAG_LAYOUT, position);
+
+ return false;
+ }
+
+ AQUtility.debug("no", position + ":" + from + "." + to);
+ convertView.setTag(AQuery.TAG_LAYOUT, null);
+ return true;
+
+ }
+
+
+ public void renderPhotos(String url, XmlDom xml, AjaxStatus status) {
+
+ if(xml == null) return;
+
+ List<Photo> entries = convertAll(xml);
+
+ listAq = new AQuery(this);
+
+ ArrayAdapter<Photo> aa = new ArrayAdapter<Photo>(this, R.layout.gallery_item, entries){
+
+ public View getView(int position, View convertView, ViewGroup parent) {
+
+ if(convertView == null){
+ convertView = getLayoutInflater().inflate(R.layout.gallery_item, parent, false);
+ }
+
+ Photo photo = getItem(position);
+
+ AQuery aq = listAq.recycle(convertView);
+
+ aq.id(R.id.name).text(photo.title);
+
+ String tbUrl = photo.tb;
+
+
+
+ if(!shouldDelayG(position, convertView, parent, tbUrl)){
+ aq.id(R.id.tb).image(tbUrl);
+ aq.id(R.id.text).text(photo.title).gone();
+ }else{
+ aq.id(R.id.tb).clear();
+ aq.id(R.id.text).text(photo.title).visible();
+ }
+
+
+ return convertView;
+
+ }
+
+
+ };
+
+ aq.id(R.id.gallery).adapter(aa);
+
+
+ }
+
+ @Override
+ protected void runSource(){
+
+ //AQUtility.invokeHandler(this, type, false, null);
+ }
+
+ class Photo{
+
+ String tb;
+ String url;
+ String title;
+ String author;
+ }
+
+}
View
3 demo/src/com/androidquery/test/image/ImageLoadingListOptionsActivity.java
@@ -8,6 +8,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
+import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
@@ -54,7 +55,7 @@ private void ajax(){
AQUtility.cleanCacheAsync(this, 0, 0);
BitmapAjaxCallback.clearCache();
- aq.id(R.id.list).adapter(null);
+ aq.id(R.id.list).adapter((Adapter) null);
String url = "https://picasaweb.google.com/data/feed/base/featured?max-results=48";
aq.progress(R.id.progress).ajax(url, XmlDom.class, this, "renderPhotos");

0 comments on commit 3e46a4a

Please sign in to comment.
Something went wrong with that request. Please try again.