Android Open Source - LiveBlurListView Blur






From Project

Back to project page LiveBlurListView.

License

The source code is released under:

Apache License

If you think the Android project LiveBlurListView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.koalcat.view;
/**/*  w ww .j a va2s .  com*/
 * @author xuchdeid@gmail.com
 *  __________________________     \_/
   |                          |   /._.\
   |  Android!Android!         > U|   |U
   |                xuchdeid  |   |___|
   |__________________________|    U U
 * */

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
//import android.os.Build;
import android.support.v4.util.LruCache;

public class Blur {
  
  private static final String TAG = "Blur";
  
  private boolean USE_JNI = true;
  
  private static final float BITMAP_SCALE_NOMAL = 0.8f;
  private static final float BLUR_RADIUS_NOMAL = 12.0f;
  
  private static final float BITMAP_SCALE_FAST = 0.5f;
  private static final float BLUR_RADIUS_FAST = 10.0f;

  private BaseRender mRender;

  private LruCache<String, Bitmap> mMemoryCache; 

  public Blur(Context context) {
    
    if (!USE_JNI) {
      int sdk = Build.VERSION.SDK_INT;
      if (sdk < 17) {
        USE_JNI = true;
      }
    }
    
    if (!USE_JNI) {
      mRender = new ScriptIntrinsicBlurRender(context);
      //mRender = new BlurRSRender(context);
    } else {
      mRender = new JNIRender();
    }
    
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);  
    final int cacheSize = maxMemory / 16;  
      
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {  
      @Override  
      protected int sizeOf(String key, Bitmap bitmap) {  
        return bitmap.getByteCount() / 1024;  
      }  
    };
  }

  public Bitmap blur(Bitmap image, boolean fast) {

    float scale = BITMAP_SCALE_NOMAL;
    float radius = BLUR_RADIUS_NOMAL;
    
    if (fast) {
      scale = BITMAP_SCALE_FAST;
      radius = BLUR_RADIUS_FAST;
    }
    
    int width = Math.round(image.getWidth() * scale);
    int height = Math.round(image.getHeight() * scale);

    Bitmap bitmap = Bitmap.createScaledBitmap(image, width, height, true);
    Bitmap outputBitmap = getBitmapFromMemCache("" + width + height);
    if (outputBitmap == null) {
      outputBitmap = bitmap.copy(bitmap.getConfig(), true);
      addBitmapToMemoryCache("" + width + height, outputBitmap);
    }

    mRender.blur(radius, bitmap, outputBitmap);
    bitmap.recycle();
    
    return outputBitmap;
  }
  
  private void addBitmapToMemoryCache(String key, Bitmap bitmap) {  
      if (getBitmapFromMemCache(key) == null) {  
          mMemoryCache.put(key, bitmap);  
      }  
  }  
    
  private Bitmap getBitmapFromMemCache(String key) {  
      return mMemoryCache.get(key);  
  }
    
  public void Destroy() {
    if (mRender != null) mRender.destroy();
  }
  
/*  public Bitmap Blur_openGL(Bitmap image, boolean fast) {
    float scale = BITMAP_SCALE_NOMAL;
    float radius = BLUR_RADIUS_NOMAL;
    
    if (fast) {
      scale = BITMAP_SCALE_FAST;
      radius = BLUR_RADIUS_FAST;
    }
    
    int width = Math.round(image.getWidth() * scale);
    int height = Math.round(image.getHeight() * scale);

/*    Bitmap bitmap = Bitmap.createScaledBitmap(image, width, height, true);
    Bitmap outputBitmap = getBitmapFromMemCache("" + width + height);
    if (outputBitmap == null) {
      outputBitmap = bitmap.copy(bitmap.getConfig(), true);
      addBitmapToMemoryCache("" + width + height, outputBitmap);
    }
*/    
/*    if (gl == null) {
      gl = new GLRender(image.getWidth(), image.getHeight());
    }
    
    gl.Blur(image, fast);
    
    return image;
    
  }
  */
}




Java Source Code List

com.koalcat.blurdemo.AppInfo.java
com.koalcat.blurdemo.ApplicationsAdapter.java
com.koalcat.blurdemo.BlurActivity.java
com.koalcat.blurdemo.FastBitmapDrawable.java
com.koalcat.blurdemo.HanziToPinyin.java
com.koalcat.blurdemo.IndexCursor.java
com.koalcat.blurdemo.Utilities.java
com.koalcat.view.BaseRender.java
com.koalcat.view.BlurRSRender.java
com.koalcat.view.Blur.java
com.koalcat.view.GLRender.java
com.koalcat.view.JNIRender.java
com.koalcat.view.LiveBlurListView.java
com.koalcat.view.LiveBlurViewGroup.java
com.koalcat.view.RSRender.java
com.koalcat.view.ScriptIntrinsicBlurRender.java