Created
April 28, 2014 03:05
-
-
Save andraskindler/11360793 to your computer and use it in GitHub Desktop.
Sample code for a tutorial (http://howrobotswork.wordpress.com/2014/04/28/how-to-dim-bitmaps-with-renderscript/)
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.andraskindler.sandbox.activity; | |
import android.app.Activity; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.os.Bundle; | |
import android.support.v8.renderscript.Allocation; | |
import android.support.v8.renderscript.RenderScript; | |
import android.widget.FrameLayout; | |
import android.widget.ImageView; | |
import com.andraskindler.sandbox.R; | |
import com.andraskindler.sandbox.ScriptC_dim; | |
import hugo.weaving.DebugLog; | |
import rx.Observable; | |
import rx.Subscriber; | |
import rx.android.schedulers.AndroidSchedulers; | |
import rx.functions.Action1; | |
public class RenderScriptActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
final FrameLayout root = (FrameLayout) getLayoutInflater().inflate(R.layout.activity_main, null); | |
setContentView(root); | |
final ImageView imageView = (ImageView) root.findViewById(R.id.iv_main); | |
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test_image); | |
dimWithRenderScript(bitmap).subscribe(new Action1<Bitmap>() { | |
@Override public void call(Bitmap bitmap) { | |
imageView.setImageBitmap(bitmap); | |
} | |
}); | |
} | |
@DebugLog | |
private Observable<Bitmap> dimWithRenderScript(final Bitmap in) { | |
return Observable.create(new Observable.OnSubscribe<Bitmap>() { | |
@Override public void call(Subscriber<? super Bitmap> subscriber) { | |
final RenderScript renderScript = RenderScript.create(RenderScriptActivity.this); | |
final ScriptC_dim script = new ScriptC_dim(renderScript); | |
script.set_dimmingValue(0.6f); | |
final Allocation alloc1 = Allocation.createFromBitmap(renderScript, in, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); | |
final Allocation alloc2 = Allocation.createTyped(renderScript, alloc1.getType()); | |
script.forEach_dim(alloc1, alloc2); | |
alloc2.copyTo(in); | |
renderScript.destroy(); | |
subscriber.onNext(in); | |
subscriber.onCompleted(); | |
} | |
}).subscribeOn(AndroidSchedulers.mainThread()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment