Java tutorial
//package com.java2s; /* * Copyright 2014 Chris Banes * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class Main { private static final String mTmdbBaseUrl = "http://image.tmdb.org/t/p/"; private static final int[] mTmdbPosterSizes = { 92, 154, 185, 342, 500, 780 }; private static String buildTmdbPosterUrl(String imageUrl, int width, boolean forceLarger) { if (mTmdbBaseUrl != null && mTmdbPosterSizes != null) { return buildTmdbUrl(mTmdbBaseUrl, imageUrl, selectSize(width, mTmdbPosterSizes, forceLarger)); } else { return null; } } private static String buildTmdbUrl(String baseUrl, String imagePath, int width) { StringBuilder url = new StringBuilder(baseUrl); if (width == Integer.MAX_VALUE) { url.append("original"); } else { url.append('w').append(width); } url.append(imagePath); return url.toString(); } private static int selectSize(final int width, final int[] widths, final boolean forceLarger) { int previousBucketWidth = 0; for (int i = 0; i < widths.length; i++) { final int currentBucketWidth = widths[i]; if (width < currentBucketWidth) { if (forceLarger || previousBucketWidth != 0) { // We're in between this and the previous bucket final int bucketDiff = currentBucketWidth - previousBucketWidth; if (width < previousBucketWidth + (bucketDiff / 2)) { return previousBucketWidth; } else { return currentBucketWidth; } } else { return currentBucketWidth; } } else if (i == widths.length - 1) { // If we get here then we're larger than a bucket if (width < currentBucketWidth * 2) { return currentBucketWidth; } } previousBucketWidth = currentBucketWidth; } return Integer.MAX_VALUE; } }