/** * 根据ImageView获得适当的压缩的宽和高 * * @param imageView * @return */ private ImageSize getImageViewWidth(ImageView imageView) { ImageSize imageSize = new ImageSize(); final DisplayMetrics displayMetrics = imageView.getContext().getResources().getDisplayMetrics(); final LayoutParams params = imageView.getLayoutParams(); int width = params.width == LayoutParams.WRAP_CONTENT ? 0 : imageView.getWidth(); // Get actual image width if (width <= 0) width = params.width; // Get layout width parameter if (width <= 0) width = getImageViewFieldValue(imageView, "mMaxWidth"); // Check // maxWidth // parameter if (width <= 0) width = displayMetrics.widthPixels; int height = params.height == LayoutParams.WRAP_CONTENT ? 0 : imageView.getHeight(); // Get actual image height if (height <= 0) height = params.height; // Get layout height parameter if (height <= 0) height = getImageViewFieldValue(imageView, "mMaxHeight"); // Check // maxHeight // parameter if (height <= 0) height = displayMetrics.heightPixels; imageSize.width = width; imageSize.height = height; return imageSize; }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) private static ImageSize getImageViewSize(ImageView imageView) { ImageSize imageSize = new ImageSize(); DisplayMetrics displayMetrics = imageView.getContext().getResources().getDisplayMetrics(); ViewGroup.LayoutParams lp = imageView.getLayoutParams(); /* * 获取图片压缩的宽和高 * */ int width = imageView.getWidth(); // 获取imageView的实际宽度 if (width <= 0) { width = lp.width; // 获取imageView在布局中设置的宽度 } if (width <= 0) { width = imageView.getMaxWidth(); } if (width <= 0) { width = displayMetrics.widthPixels; } imageSize.width = width; int height = imageView.getHeight(); // 获取imageView的实际宽度 if (height <= 0) { height = lp.height; // 获取imageView在布局中设置的宽度 } if (height <= 0) { height = imageView.getMaxHeight(); } if (height <= 0) { height = displayMetrics.heightPixels; } imageSize.height = height; return imageSize; }
/** * Generates key for memory cache for incoming image (URI + size).<br> * Pattern for cache key - <b>[imageUri]_[width]x[height]</b>. */ public static String generateKey(String imageUri, ImageSize targetSize) { return new StringBuilder(imageUri) .append(URI_AND_SIZE_SEPARATOR) .append(targetSize.getWidth()) .append(WIDTH_AND_HEIGHT_SEPARATOR) .append(targetSize.getHeight()) .toString(); }
/** * 根据ImageView获适当的压缩的宽和高 * * @param view * @return */ public static ImageSize getImageViewSize(View view) { ImageSize imageSize = new ImageSize(); imageSize.width = getExpectWidth(view); imageSize.height = getExpectHeight(view); return imageSize; }
static Image imageFromElement(DomElement e) { Image i = new Image(); i.title = e.getChildText("title"); i.url = e.getChildText("url"); i.format = e.getChildText("format"); try { i.dateAdded = DATE_ADDED_FORMAT.parse(e.getChildText("dateadded")); } catch (ParseException e1) { e1.printStackTrace(); } DomElement owner = e.getChild("owner"); if (owner != null) i.owner = owner.getChildText("name"); DomElement votes = e.getChild("votes"); if (votes != null) { i.thumbsUp = Integer.parseInt(votes.getChildText("thumbsup")); i.thumbsDown = Integer.parseInt(votes.getChildText("thumbsdown")); } DomElement sizes = e.getChild("sizes"); for (DomElement image : sizes.getChildren("size")) { // code copied from ImageHolder.loadImages String attribute = image.getAttribute("name"); ImageSize size; if (attribute == null) size = ImageSize.MEDIUM; // workaround for image responses without size attr. else size = ImageSize.valueOf(attribute.toUpperCase(Locale.ENGLISH)); i.imageUrls.put(size, image.getText()); } return i; }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { final ImageSize size = ImageSize.fromString(req.getParameter(SIZE_PARAM)); URL url = null; try { url = new URL(req.getParameter(URL_PARAM)); Preconditions.checkNotNull(url); } catch (Exception e) { LOG.error("Invalid image url requested", e); resp.sendError( HttpServletResponse.SC_BAD_REQUEST, "Please provide a valid image url parameter"); return; } try { CachedImage img = cache.get(url, size); resp.setHeader(HttpHeaders.CONTENT_TYPE, img.getMimeType()); ByteStreams.copy(img, resp.getOutputStream()); } catch (IOException e) { String errMsg = String.format("No image found for url '%s'", url); LOG.error(errMsg, e); resp.sendError(HttpServletResponse.SC_NOT_FOUND, errMsg); } finally { resp.flushBuffer(); } }
/** * Returns the URL of the image in the specified size, or <code>null</code> if not available. * * @param size The preferred size * @return an image URL * @author Harry Chen */ public String getImageURL() { ImageSize[] sizes = ImageSize.values(); for (ImageSize testSize : sizes) { String url = imageUrls.get(testSize); if (url != null) return url; } return null; }
@Override public int compareTo(ImageSize other) { long thisImageSize = this.getWidth() * this.getHeight(); long otherImageSize = other.getWidth() * other.getHeight(); if (thisImageSize < otherImageSize) { return -1; } else if (thisImageSize == otherImageSize) { if (this.getWidth() < other.getWidth()) { return -1; } else if (this.getWidth() == other.getWidth()) { return 0; } else { return 1; } } else { return 1; } }
/** * Adds load image task to execution pool. Image will be returned with {@link * ImageLoadingListener#onLoadingComplete(String, android.view.View, android.graphics.Bitmap)} * callback}.<br> * <b>NOTE:</b> {@link #init(ImageLoaderConfiguration)} method must be called before this method * call * * @param uri Image URI (i.e. "http://site.com/image.png", "file:///mnt/sdcard/image.png") * @param targetImageSize Minimal size for {@link Bitmap} which will be returned in {@linkplain * ImageLoadingListener#onLoadingComplete(String, android.view.View, android.graphics.Bitmap)} * callback}. Downloaded image will be decoded and scaled to {@link Bitmap} of the size which * is <b>equal or larger</b> (usually a bit larger) than incoming minImageSize . * @param options {@linkplain DisplayImageOptions Display image options} for image displaying. If * <b>null</b> - default display image options {@linkplain * ImageLoaderConfiguration.Builder#defaultDisplayImageOptions(DisplayImageOptions) from * configuration} will be used.<br> * Incoming options should contain {@link FakeBitmapDisplayer} as displayer. * @param listener {@linkplain ImageLoadingListener Listener} for image loading process. Listener * fires events on UI thread. * @throws IllegalStateException if {@link #init(ImageLoaderConfiguration)} method wasn't called * before */ public void loadImage( String uri, ImageSize targetImageSize, DisplayImageOptions options, ImageLoadingListener listener) { checkConfiguration(); if (targetImageSize == null) { targetImageSize = new ImageSize( configuration.maxImageWidthForMemoryCache, configuration.maxImageHeightForMemoryCache); } if (options == null) { options = configuration.defaultDisplayImageOptions; } DisplayImageOptions optionsWithFakeDisplayer; if (options.getDisplayer() instanceof FakeBitmapDisplayer) { optionsWithFakeDisplayer = options; } else { optionsWithFakeDisplayer = new DisplayImageOptions.Builder() .cloneFrom(options) .displayer(fakeBitmapDisplayer) .build(); } ImageView fakeImage = new ImageView(configuration.context); fakeImage.setLayoutParams( new LayoutParams(targetImageSize.getWidth(), targetImageSize.getHeight())); fakeImage.setScaleType(ScaleType.CENTER_CROP); displayImage(uri, fakeImage, optionsWithFakeDisplayer, listener); }
/** * Extract image URL * * @param imageList the image list from XML * @return the found image URL or null if not found */ private String extractImageURL(List<Image> imageList) { String imageURL = null; for (Image image : imageList) { // for each image if ((image != null) && (lastFMCoverServicePropertiesBean.getImageSize().equalsIgnoreCase(image.getSize()))) { imageURL = image.getValue(); } } if (imageURL == null) { // image not found, try to find the biggest one for (ImageSize imageSize : ImageSize.values()) { for (Image image : imageList) { if ((image != null) && (imageSize.toString().equalsIgnoreCase(image.getSize()))) { imageURL = image.getValue(); LOGGER.warn("Unable to find configured image size - used : " + imageSize); break; } } if (imageURL != null) break; } } return imageURL; }
/** * @param holder * @param element */ protected static void loadImages(final ImageHolder holder, final DomElement element) { final Collection<DomElement> images = element.getChildren("image"); for (final DomElement image : images) { final String attribute = image.getAttribute("size"); ImageSize size = null; if (attribute == null) { size = ImageSize.LARGESQUARE; } else { try { size = ImageSize.valueOf(attribute.toUpperCase(Locale.ENGLISH)); } catch (final IllegalArgumentException e) { // if they suddenly again introduce a new image size } } if (size != null) { holder.imageUrls.put(size, image.getText()); } } }
public String toString(@NotNull ImageSize data) { return data.toString(); }