Android Blur example-renderscript-with effect diagram and code

Source: Internet
Author: User
Tags gety

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

Reference 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.

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 topmost layer, is the red area, this layer is a clear picture.

public static class Placeholderfragment extends Fragment {//new Android Adt-bundle by default in activity with a Fragment, supposedly Android Stdio long ago. 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 and updates the blur layer when the slider position changes. The delay is to ensure that the View.getx,view.getwidth method can go to the value,This is just for initialization, so deferred execution is better. If you want to read WEIDHT and x every time you visualize it, 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 directly with the 10px, is actually 5dip, on my phone Galaxy Nexus, 1dip=2px, actually should be converted to。 return true; This is a reference to the article in the request, did not try 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, and then the cropped image is obfuscated based on radius, and the obfuscated image is 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);                        This is where the image is drawn directly at the origin of the new coordinate system, and if the coordinate system is not set, it is equivalent to drawing the image on (View.getx (), view.gety), and Android to the right is the x-axis square, and the y-axis in the downward direction. The process of blurring a picture renderscript rs = renderscript.create (getaCtivity ()); Renderscript requirements APILevel 17, this is more disgusting, V8 support package is not particularly useful, really want to blur the words, or OPENCV jni to engage 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 several hours has not been approved ... http://download.csdn.net/detail/u011267546/7502603 Note Code MINSDK I set the relatively high, is the API Level17, no way, Renderscript support library did not fix.

4. Several questions

Renderscript Although there is a SUPPORT-V8 support library, but I did the meeting, also 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 oneself to make up relatively good, on-line OPENCV related fuzzy algorithm many. In addition, if the image is large, fuzzy processing is time consuming, preferably asynchronous.

getwidth,getheight,getleft Call Timing OnStart, onreusme These are not, only in Onwindowfoucschange. The example of this article is called at initialization time, so it can be deferred for one execution, and if it is called every time it is switched from the background to the foreground, it is called in Onwindowfoucschange.

using the canvas trim bitmap Note the coordinate system, Android right is the x-axis square, and down the y-axis positive direction.

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);
Allow view to drag
Write a simple method, through the view of the tag+ontouchevent, realize the view can be dragged.

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 ();}}

Related Article

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.