-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(android): blur example to work like iOS and use an image
- Loading branch information
1 parent
eaac06a
commit 614e83c
Showing
2 changed files
with
55 additions
and
48 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,60 @@ | ||
import Bitmap from 'android.graphics.Bitmap'; | ||
import BitmapFactory from 'android.graphics.BitmapFactory'; | ||
import Config from 'android.graphics.Bitmap.Config'; | ||
import RenderScript from 'android.renderscript.RenderScript'; | ||
import Allocation from 'android.renderscript.Allocation'; | ||
import Element from 'android.renderscript.Element'; | ||
import ScriptIntrinsicBlur from 'android.renderscript.ScriptIntrinsicBlur'; | ||
import Activity from 'android.app.Activity'; | ||
import Paint from 'android.graphics.Paint'; | ||
import Color from 'android.graphics.Color'; | ||
import Canvas from 'android.graphics.Canvas'; | ||
import ImageView from 'android.widget.ImageView'; | ||
|
||
(function(container) { | ||
const activity = new Activity(Ti.Android.currentActivity); | ||
|
||
const WIDTH = 200; | ||
const HEIGHT = 200; | ||
|
||
// create input image | ||
// TODO pass ImageView/TiBlob | ||
const bmp = Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_8888); | ||
const canvas = new Canvas(bmp); | ||
const paint = new Paint(); | ||
paint.setColor(Color.rgb(255, 0, 0)); | ||
paint.setStrokeWidth(10); | ||
canvas.drawLine(WIDTH * 0.5, 0, WIDTH * 0.5, HEIGHT, paint); | ||
paint.setColor(Color.rgb(0, 255, 0)); | ||
canvas.drawLine(0, HEIGHT * 0.5, WIDTH, HEIGHT * 0.5, paint); | ||
|
||
// create blur output image | ||
const bmpOut = Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_8888); | ||
|
||
// blur | ||
const rs = RenderScript.create(activity); | ||
const blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); | ||
const allIn = Allocation.createFromBitmap(rs, bmp); | ||
const allOut = Allocation.createFromBitmap(rs, bmpOut); | ||
|
||
blurScript.setRadius(10.0); // set blur value | ||
blurScript.setInput(allIn); | ||
blurScript.forEach(allOut); | ||
allOut.copyTo(bmpOut); | ||
bmp.recycle(); | ||
rs.destroy(); | ||
|
||
// create imageview and attach it | ||
const image = new ImageView(activity); | ||
container.add(image); | ||
image.setImageBitmap(bmpOut); | ||
})($.blur_container); | ||
|
||
function onOpen() { | ||
// Fetch the window's activity reference. | ||
const activity = new Activity($.win.activity); | ||
|
||
// Load the source image from the APK's "assets" folder. | ||
const stream = activity.getAssets().open('Resources/images/appc-logo.png') | ||
$.sourceBitmap = BitmapFactory.decodeStream(stream); | ||
stream.close(); | ||
|
||
// Display a native ImageView with the above image. | ||
$.imageView = new ImageView(activity); | ||
$.imageView.setImageBitmap($.sourceBitmap); | ||
$.win.insertAt({ | ||
position: 0, | ||
view: $.imageView | ||
}); | ||
} | ||
|
||
function onBlurImage() { | ||
// Fetch the window's activity reference. | ||
const activity = new Activity($.win.activity); | ||
|
||
// Create a blurred bitmap from the source bitmap. | ||
const blurredBitmap = Bitmap.createBitmap($.sourceBitmap.getWidth(), $.sourceBitmap.getHeight(), Config.ARGB_8888); | ||
const rs = RenderScript.create(activity); | ||
const blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); | ||
const allIn = Allocation.createFromBitmap(rs, $.sourceBitmap); | ||
const allOut = Allocation.createFromBitmap(rs, blurredBitmap); | ||
blurScript.setRadius(10.0); | ||
blurScript.setInput(allIn); | ||
blurScript.forEach(allOut); | ||
allOut.copyTo(blurredBitmap); | ||
rs.destroy(); | ||
|
||
// Display the blurred image. | ||
$.imageView.setImageBitmap(blurredBitmap); | ||
|
||
// Update buttons. | ||
$.blurButton.visible = false; | ||
$.unblurButton.visible = true; | ||
} | ||
|
||
function onUnblurImage() { | ||
// Display the original unblurred image. | ||
$.imageView.setImageBitmap($.sourceBitmap); | ||
|
||
// Update buttons. | ||
$.blurButton.visible = true; | ||
$.unblurButton.visible = false; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
<Alloy> | ||
<Window id="win" title="Blur"> | ||
|
||
<ScrollView> | ||
<View class="vbox" id="blur_container" /> | ||
</ScrollView> | ||
|
||
<Window id="win" title="Blur" onOpen="onOpen"> | ||
<Button id="blurButton" bottom="30" onClick="onBlurImage">Blur Image</Button> | ||
<Button id="unblurButton" bottom="30" visible="false" onClick="onUnblurImage">Unblur Image</Button> | ||
</Window> | ||
</Alloy> |