Created
March 15, 2015 12:32
-
-
Save easternHong/e03dcfa0e600fc841ecb to your computer and use it in GitHub Desktop.
EffectBlur
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.renderscriptintrinsic; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.os.AsyncTask; | |
import android.renderscript.Allocation; | |
import android.renderscript.Element; | |
import android.renderscript.RenderScript; | |
import android.renderscript.ScriptIntrinsicBlur; | |
import android.widget.ImageView; | |
/** | |
* Created by hunt on 2015/3/15. | |
*/ | |
//this file refers from android_sdk_sample {RenderScriptIntrinsic 21} | |
/** | |
* steps: | |
* 1.get ImageView : mImageView = (ImageView) findViewById(R.id.imageView); | |
* 2.get BitMapBlur Instance : mBitMapBlur = new BitMapBlur(this, mImageView, R.drawable.data); | |
* 3.set ImageView's BackGround : mImageView.setImageBitmap(mBitMapBlur.getBitmapsOut()); | |
* 4.Create renderScript and set depth : mBitMapBlur.createScript().updateImage(100); | |
*/ | |
public class BitMapBlur { | |
private Context mContex; | |
//the imageview you want to deal with | |
private ImageView mTargetView; | |
//hold the bitMap cache . | |
private Bitmap mBitmapIn; | |
// | |
private Bitmap mBitmapsOut; | |
private RenderScript mRS; | |
private Allocation mInAllocation; | |
private Allocation mOutAllocations; | |
private ScriptIntrinsicBlur mScriptBlur; | |
// an AsyncTask | |
private RenderScriptTask mLatestTask = null; | |
public BitMapBlur(Context context, ImageView view, int drawable) { | |
this.mContex = context; | |
this.mTargetView = view; | |
//Set up main image view | |
mBitmapIn = loadBitmap(drawable); | |
mBitmapsOut = Bitmap.createBitmap(mBitmapIn.getWidth(), | |
mBitmapIn.getHeight(), mBitmapIn.getConfig()); | |
} | |
public Bitmap getBitmapsOut() { | |
return mBitmapsOut; | |
} | |
public BitMapBlur createScript() { | |
mRS = RenderScript.create(mContex); | |
mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn); | |
mOutAllocations = Allocation.createFromBitmap(mRS, mBitmapsOut); | |
/* | |
Create intrinsics. | |
RenderScript has built-in features such as blur, convolve filter etc. | |
These intrinsics are handy for specific operations without writing RenderScript kernel. | |
In the sample, it's creating blur, convolve and matrix intrinsics. | |
*/ | |
mScriptBlur = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS)); | |
return this; | |
} | |
private void performFilter(Allocation inAllocation, | |
Allocation outAllocation, Bitmap bitmapOut, float value) { | |
/* | |
* Set blur kernel size | |
*/ | |
mScriptBlur.setRadius(value); | |
/* | |
* Invoke filter kernel | |
*/ | |
mScriptBlur.setInput(inAllocation); | |
mScriptBlur.forEach(outAllocation); | |
/* | |
* Copy to bitmap and invalidate image view | |
*/ | |
outAllocation.copyTo(bitmapOut); | |
} | |
/* | |
Convert seekBar progress parameter (0-100 in range) to parameter for each intrinsic filter. | |
(e.g. 1.0-25.0 in Blur filter) | |
*/ | |
private float getFilterParameter(int i) { | |
final float max = 25.0f; | |
final float min = 1.f; | |
return (float) ((max - min) * (i / 100.0) + min); | |
} | |
/* | |
Invoke AsynchTask and cancel previous task. | |
When AsyncTasks are piled up (typically in slow device with heavy kernel), | |
Only the latest (and already started) task invokes RenderScript operation. | |
*/ | |
public void updateImage(int progress) { | |
float f = getFilterParameter(progress); | |
if (mLatestTask != null) | |
mLatestTask.cancel(false); | |
mLatestTask = new RenderScriptTask(); | |
mLatestTask.execute(f); | |
} | |
/* | |
Helper to load Bitmap from resource | |
*/ | |
private Bitmap loadBitmap(int resource) { | |
final BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inPreferredConfig = Bitmap.Config.ARGB_8888; | |
return BitmapFactory.decodeResource(mContex.getResources(), resource, options); | |
} | |
/* | |
* In the AsyncTask, it invokes RenderScript intrinsics to do a filtering. | |
* After the filtering is done, an operation blocks at Allication.copyTo() in AsyncTask thread. | |
* Once all operation is finished at onPostExecute() in UI thread, it can invalidate and update ImageView UI. | |
*/ | |
private class RenderScriptTask extends AsyncTask<Float, Integer, Integer> { | |
Boolean issued = false; | |
protected Integer doInBackground(Float... values) { | |
if (isCancelled() == false) { | |
issued = true; | |
performFilter(mInAllocation, mOutAllocations, mBitmapsOut, values[0]); | |
} | |
return 0; | |
} | |
void updateView(Integer result) { | |
if (result != -1) { | |
// Request UI update | |
mTargetView.setImageBitmap(mBitmapsOut); | |
mTargetView.invalidate(); | |
} | |
} | |
protected void onPostExecute(Integer result) { | |
updateView(result); | |
} | |
protected void onCancelled(Integer result) { | |
if (issued) { | |
updateView(result); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment