power7714

AndroidShapeNetworkImageTest

Feb 14th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.44 KB | None | 0 0
  1. import android.content.Context;
  2. import android.util.AttributeSet;
  3. import android.view.ViewGroup.LayoutParams;
  4. import android.text.TextUtils;
  5.  
  6. import com.android.volley.VolleyError;
  7. import com.android.volley.toolbox.ImageLoader;
  8. import com.android.volley.toolbox.ImageLoader.ImageContainer;
  9. import com.android.volley.toolbox.ImageLoader.ImageListener;
  10. import com.github.siyamed.shapeimageview.ShaderImageView;
  11. import com.github.siyamed.shapeimageview.shader.ShaderHelper;
  12. import com.github.siyamed.shapeimageview.shader.SvgShader;
  13.  
  14.  
  15.  
  16. /**
  17. * Created by USER on 2/13/2016.
  18. */
  19. public class ShapeNetworkImage extends ShaderImageView{
  20. private SvgShader shader;
  21. /** The URL of the network image to load */
  22. private String mUrl;
  23.  
  24. /**
  25. * Resource ID of the image to be used as a placeholder until the network image is loaded.
  26. */
  27. private int mDefaultImageId;
  28.  
  29. /**
  30. * Resource ID of the image to be used if the network response fails.
  31. */
  32. private int mErrorImageId;
  33.  
  34. /** Local copy of the ImageLoader. */
  35. private ImageLoader mImageLoader;
  36.  
  37. /** Current ImageContainer. (either in-flight or finished) */
  38. private ImageLoader.ImageContainer mImageContainer;
  39.  
  40. public ShapeNetworkImage(Context context) {
  41. super(context);
  42. }
  43.  
  44. public ShapeNetworkImage(Context context, AttributeSet attrs) {
  45. super(context, attrs);
  46. }
  47.  
  48. public ShapeNetworkImage(Context context, AttributeSet attrs, int defStyle) {
  49. super(context, attrs, defStyle);
  50. }
  51.  
  52. @Override
  53. public ShaderHelper createImageViewHelper() {
  54. shader = new SvgShader();
  55. return shader;
  56. }
  57.  
  58. public void setStrokeMiter(int strokeMiter) {
  59. if(shader != null) {
  60. shader.setStrokeMiter(strokeMiter);
  61. invalidate();
  62. }
  63. }
  64.  
  65. public void setStrokeCap(int strokeCap) {
  66. if(shader != null) {
  67. shader.setStrokeCap(strokeCap);
  68. invalidate();
  69. }
  70. }
  71.  
  72. public void setStrokeJoin(int strokeJoin) {
  73. if(shader != null) {
  74. shader.setStrokeJoin(strokeJoin);
  75. invalidate();
  76. }
  77. }
  78.  
  79. public void setBorderType(int borderType) {
  80. if(shader != null) {
  81. shader.setBorderType(borderType);
  82. invalidate();
  83. }
  84. }
  85.  
  86. public void setShapeResId(int resId) {
  87. if(shader != null) {
  88. shader.setShapeResId(getContext(), resId);
  89. invalidate();
  90. }
  91. }
  92.  
  93. public void setImageUrl(String url, ImageLoader imageLoader) {
  94. mUrl = url;
  95. mImageLoader = imageLoader;
  96. // The URL has potentially changed. See if we need to load it.
  97. loadImageIfNecessary(false);
  98. }
  99.  
  100. /**
  101. * Gets the URL of the image that should be loaded into this view, or null if no URL has been set.
  102. * The image may or may not already be downloaded and set into the view.
  103. * @return the URL of the image to be set into the view, or null.
  104. */
  105. public String getImageURL()
  106. {
  107. return mUrl;
  108. }
  109.  
  110. /**
  111. * Sets the default image resource ID to be used for this view until the attempt to load it
  112. * completes.
  113. */
  114. public void setDefaultImageResId(int defaultImage) {
  115. mDefaultImageId = defaultImage;
  116. }
  117.  
  118. /**
  119. * Sets the error image resource ID to be used for this view in the event that the image
  120. * requested fails to load.
  121. */
  122. public void setErrorImageResId(int errorImage) {
  123. mErrorImageId = errorImage;
  124. }
  125.  
  126. /**
  127. * Loads the image for the view if it isn't already loaded.
  128. * @param isInLayoutPass True if this was invoked from a layout pass, false otherwise.
  129. */
  130. void loadImageIfNecessary(final boolean isInLayoutPass) {
  131. int width = getWidth();
  132. int height = getHeight();
  133. ScaleType scaleType = getScaleType();
  134.  
  135. boolean wrapWidth = false, wrapHeight = false;
  136. if (getLayoutParams() != null) {
  137. wrapWidth = getLayoutParams().width == LayoutParams.WRAP_CONTENT;
  138. wrapHeight = getLayoutParams().height == LayoutParams.WRAP_CONTENT;
  139. }
  140.  
  141. // if the view's bounds aren't known yet, and this is not a wrap-content/wrap-content
  142. // view, hold off on loading the image.
  143. boolean isFullyWrapContent = wrapWidth && wrapHeight;
  144. if (width == 0 && height == 0 && !isFullyWrapContent) {
  145. return;
  146. }
  147.  
  148. // if the URL to be loaded in this view is empty, cancel any old requests and clear the
  149. // currently loaded image.
  150. if (TextUtils.isEmpty(mUrl)) {
  151. if (mImageContainer != null) {
  152. mImageContainer.cancelRequest();
  153. mImageContainer = null;
  154. }
  155. setDefaultImageOrNull();
  156. return;
  157. }
  158.  
  159. // if there was an old request in this view, check if it needs to be canceled.
  160. if (mImageContainer != null && mImageContainer.getRequestUrl() != null) {
  161. if (mImageContainer.getRequestUrl().equals(mUrl)) {
  162. // if the request is from the same URL, return.
  163. return;
  164. } else {
  165. // if there is a pre-existing request, cancel it if it's fetching a different URL.
  166. mImageContainer.cancelRequest();
  167. setDefaultImageOrNull();
  168. }
  169. }
  170.  
  171. // Calculate the max image width / height to use while ignoring WRAP_CONTENT dimens.
  172. int maxWidth = wrapWidth ? 0 : width;
  173. int maxHeight = wrapHeight ? 0 : height;
  174.  
  175. // The pre-existing content of this view didn't match the current URL. Load the new image
  176. // from the network.
  177. ImageLoader.ImageContainer newContainer = mImageLoader.get(mUrl,
  178. new ImageListener() {
  179. @Override
  180. public void onErrorResponse(VolleyError error) {
  181. if (mErrorImageId != 0) {
  182. setImageResource(mErrorImageId);
  183. }
  184. }
  185.  
  186. @Override
  187. public void onResponse(final ImageContainer response, boolean isImmediate) {
  188. // If this was an immediate response that was delivered inside of a layout
  189. // pass do not set the image immediately as it will trigger a requestLayout
  190. // inside of a layout. Instead, defer setting the image by posting back to
  191. // the main thread.
  192. if (isImmediate && isInLayoutPass) {
  193. post(new Runnable() {
  194. @Override
  195. public void run() {
  196. onResponse(response, false);
  197. }
  198. });
  199. return;
  200. }
  201.  
  202. if (response.getBitmap() != null) {
  203. setImageBitmap(response.getBitmap());
  204. } else if (mDefaultImageId != 0) {
  205. setImageResource(mDefaultImageId);
  206. }
  207. }
  208. }, maxWidth, maxHeight, scaleType);
  209.  
  210. // update the ImageContainer to be the new bitmap container.
  211. mImageContainer = newContainer;
  212. }
  213.  
  214. private void setDefaultImageOrNull() {
  215. if(mDefaultImageId != 0) {
  216. setImageResource(mDefaultImageId);
  217. }
  218. else {
  219. setImageBitmap(null);
  220. }
  221. }
  222.  
  223. @Override
  224. protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  225. super.onLayout(changed, left, top, right, bottom);
  226. loadImageIfNecessary(true);
  227. }
  228.  
  229. @Override
  230. protected void onDetachedFromWindow() {
  231. if (mImageContainer != null) {
  232. // If the view was bound to an image request, cancel it and clear
  233. // out the image from the view.
  234. mImageContainer.cancelRequest();
  235. setImageBitmap(null);
  236. // also clear out the container so we can reload the image if necessary.
  237. mImageContainer = null;
  238. }
  239. super.onDetachedFromWindow();
  240. }
  241.  
  242. @Override
  243. protected void drawableStateChanged() {
  244. super.drawableStateChanged();
  245. invalidate();
  246. }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment