/web-image-view/src/main/java/com/goal98/android/WebGalleryAdapter.java

https://github.com/shangrz/FlipDroid · Java · 179 lines · 95 code · 33 blank · 51 comment · 5 complexity · 3beda9199e9da4d3fc34aa9fd1718e7b MD5 · raw file

  1. /* Copyright (c) 2009 Matthias Käppler
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package com.goal98.android;
  16. import android.content.Context;
  17. import android.graphics.drawable.Drawable;
  18. import android.view.Gravity;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.widget.BaseAdapter;
  22. import android.widget.FrameLayout;
  23. import android.widget.Gallery.LayoutParams;
  24. import java.util.List;
  25. /**
  26. * Can be used as an adapter for an Android {@link android.widget.Gallery} view. This adapter loads the images to
  27. * be shown from the web.
  28. *
  29. * @author Matthias Kaeppler
  30. */
  31. public class WebGalleryAdapter extends BaseAdapter {
  32. public static final int NO_DRAWABLE = -1;
  33. private List<String> imageUrls;
  34. private Context context;
  35. private Drawable progressDrawable, errorDrawable;
  36. public WebGalleryAdapter(Context context) {
  37. initialize(context, null, null, null);
  38. }
  39. /**
  40. * @param context
  41. * the current context
  42. * @param imageUrls
  43. * the set of it.tika.mongodb.image URLs which are to be loaded and displayed
  44. */
  45. public WebGalleryAdapter(Context context, List<String> imageUrls) {
  46. initialize(context, imageUrls, null, null);
  47. }
  48. /**
  49. * @param context
  50. * the current context
  51. * @param imageUrls
  52. * the set of it.tika.mongodb.image URLs which are to be loaded and displayed
  53. * @param progressDrawableResId
  54. * the resource ID of the drawable that will be used for rendering progress
  55. */
  56. public WebGalleryAdapter(Context context, List<String> imageUrls, int progressDrawableResId) {
  57. initialize(context, imageUrls, context.getResources().getDrawable(progressDrawableResId),
  58. null);
  59. }
  60. /**
  61. * @param context
  62. * the current context
  63. * @param imageUrls
  64. * the set of it.tika.mongodb.image URLs which are to be loaded and displayed
  65. * @param progressDrawableResId
  66. * the resource ID of the drawable that will be used for rendering progress
  67. * @param errorDrawableId
  68. * the resource ID of the drawable that will be used if a read error occurs
  69. */
  70. public WebGalleryAdapter(Context context, List<String> imageUrls, int progressDrawableResId,
  71. int errorDrawableId) {
  72. initialize(context, imageUrls, progressDrawableResId == NO_DRAWABLE ? null : context
  73. .getResources().getDrawable(progressDrawableResId),
  74. errorDrawableId == NO_DRAWABLE ? null : context.getResources().getDrawable(
  75. errorDrawableId));
  76. }
  77. private void initialize(Context context, List<String> imageUrls, Drawable progressDrawable,
  78. Drawable errorDrawable) {
  79. this.imageUrls = imageUrls;
  80. this.context = context;
  81. this.progressDrawable = progressDrawable;
  82. this.errorDrawable = errorDrawable;
  83. }
  84. public int getCount() {
  85. return imageUrls.size();
  86. }
  87. public Object getItem(int position) {
  88. return imageUrls.get(position);
  89. }
  90. public long getItemId(int position) {
  91. return position;
  92. }
  93. public void setImageUrls(List<String> imageUrls) {
  94. this.imageUrls = imageUrls;
  95. }
  96. public List<String> getImageUrls() {
  97. return imageUrls;
  98. }
  99. public void setProgressDrawable(Drawable progressDrawable) {
  100. this.progressDrawable = progressDrawable;
  101. }
  102. public Drawable getProgressDrawable() {
  103. return progressDrawable;
  104. }
  105. // TODO: both convertView and ViewHolder are pointless at the moment, since there's a framework
  106. // bug which causes views to not be cached in a Gallery widget:
  107. // http://code.google.com/p/android/issues/detail?id=3376
  108. public View getView(int position, View convertView, ViewGroup parent) {
  109. String imageUrl = (String) getItem(position);
  110. ViewHolder viewHolder = null;
  111. WebImageView webImageView = null;
  112. if (convertView == null) {
  113. // create the it.tika.mongodb.image view
  114. webImageView = new WebImageView(context, null, progressDrawable,
  115. errorDrawable, false);
  116. FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
  117. LayoutParams.WRAP_CONTENT);
  118. lp.gravity = Gravity.CENTER;
  119. webImageView.setLayoutParams(lp);
  120. // create the container layout for the it.tika.mongodb.image view
  121. FrameLayout container = new FrameLayout(context);
  122. container.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
  123. LayoutParams.FILL_PARENT));
  124. container.addView(webImageView, 0);
  125. convertView = container;
  126. viewHolder = new ViewHolder();
  127. viewHolder.webImageView = webImageView;
  128. convertView.setTag(viewHolder);
  129. } else {
  130. viewHolder = (ViewHolder) convertView.getTag();
  131. webImageView = viewHolder.webImageView;
  132. }
  133. // calling reset is important to prevent old images from displaying in a recycled view.
  134. webImageView.reset();
  135. webImageView.setImageUrl(imageUrl);
  136. webImageView.loadImage();
  137. onGetView(position, convertView, parent);
  138. return convertView;
  139. }
  140. protected void onGetView(int position, View convertView, ViewGroup parent) {
  141. // for extension
  142. }
  143. private static final class ViewHolder {
  144. private WebImageView webImageView;
  145. }
  146. }