Android Gaussian algorithm in "on the Road" APP implementation

Source: Internet
Author: User
Tags gety

Reprint please indicate source: http://blog.csdn.net/yianemail/article/details/47019383

One: We can see the effect of the Gaussian blur of the app "on the Road", and the image will become clearer and bigger when pulled down.

Automatically restores the Blur effect when released.

Effect analysis.

I began to think directly with the Gaussian Blur image processing, in the image of the continuous pull down the process of the image of the Gaussian processing to achieve real-time to the image of Gaussian blur, to the bottom of the picture to get the clear, in the process of pulling, the image of the blur also changes.

However, the Gaussian Blur image is a time-consuming process. So the above processing will be very card. However, in the "On the road" this app does not have a card phenomenon.

So I think the realization of the idea is:

1, the layout has two overlapping imageview (none specified src). (This layout is the head layout of the ListView as the main layout)

2, the code implements the Gaussian processing of the picture. Also to the layout two imageview do setimagebitmap. The blurred picture is covered with a clear picture.  

3, to blur the picture do Setalpha and do the amplification process.

Two: Main code processing:

Package Com.roger.listimgdemo;import Android.content.context;import Android.content.res.typedarray;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.matrix;import Android.graphics.pointf;import Android.os.handler;import Android.os.message;import Android.util.AttributeSet; Import Android.util.displaymetrics;import Android.view.layoutinflater;import Android.view.motionevent;import Android.view.view;import Android.view.windowmanager;import Android.widget.imageview;import Android.widget.listadapter;import Android.widget.listview;import Android.widget.relativelayout;import com.roger.listimgdemo.r;/** * ListView head Picture drop-down magnification, use must call method Setimageid or Setimagebitmap set picture otherwise error Created by Roger * on  14-4-30. */public class Imglistview extends ListView {float mlasty;private static final int back_scale = 0;private Boolean Ishavehead = false;//If the head has a picture private float ScaleY = 0;private Boolean isbacking = false;//is in the rebound state private int Displaywid Th;private Context McontexT;private Bitmap bmp;private View headerview;private ImageView imageview;/** used to record the coordinates where the dragged picture was moved */private matrix matrix = new Matrix ();/** is used to record the coordinates where the picture is to be dragged */private matrix Currentmatrix = new Matrix ();p rivate matrix Defaultmatrix = new Matrix () ;p rivate float imgheight, imgwidth;/** record is drag photo mode or zoom out photo mode 0: Drag mode, 1: zoom */private int mode = 0;//Initial state/** drag photo mode */private Final int mode_drag = 1;/** used to record the coordinate position at the beginning */private PointF startPoint = new PointF ();p rivate int mimageid;private attribut ESet attrs;private ImageView mohu;private float alpha;public imglistview (context context) {super (context); This.mcontext = Context;initview ();} Public Imglistview (context context, AttributeSet Attrs) {Super (context, attrs); this.mcontext = Context;this.attrs = Attrs;initview ();} Public Imglistview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); this.mcontext = Co Ntext;this.attrs = Attrs;initview ();} public void Setadapter (ListAdapter adapter) {Super.setadapter (adapter);} Public void Addheaderview (View v) {Super.addheaderview (v);} public void Setimageid (int id) {This.mimageid = Id;bmp = Bitmapfactory.decoderesource (Getresources (), Mimageid); Ishavehead) This.removeheaderview (Headerview); Inithead ();} public void Setimagebitmap (Bitmap bit) {this.bmp = Bit;if (ishavehead) This.removeheaderview (Headerview); Inithead ();} /** * Initialize picture */private void Initview () {/* Get screen resolution size */displaymetrics DM = new Displaymetrics (); WindowManager mWm = (windowmanager) mcontext.getsystemservice (Context.window_service); Mwm.getdefaultdisplay (). Getmetrics (DM);d isplaywidth = dm.widthpixels; TypedArray a = mcontext.obtainstyledattributes (Attrs,r.styleable.imglistview); Mimageid = A.getresourceid ( R.styleable.imglistview_headimage, 0); a.recycle (); if (null = = BMP && Mimageid! = 0) {BMP = Bitmapfactory.decodere Source (Getresources (), Mimageid); Inithead ();}} private void Inithead () {/** * Gets the head layout */layoutinflater inflater = Layoutinflater.from (mcontext); Headerview = Inflater.inf Late (r.layout.tOP_IMG, NULL);/** * Find picture control */imageview = (ImageView) Headerview.findviewbyid (R.id.qingxi); Mohu = (ImageView) Headerview.findviewbyid (R.id.mohu);/** * blur the picture */bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.C); Bitmap bit = Blurbitmap.boxblurfilter (BITMAP);/** * Set Picture */imageview.setimagebitmap (Bitmap); Mohu.setimagebitmap (bit) Float scale = (float) displaywidth/(float) bmp.getwidth ();//1080/1800matrix.postscale (scale, scale, 0, 0); Mohu.setima Gematrix (Matrix);d efaultmatrix.set (matrix);/** * This side is the width and height of the child view to be set */imgheight = scale * Bmp.getheight (); imgwidth = Scale * bmp.getwidth ();/** * Relativelayout.layoutparams Specifies the height and width of the child view this side to note, because there are two sub-view settings two times */ Relativelayout.layoutparams relativelayouts = new Relativelayout.layoutparams ((int) imgwidth, (int) imgHeight); Imageview.setlayoutparams (relativelayouts); Relativelayout.layoutparams relativelayout = new Relativelayout.layoutparams ((int) imgwidth, (int) imgHeight); Mohu.setlayoutparams (relativelayout); This.addheadervIew (headerview); ishavehead = true;} /** * Swipe down to make the picture larger * * @param event * @return */public boolean ontouchevent (Motionevent event) {if (!ishavehead) {//No head picture re Turn super.ontouchevent (event);} Switch (event.getaction () & Motionevent.action_mask) {//Finger pressed down screen case MotionEvent.ACTION_DOWN:if (isbacking) {return Super.ontouchevent (event);} int[] location = new Int[2];mohu.getlocationinwindow, if (Location[1] >= 0) {mode = mode_drag;//record ImageView when Previous move Position mlasty = Event.gety (); Currentmatrix.set (Imageview.getimagematrix ()); Startpoint.set (Event.getx (), Event.getY ());} break;//the finger moves on the screen, the event is constantly triggered case motionevent.action_move:/** * <=300: Specifies that only the image area is allowed to slide */if (event.gety () <=) {F  Loat dy = event.gety ()-startpoint.y;if (Dy > 0) {Float y = event.gety (); float Alphadelt = (mlasty-y)/1000;alpha = Mohu.getalpha () + alphadelt;if (Alpha > 1.0) {alpha = 1.0f;} else if (Alpha < 0.0) {alpha = 0.0f;} Mohu.setalpha (Alpha); if (mode = = Mode_drag) {Float dx = event.getx ()-StartpoInt.x; Get the X-axis moving distance if (DY/2 + imgheight <= 1.5 * imgheight) {matrix.set (Currentmatrix); float scale = (DY/2 + imgheight)/ (imgheight);//Get the Scaling multiplier if (Scale > 1.2) {scales = (float) 1.2;scaley = dy; Relativelayout.layoutparams relativelayout = new Relativelayout.layoutparams ((int) (scale * imgwidth), (int.) (Scale * ImgHeight)); Mohu.setlayoutparams (relativelayout); Matrix.postscale (scale, scale, IMGWIDTH/2, 0); Mohu.setimagematrix (matrix);} If (scale <= 1.2) {ScaleY = dy; Relativelayout.layoutparams Relativelayouta = new Relativelayout.layoutparams ((int) (scale * imgwidth), (int.) (Scale * ImgHeight)); Imageview.setlayoutparams (Relativelayouta); Matrix.postscale (scaled, scale, IMGWIDTH/2, 0);/** * Zoom Out * *        Imageview.setimagematrix (matrix); Relativelayout.layoutparams relativelayout = new Relativelayout.layoutparams ((int) (scale * imgwidth), (int.) (Scale * ImgHeight)); Mohu.setlayoutparams (relativelayout); Matrix.postscale (scaled, scale, IMGWIDTH/2, 0);/** * Zoom Out * * Mohu.setimagematriX (Matrix);}}}} break;//finger leave screen case motionevent.action_up://when contact leaves screen, picture restores Mhandler.sendemptymessage (Back_scale); case MotionEvent.ACTION_POINTER_UP:mode = 0;break;} Return Super.ontouchevent (event);} Private Handler Mhandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {//TODO auto-generated method s Tubswitch (msg.what) {case Back_scale:float scale = (SCALEY/2 + imgheight)/(imgheight);//Get a scaling multiplier if (Scales > 1.2) {s Cale = (float) 1.2;} if (ScaleY > 0) {//Mohu.setalpha (Alpha); Mohu.setalpha (1.0f); isbacking = True;matrix.set (Currentmatrix); Relativelayout.layoutparams Relativelayouta = new Relativelayout.layoutparams ((int) (scale * imgwidth), (int.) (Scale * IM Gheight)); Imageview.setlayoutparams (Relativelayouta); Matrix.postscale (scale, scale, IMGWIDTH/2, 0); Imageview.setimagematrix (matrix); Relativelayout.layoutparams relativelayout = new Relativelayout.layoutparams ((int) (scale * imgwidth), (int.) (Scale * img) Height)); Mohu.setlayoutparams (relativelayout); matrix.postsCale (scale, scale, IMGWIDTH/2, 0); Mohu.setimagematrix (matrix); ScaleY = (float) (SCALEY/2-1); Mhandler.sendemptymessag Edelayed (Back_scale, 20);} else {ScaleY = 0; Relativelayout.layoutparams Relativelayouta = new Relativelayout.layoutparams ((int) (scale * imgwidth), (int.) (Scale * IM Gheight)); Imageview.setlayoutparams (Relativelayouta); Matrix.set (Defaultmatrix); Imageview.setimagematrix (Matrix ); Relativelayout.layoutparams relativelayout = new Relativelayout.layoutparams ((int) imgwidth, (int) imgHeight); Mohu.setlayoutparams (relativelayout); Matrix.set (Defaultmatrix); Mohu.setimagematrix (matrix); isBacking = false;} Break;default:break;} Super.handlemessage (msg);}};}

Three: Effect realization

Start gradually pull down: clear and big


Eventually become bigger and clearer.



Resources Download: Project Download




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Gaussian algorithm in "on the Road" APP implementation

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.