PageRenderTime 3998ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/mgimlibs/src/com/squareup/picasso/Request.java

https://gitlab.com/lisit1003/TTAndroidClient
Java | 347 lines | 221 code | 31 blank | 95 comment | 37 complexity | f7d4e7a293f8563c6350dc06a5e4de6a MD5 | raw file
  1. /*
  2. * Copyright (C) 2013 Square, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.squareup.picasso;
  17. import android.graphics.Bitmap;
  18. import android.net.Uri;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import static java.util.Collections.unmodifiableList;
  22. /**
  23. * Immutable data about an image and the transformations that will be applied to
  24. * it.
  25. */
  26. public final class Request {
  27. /**
  28. * The image URI.
  29. * <p>
  30. * This is mutually exclusive with {@link #resourceId}.
  31. */
  32. public final Uri uri;
  33. /**
  34. * The image resource ID.
  35. * <p>
  36. * This is mutually exclusive with {@link #uri}.
  37. */
  38. public final int resourceId;
  39. /**
  40. * List of custom transformations to be applied after the built-in
  41. * transformations.
  42. */
  43. public final List<Transformation> transformations;
  44. /** Target image width for resizing. */
  45. public final int targetWidth;
  46. /** Target image height for resizing. */
  47. public final int targetHeight;
  48. /**
  49. * True if the final image should use the 'centerCrop' scale technique.
  50. * <p>
  51. * This is mutually exclusive with {@link #centerInside}.
  52. */
  53. public final boolean centerCrop;
  54. /**
  55. * True if the final image should use the 'centerInside' scale technique.
  56. * <p>
  57. * This is mutually exclusive with {@link #centerCrop}.
  58. */
  59. public final boolean centerInside;
  60. /** Amount to rotate the image in degrees. */
  61. public final float rotationDegrees;
  62. /** Rotation pivot on the X axis. */
  63. public final float rotationPivotX;
  64. /** Rotation pivot on the Y axis. */
  65. public final float rotationPivotY;
  66. /**
  67. * Whether or not {@link #rotationPivotX} and {@link #rotationPivotY} are
  68. * set.
  69. */
  70. public final boolean hasRotationPivot;
  71. /** Target image config for decoding. */
  72. public final Bitmap.Config config;
  73. private Request(Uri uri, int resourceId,
  74. List<Transformation> transformations, int targetWidth,
  75. int targetHeight, boolean centerCrop, boolean centerInside,
  76. float rotationDegrees, float rotationPivotX, float rotationPivotY,
  77. boolean hasRotationPivot, Bitmap.Config config) {
  78. this.uri = uri;
  79. this.resourceId = resourceId;
  80. if (transformations == null) {
  81. this.transformations = null;
  82. } else {
  83. this.transformations = unmodifiableList(transformations);
  84. }
  85. this.targetWidth = targetWidth;
  86. this.targetHeight = targetHeight;
  87. this.centerCrop = centerCrop;
  88. this.centerInside = centerInside;
  89. this.rotationDegrees = rotationDegrees;
  90. this.rotationPivotX = rotationPivotX;
  91. this.rotationPivotY = rotationPivotY;
  92. this.hasRotationPivot = hasRotationPivot;
  93. this.config = config;
  94. }
  95. String getName() {
  96. if (uri != null) {
  97. return uri.getPath();
  98. }
  99. return Integer.toHexString(resourceId);
  100. }
  101. public boolean hasSize() {
  102. return targetWidth != 0;
  103. }
  104. boolean needsTransformation() {
  105. return needsMatrixTransform() || hasCustomTransformations();
  106. }
  107. boolean needsMatrixTransform() {
  108. return targetWidth != 0 || rotationDegrees != 0;
  109. }
  110. boolean hasCustomTransformations() {
  111. return transformations != null;
  112. }
  113. public Builder buildUpon() {
  114. return new Builder(this);
  115. }
  116. /** Builder for creating {@link Request} instances. */
  117. public static final class Builder {
  118. private Uri uri;
  119. private int resourceId;
  120. private int targetWidth;
  121. private int targetHeight;
  122. private boolean centerCrop;
  123. private boolean centerInside;
  124. private float rotationDegrees;
  125. private float rotationPivotX;
  126. private float rotationPivotY;
  127. private boolean hasRotationPivot;
  128. private List<Transformation> transformations;
  129. private Bitmap.Config config;
  130. /** Start building a request using the specified {@link Uri}. */
  131. public Builder(Uri uri) {
  132. setUri(uri);
  133. }
  134. /** Start building a request using the specified resource ID. */
  135. public Builder(int resourceId) {
  136. setResourceId(resourceId);
  137. }
  138. Builder(Uri uri, int resourceId) {
  139. this.uri = uri;
  140. this.resourceId = resourceId;
  141. }
  142. private Builder(Request request) {
  143. uri = request.uri;
  144. resourceId = request.resourceId;
  145. targetWidth = request.targetWidth;
  146. targetHeight = request.targetHeight;
  147. centerCrop = request.centerCrop;
  148. centerInside = request.centerInside;
  149. rotationDegrees = request.rotationDegrees;
  150. rotationPivotX = request.rotationPivotX;
  151. rotationPivotY = request.rotationPivotY;
  152. hasRotationPivot = request.hasRotationPivot;
  153. if (request.transformations != null) {
  154. transformations = new ArrayList<Transformation>(
  155. request.transformations);
  156. }
  157. config = request.config;
  158. }
  159. boolean hasImage() {
  160. return uri != null || resourceId != 0;
  161. }
  162. boolean hasSize() {
  163. return targetWidth != 0;
  164. }
  165. /**
  166. * Set the target image Uri.
  167. * <p>
  168. * This will clear an image resource ID if one is set.
  169. */
  170. public Builder setUri(Uri uri) {
  171. if (uri == null) {
  172. throw new IllegalArgumentException("Image URI may not be null.");
  173. }
  174. this.uri = uri;
  175. this.resourceId = 0;
  176. return this;
  177. }
  178. /**
  179. * Set the target image resource ID.
  180. * <p>
  181. * This will clear an image Uri if one is set.
  182. */
  183. public Builder setResourceId(int resourceId) {
  184. if (resourceId == 0) {
  185. throw new IllegalArgumentException(
  186. "Image resource ID may not be 0.");
  187. }
  188. this.resourceId = resourceId;
  189. this.uri = null;
  190. return this;
  191. }
  192. /** Resize the image to the specified size in pixels. */
  193. public Builder resize(int targetWidth, int targetHeight) {
  194. if (targetWidth <= 0) {
  195. throw new IllegalArgumentException(
  196. "Width must be positive number.");
  197. }
  198. if (targetHeight <= 0) {
  199. throw new IllegalArgumentException(
  200. "Height must be positive number.");
  201. }
  202. this.targetWidth = targetWidth;
  203. this.targetHeight = targetHeight;
  204. return this;
  205. }
  206. /**
  207. * Clear the resize transformation, if any. This will also clear center
  208. * crop/inside if set.
  209. */
  210. public Builder clearResize() {
  211. targetWidth = 0;
  212. targetHeight = 0;
  213. centerCrop = false;
  214. centerInside = false;
  215. return this;
  216. }
  217. /**
  218. * Crops an image inside of the bounds specified by
  219. * {@link #resize(int, int)} rather than distorting the aspect ratio.
  220. * This cropping technique scales the image so that it fills the
  221. * requested bounds and then crops the extra.
  222. */
  223. public Builder centerCrop() {
  224. if (centerInside) {
  225. throw new IllegalStateException(
  226. "Center crop can not be used after calling centerInside");
  227. }
  228. centerCrop = true;
  229. return this;
  230. }
  231. /** Clear the center crop transformation flag, if set. */
  232. public Builder clearCenterCrop() {
  233. centerCrop = false;
  234. return this;
  235. }
  236. /**
  237. * Centers an image inside of the bounds specified by
  238. * {@link #resize(int, int)}. This scales the image so that both
  239. * dimensions are equal to or less than the requested bounds.
  240. */
  241. public Builder centerInside() {
  242. if (centerCrop) {
  243. throw new IllegalStateException(
  244. "Center inside can not be used after calling centerCrop");
  245. }
  246. centerInside = true;
  247. return this;
  248. }
  249. /** Clear the center inside transformation flag, if set. */
  250. public Builder clearCenterInside() {
  251. centerInside = false;
  252. return this;
  253. }
  254. /** Rotate the image by the specified degrees. */
  255. public Builder rotate(float degrees) {
  256. rotationDegrees = degrees;
  257. return this;
  258. }
  259. /** Rotate the image by the specified degrees around a pivot point. */
  260. public Builder rotate(float degrees, float pivotX, float pivotY) {
  261. rotationDegrees = degrees;
  262. rotationPivotX = pivotX;
  263. rotationPivotY = pivotY;
  264. hasRotationPivot = true;
  265. return this;
  266. }
  267. /** Clear the rotation transformation, if any. */
  268. public Builder clearRotation() {
  269. rotationDegrees = 0;
  270. rotationPivotX = 0;
  271. rotationPivotY = 0;
  272. hasRotationPivot = false;
  273. return this;
  274. }
  275. /** Decode the image using the specified config. */
  276. public Builder config(Bitmap.Config config) {
  277. this.config = config;
  278. return this;
  279. }
  280. /**
  281. * Add a custom transformation to be applied to the image.
  282. * <p/>
  283. * Custom transformations will always be run after the built-in
  284. * transformations.
  285. */
  286. public Builder transform(Transformation transformation) {
  287. if (transformation == null) {
  288. throw new IllegalArgumentException(
  289. "Transformation must not be null.");
  290. }
  291. if (transformations == null) {
  292. transformations = new ArrayList<Transformation>(2);
  293. }
  294. transformations.add(transformation);
  295. return this;
  296. }
  297. /** Create the immutable {@link Request} object. */
  298. public Request build() {
  299. if (centerInside && centerCrop) {
  300. throw new IllegalStateException(
  301. "Center crop and center inside can not be used together.");
  302. }
  303. if (centerCrop && targetWidth == 0) {
  304. throw new IllegalStateException(
  305. "Center crop requires calling resize.");
  306. }
  307. if (centerInside && targetWidth == 0) {
  308. throw new IllegalStateException(
  309. "Center inside requires calling resize.");
  310. }
  311. return new Request(uri, resourceId, transformations, targetWidth,
  312. targetHeight, centerCrop, centerInside, rotationDegrees,
  313. rotationPivotX, rotationPivotY, hasRotationPivot, config);
  314. }
  315. }
  316. }