net.evecom.androidecssp.activity.pub.imagescan.NativeImageLoader.java Source code

Java tutorial

Introduction

Here is the source code for net.evecom.androidecssp.activity.pub.imagescan.NativeImageLoader.java

Source

/*
 * Copyright (c) 2005, 2014, EVECOM Technology Co.,Ltd. All rights reserved.
 * EVECOM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 */
package net.evecom.androidecssp.activity.pub.imagescan;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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;

/**
 * 
 * ,getInstance()NativeImageLoader
 * loadNativeImage()
 * 
 * @author Mars zhang
 * @created 2015-11-25 11:26:10
 */
public class NativeImageLoader {
    /** MemberVariables */
    private LruCache<String, Bitmap> mMemoryCache;
    /** MemberVariables */
    private static NativeImageLoader mInstance = new NativeImageLoader();
    /** MemberVariables */
    private ExecutorService mImageThreadPool = Executors.newFixedThreadPool(1);

    /**
     * 
     * 
     * 
     * @author Mars zhang
     * @created 2015-11-25 12:06:01
     */
    private NativeImageLoader() {
        // 
        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 NativeImageLoader getInstance() {
        return mInstance;
    }

    /**
     * 
     * 
     * @param path
     * @param mCallBack
     * @return
     */
    public Bitmap loadNativeImage(final String path, final NativeImageCallBack mCallBack) {
        return this.loadNativeImage(path, null, mCallBack);
    }

    /**
     * mPointImageViewImageViewBitmap
     * loadNativeImage(final String path, final NativeImageCallBack
     * mCallBack)
     * 
     * @param path
     * @param mPoint
     * @param mCallBack
     * @return
     */
    public Bitmap loadNativeImage(final String path, final Point mPoint, final NativeImageCallBack 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);
            }

        };

        // BitmapBitmapmMemoryCache
        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 width
     * @param height
     */
    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 NativeImageCallBack {
        /**
         * Bitmap
         * 
         * @param bitmap
         * @param path
         */
        public void onImageLoader(Bitmap bitmap, String path);
    }
}