Java tutorial
/**************************************************************************** * Copyright (C) 2014 www.apkdv.com * Author: LengYue ProjectName: com.dv.Task * * 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 com.dv.BitMap.Native; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Point; import android.os.Handler; import android.os.Message; import android.support.v4.util.LruCache; //~--- JDK imports ------------------------------------------------------------ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * DvNativeImageLoader? description * ??? * * @ProJect DvTools * @Author LengYue * @Support http://www.apkdv.com/ * @CLassName DvNativeImageLoader */ public class DvNativeImageLoader { private static DvNativeImageLoader mInstance = new DvNativeImageLoader(); private ExecutorService mImageThreadPool = Executors.newFixedThreadPool(1); private LruCache<String, Bitmap> mMemoryCache; private DvNativeImageLoader() { // ?? final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // 1/4? final int cacheSize = maxMemory / 4; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { // ??? @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getRowBytes() * bitmap.getHeight() / 1024; } }; } /** * ??NativeImageLoader * * @return */ public static DvNativeImageLoader getInstance() { return mInstance; } /** * ?? * * @param path * @param mCallBack * @return */ public Bitmap loadNativeImage(final String path, final DvNativeImageCallBack mCallBack) { return this.loadNativeImage(path, null, mCallBack); } /** * ?mPoint??ImageView?ImageView???Bitmap * ??loadNativeImage(final String path, final NativeImageCallBack mCallBack)? * * @param path * @param mPoint * @param mCallBack * @return */ public Bitmap loadNativeImage(final String path, final Point mPoint, final DvNativeImageCallBack mCallBack) { // ?Bitmap Bitmap bitmap = getBitmapFromMemCache(path); final Handler mHander = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); mCallBack.onImageLoader((Bitmap) msg.obj, path); } }; // Bitmap??BitmapmMemoryCache if (bitmap == null) { mImageThreadPool.execute(new Runnable() { @Override public void run() { // ? Bitmap mBitmap = decodeThumbBitmapForFile(path, (mPoint == null) ? 0 : mPoint.x, (mPoint == null) ? 0 : mPoint.y); Message msg = mHander.obtainMessage(); msg.obj = mBitmap; mHander.sendMessage(msg); // addBitmapToMemoryCache(path, mBitmap); } }); } return bitmap; } /** * Bitmap * * @param key * @param bitmap */ private void addBitmapToMemoryCache(String key, Bitmap bitmap) { if ((getBitmapFromMemCache(key) == null) && (bitmap != null)) { mMemoryCache.put(key, bitmap); } } /** * ?key?? * * @param key * @return */ private Bitmap getBitmapFromMemCache(String key) { return mMemoryCache.get(key); } /** * ?View(?ImageView)?? * * @param path * @param viewWidth * @param viewHeight * @return */ private Bitmap decodeThumbBitmapForFile(String path, int viewWidth, int viewHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); // true,?Bitmap?? options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); // options.inSampleSize = computeScale(options, viewWidth, viewHeight); // false,?Bitmap options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(path, options); } /** * ?View(?ImageView)?Bitmap? * * @param options * @param viewWidth * @param viewHeight * @return */ private int computeScale(BitmapFactory.Options options, int viewWidth, int viewHeight) { int inSampleSize = 1; if ((viewWidth == 0) || (viewWidth == 0)) { return inSampleSize; } int bitmapWidth = options.outWidth; int bitmapHeight = options.outHeight; // ?BitmapView if ((bitmapWidth > viewWidth) || (bitmapHeight > viewWidth)) { int widthScale = Math.round((float) bitmapWidth / (float) viewWidth); int heightScale = Math.round((float) bitmapHeight / (float) viewWidth); // ?????? inSampleSize = (widthScale < heightScale) ? widthScale : heightScale; } return inSampleSize; } /** * ? * * @author xiaanming */ public interface DvNativeImageCallBack { /** * ?Bitmap * * @param bitmap * @param path */ public void onImageLoader(Bitmap bitmap, String path); } }