Java tutorial
/* * Copyright (C) 2014 The Android Open Source Project * * 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. */ package net.quduo.pixel.interfaces.android.common; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Build; import android.support.v4.util.LruCache; import android.util.Log; import net.quduo.pixel.BuildConfig; /** * ? */ public class ImageLoader { private static final String TAG = ImageLoader.class.getName(); private static final boolean DEBUG = BuildConfig.DEBUG; /** * ? */ private static LruCache<String, Bitmap> mMemoryCache; /** * ImageLoader */ private static ImageLoader mImageLoader; private ImageLoader() { // ??? int maxMemory = (int) Runtime.getRuntime().maxMemory(); // ???1/8 int cacheSize = maxMemory / 8; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) { return bitmap.getByteCount(); } else { return getByteCount(bitmap); } } }; } /** * ?ImageLoader * * @return ImageLoader */ public static ImageLoader getInstance() { if (mImageLoader == null) { mImageLoader = new ImageLoader(); } return mImageLoader; } /** * LruCache * * @param key LruCacheURL? * @param bitmap LruCacheBitmap */ public void addBitmapToMemoryCache(String key, Bitmap bitmap) { // TODO bitmap is null NullPointerException: key == null || value == null if (getBitmapFromMemoryCache(key) == null && bitmap != null) { // Log.e(TAG, "KEY:" + key + "VALUE:" + bitmap); mMemoryCache.put(key, bitmap); } } /** * LruCache??null * * @param key LruCacheURL? * @return Bitmapnull */ public Bitmap getBitmapFromMemoryCache(String key) { return mMemoryCache.get(key); } public static Bitmap decodeSampledBitmapFromResource(String pathName) { // ?inJustDecodeBoundstrue??? final BitmapFactory.Options options = new BitmapFactory.Options(); // options.inJustDecodeBounds = true; // BitmapFactory.decodeFile(pathName, options); // ?inSampleSize // options.inSampleSize = calculateInSampleSize(options, targetWidth); // ?inSampleSize?? options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(pathName, options); } /** * Bitmap.getByteCount()?Bitmap?? * * @param bitmap Bitmap * @return Bitmap?? */ public static int getByteCount(Bitmap bitmap) { Bitmap.Config config = bitmap.getConfig(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); int pixelByte = 4; // Config.ARGB_8888 if (config == Bitmap.Config.ARGB_8888) { pixelByte = 4; } else if (config == Bitmap.Config.ALPHA_8) { pixelByte = 1; } else if (config == Bitmap.Config.ARGB_4444) { pixelByte = 2; } else if (config == Bitmap.Config.RGB_565) { pixelByte = 2; } return width * height * pixelByte; } /* public static int calculateInSampleSize(BitmapFactory.Options options, int targetWidth) { // ? final int width = options.outWidth; int inSampleSize = 1; if (width > targetWidth) { // final int widthRatio = Math.round((float) width / (float) targetWidth); inSampleSize = widthRatio; } return inSampleSize; } private int calculateInSampleSize(Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math. round((float)height / ( float)reqHeight); } else { inSampleSize = Math. round((float)width / ( float)reqWidth); } } return inSampleSize; } */ }