Android Blur Demo sample-renderscript-effect Diagram and code

Source: Internet
Author: User
Tags gety

This article links http://blog.csdn.net/xiaodongrush/article/details/31031411

References link Android advanced Blur technology Http://stackoverflow.com/questions/14879439/renderscript-via-the-support-library

1. Procedures

Drag the red area to show a clear part of the car.

Drag the slider below to change the degree of blur.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqveglhb2rvbmdydxno/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

2. Program implementation Methods

Realize the idea. With framelayout made three layers, the bottom layer is a clear picture, the middle layer is blurred picture, the top layer. is the red area, this layer is a clear picture.

public static class Placeholderfragment extends Fragment {//new Android Adt-bundle with a Fragment in activity by default. It's said that Android Stdio has long been like this. Private ImageView moriginiv;private ImageView mbluriv;private ImageView mcleariv;private SeekBar Mradiussb;public placeholderfragment () {} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup Container,bundle savedinstancestate) {View Rootview = inflater.inflate (R.layout.fragment_main, Container,false); return Rootview;} @Overridepublic void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate); Moriginiv = (ImageView) getactivity (). Findviewbyid (r.id.origin_image); Mbluriv = (ImageView) getactivity (). Findviewbyid (r.id.blur_image); Mcleariv = (ImageView) getactivity (). Findviewbyid (r.id.clear_image); MRADIUSSB = ( SeekBar) getactivity (). Findviewbyid (R.id.radius_seekbar);d rawblurimage (); Initializes the blur layer. Setseekbarchangelisten (); Sets the Seekbar callback.                        Update the Blur layer when the slider position changes. The delay is to ensure view.getx. View.getwidth this kind of method can go to the numerical value,This is just for initialization, so the delay is better. 

Let's say if you visualize it every time. To read weidht and X, you can call it again in Activity#onwindowfocuschange. Runnable Runnable = new Runnable () {@Overridepublic void run () {Onmovelistener listener = new Onmovelistener () {@Overridep ublic void onmoved () {Moriginiv.builddrawingcache (); Clear (Moriginiv.getdrawingcache (), Mcleariv, 10);// This is a good way to get the view to draw the image}}; Moveutils.enablemove (Mcleariv, listener);}; Mcleariv.postdelayed (runnable, 500);} private void Drawblurimage () {moriginiv.getviewtreeobserver (). Addonpredrawlistener (New Onpredrawlistener () {@ Overridepublic Boolean Onpredraw () {moriginiv.getviewtreeobserver (). Removeonpredrawlistener (this); Moriginiv.builddrawingcache (); Float radius = mradiussb.getprogress (); if (RADIUS < 0.1) {// Renderscript requires radius between 0 and 25 and cannot be equal to RADIUS = 0.1f;} if (Radius > 24.9) {radius = 24.9f;} Blur (Moriginiv.getdrawingcache (), mbluriv, RADIUS); Clear (Moriginiv.getdrawingcache (), Mcleariv, 10); Here in order to display the border. Lazy the direct use of 10px, is actually 5dip. On my phone Galaxy Nexus. 1DIP=2PX, in fact, should be converted. return true; This isRequired in the article. Never tried false. }});} private void Setseekbarchangelisten () {Mradiussb.setonseekbarchangelistener (new Onseekbarchangelistener () {@ overridepublic void Onstoptrackingtouch (SeekBar arg0) {} @Overridepublic void Onstarttrackingtouch (SeekBar arg0) {}@ overridepublic void onprogresschanged (SeekBar arg0, int arg1,boolean arg2) {drawblurimage ();});} First, a cropped image is generated from bkg based on the size of the view, then the cropped image is blurred based on radius, and the obfuscated image is then set to view. @TargetApi (build.version_codes. JELLY_BEAN_MR1) private void Blur (Bitmap bkg, view view, float radius) {//Trim picture process Bitmap overlay = B Itmap.createbitmap (int) (View.getmeasuredwidth ()), (int) (View.getmeasuredheight ()), Bitmap.Config.ARGB_8888); Canvas canvas = new canvas (overlay); Canvas.translate (-view.getx (),-view.gety ()); Here is the set coordinate system origin point Canvas.drawbitmap (bkg, 0, 0, NULL); Here the image is drawn directly at the origin of the new coordinate system, assuming that the coordinate system is not set, the equivalent of drawing an image on (View.getx (), view.gety), Android to the right x-axis square, and the y-axis positive direction downward. The process of blurring a picture renderscript rs = renderscript.create (getactivity ()); RendeRscript requires APILevel 17, which is more disgusting. V8 support packages are not particularly useful. Really want to blur, or OpenCV jni to do it. Allocation Overlayalloc = Allocation.createfrombitmap (rs, overlay); Scriptintrinsicblur blur = Scriptintrinsicblur.create (Rs,overlayalloc.getelement ()); Blur.setinput (OverlayAlloc); Blur.setradius (RADIUS); Blur.foreach (Overlayalloc); Overlayalloc.copyto (overlay); Set Picture View.setbackground (New Bitmapdrawable (Getresources (), overlay)); Rs.destroy ();} First, a cropped image is generated from bkg based on the size of the view, and then the cropped image is set to the view. private void Clear (Bitmap bkg, ImageView view, int paddingpx) {Bitmap overlay = Bitmap.createbitmap ((int) (view.getmeasure Dwidth ()-PADDINGPX * 2), (int) (View.getmeasuredheight ()-PADDINGPX * 2), Bitmap.Config.ARGB_8888); Canvas canvas = new canvas (overlay), Canvas.translate (-view.getx ()-PADDINGPX,-view.gety ()-PADDINGPX); Canvas.drawbitmap (bkg, 0, 0, null); View.setimagedrawable (New Bitmapdrawable (Getresources (), overlay));}}



3. Code Download

The evil csdn uploaded the code. For hours, not yet checked out ...

http://download.csdn.net/detail/u011267546/7502603 Note that the minsdk of the code I set is relatively high, is the API Level17. No way. The support library for Renderscript was not done.

4. Several questions

Renderscript Although there is a SUPPORT-V8 support library. But I did the meeting, did not compile successfully.

Also saw a post said on the 2.3.5 Renderscript have a problem. So the feeling is not particularly reliable, or JNI+OPENCV himself to get better. There are many fuzzy algorithms related to online OpenCV.

In addition, it is assumed that the image is very large and the fuzzy processing is time consuming. It is best to do it asynchronously.

getwidth. Getheight,getleft call time OnStart, onreusme These are not, just can in Onwindowfoucschange.

The demo sample of this article is called when initializing. So it is possible to delay a run, assuming that if you switch from the background to the foreground, it will be called. Then it is called in Onwindowfoucschange.

Use the canvas trim bitmap to pay attention to the coordinate system. Android to the right is the X Axis square. The y-axis positive direction when down.

private void Clear (Bitmap bkg, ImageView view, int paddingpx) {Bitmap overlay = Bitmap.createbitmap ((int) (view.getmeasure Dwidth ()-PADDINGPX * 2), (int) (View.getmeasuredheight ()-PADDINGPX * 2), Bitmap.Config.ARGB_8888); Canvas canvas = new canvas (overlay), Canvas.translate (-view.getx ()-PADDINGPX,-view.gety ()-PADDINGPX); Canvas.drawbitmap (bkg, 0, 0, null); View.setimagedrawable (New Bitmapdrawable (Getresources (), overlay));}
get picture Draw cache

Moriginiv.builddrawingcache (); Clear (Moriginiv.getdrawingcache (), Mcleariv, 10);
Let View be dragged
Write a simple way to implement view tag+ontouchevent by using the view to drag.

Package Com.example.blurtest;import Android.view.motionevent;import Android.view.view;import  Android.view.view.ontouchlistener;public class Moveutils {private static final int state_idle = 0;private static final int state_moving = 1;private static final int min_gap = 5;private Static class Info {public int state = State_idle;public Flo At lastx = -1;public float lasty = -1;public Onmovelistener listener;} private static Info GetInfo (view view) {if (View.gettag () = = null) {View.settag (New Info ());} Return (Info) (View.gettag ());} private static void Trytomove (view view, Motionevent ev) {Info Info = getInfo (view); if (info.state! = state_moving) {retur n;}  float x = Ev.getx ()-Info.lastx;float y = ev.gety ()-Info.lasty;if (Math.Abs (x) < Min_gap && Math.Abs (y) < MIN_GAP) {return;} View.setx (View.getx () + x); View.sety (view.gety () + y); view.invalidate (); info.listener.onMoved ();} public static void Enablemove (View target, Onmovelistener listener) {Info info = new Info (); Info.listener= Listener;target.settag (info); Target.setontouchlistener (new Ontouchlistener () {@Overridepublic Boolean OnTouch ( View view, Motionevent ev) {Info info = getInfo (view); int action = Ev.getaction () & Motionevent.action_mask;switch (AC tion) {case MotionEvent.ACTION_DOWN:info.state = State_moving;info.lastx = Ev.getx (); info.lasty = Ev.gety (); View.settag (info); Break;case MotionEvent.ACTION_MOVE:tryToMove (view, Ev); Break;case motionevent.action_up: Info.state = State_idle;info.lastx = Ev.getx (); info.lasty = Ev.gety (); View.settag (info);d Efault:break;} return true;});} public static interface Onmovelistener {public void onmoved ();}}

Android Blur Demo sample-renderscript-attached with code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.