Android advanced: Allows android to Display photos and animations. The effects are similar to gallery gestures.

Source: Internet
Author: User

The camera software comes with the Android system. His photo view is a galery first. After you click it to view it, it is similar to this effect. The experience is quite good. I made a demo Based on the effect.



First, check the effect:

 


 

 

Then the sliding animation effect is added.

 

See Implementation: flingimagedemo

 

The main implementation is custom view.

 

Import android. content. context; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. canvas; <br/> Import android. graphics. color; <br/> Import android. graphics. paint; <br/> Import android. graphics. rect; <br/> Import android. util. attributeset; <br/> Import android. view. view; <br/> Import android. view. animation. animation; <br/> Import android. view. animation. linearinterpolator; <br/> Import Andro Id. view. animation. transformation; <br/>/** <br/> * view photos <br/> */<br/> public class flingview extends view {</P> <p> private bitmap bitmap; <br/> private bitmap nbitmap; <br/> private bitmap fbitmap; </P> <p> Public int offsetx = 0; <br/> Public int offsety = 0; </P> <p> Public static int postion = 0; <br/> int mlastflingx = 0; <br/> Boolean offsetright = false; </P> <p> private bitmap [] bitmaps; </P> <p> Public Flingview (context, attributeset attrs) {<br/> super (context, attrs ); </P> <p >}</P> <p> Public flingview (context, bitmap [] bitmaps) {<br/> super (context ); <br/> This. bitmaps = bitmaps; <br/> bitmap = getbitmap (0); <br/> nbitmap = getbitmap (1 ); <br/>}</P> <p> @ override <br/> Public void draw (canvas) {<br/> paint P = new paint (); <br/> canvas. drawcolor (color. black); <br/> If (offsetx <0) {< Br/> If (nbitmap! = NULL) {<br/> rect recttemp = new rect (flingimagedemo. screen_width + 15 + offsetx, 0, flingimagedemo. screen_width + 15 + offsetx + flingimagedemo. screen_width, flingimagedemo. screen_height); <br/> canvas. drawbitmap (nbitmap, null, recttemp, P); <br/>}< br/>} else if (offsetx> 0) {<br/> If (fbitmap! = NULL) {<br/> rect recttemp = new rect (-flingimagedemo. SCREEN_WIDTH-15 + offsetx, 0,-flingimagedemo. SCREEN_WIDTH-15 + offsetx + flingimagedemo. screen_width, flingimagedemo. screen_height); <br/> canvas. drawbitmap (fbitmap, null, recttemp, P); <br/>}< br/> If (Bitmap! = NULL) {<br/> rect recttemp = new rect (offsetx, offsety, offsetx + flingimagedemo. screen_width, offsety + flingimagedemo. screen_height); <br/> canvas. drawbitmap (bitmap, null, recttemp, P); <br/>}</P> <p> Public void handlescroll (INT deltax) {<br/> If (deltax> 0) {<br/> offsetx-=-deltax; <br/>} else {<br/> offsetx + = deltax; <br/>}< br/> invalidate (); <br/>}</P> <p> // you can switch to the next <br/> Boolean flag = fa LSE; <br/> // marked as needing to slide right <br/> Boolean flag1 = false; <br/> // marked as sliding left <br/> Boolean flag2 = false; </P> <p> class myanimation extends animation {</P> <p> private int temp; </P> <p> @ override <br/> Public void initialize (INT width, int height, int parentwidth, <br/> int parentheight) {<br/> temp = offsetx; <br/> super. initialize (width, height, parentwidth, parentheight); <br/> setduration (500); <br/> setfillafter (tr UE); <br/> setinterpolator (New linearinterpolator (); <br/>}</P> <p> @ override <br/> protected void applytransformation (float interpolatedtime, <br/> transformation T) {<br/> // log. I ("BB", "offsetx =>" + offsetx); <br/> // when you need to slide the image, change the offsetx size in the direction. <br/> If (FLAG) {<br/> If (temp> 0) {<br/> offsetx = (INT) (flingimagedemo. SCREEN_WIDTH-temp) * interpolatedtime + temp); <br/>} else {<br/> offsetx = (INT) (-flingimagedem O. SCREEN_WIDTH-temp) * interpolatedtime + temp); <br/>}< br/> // no need to change the case <br/>} else {<br/> offsetx = (INT) (temp * (1-interpolatedtime); <br/>}</P> <p> invalidate (); <br/>}< br/> // do some work after the animation ends <br/> @ override <br/> protected void onanimationend () {<br/> If (flag1) {<br/> nbitmap = bitmap; <br/> bitmap = fbitmap; <br/> fbitmap = NULL; <br/> postion = postion-1; <br/>} else if (flag2) {<br/> fbitmap = Bi TMAP; <br/> bitmap = nbitmap; <br/> nbitmap = NULL; <br/> postion = postion + 1; <br/>}< br/> flag1 = false; <br/> flag2 = false; <br/> offsetx = 0; <br/> If (fbitmap = NULL & offsetx = 0) {<br/> If (postion> 0) {<br/> fbitmap = getbitmap (postion-1 ); <br/>}</P> <p>} else if (nbitmap = NULL & offsetx = 0) {<br/> If (postion <bitmaps. length-1) {<br/> nbitmap = getbitmap (postion + 1); <br/>}< br/> clear Animation (); <br/> flag = false; <br/>}</P> <p> Public void onfling (INT paramfloat1) {<br/> If (offsetx> flingimagedemo. screen_width/3) {<br/> If (fbitmap! = NULL) {<br/> flag = true; <br/> flag1 = true; <br/>}< br/>} else if (offsetx <-flingimagedemo. screen_width/3) {<br/> If (nbitmap! = NULL) {<br/> flag = true; <br/> flag2 = true; <br/>}< br/> // start animation effect <br/> startanimation (New myanimation (); <br/> invalidate (); </P> <p >}< br/>/** <br/> * obtain the image at the current position. <br/> * @ Param currentpos <br/> * @ return <br/> */<br/> Public bitmap getbitmap (INT currentpos) {<br/> If (currentpos> bitmaps. length-1) {<br/> return NULL; <br/>}< br/> bitmap currbitmap = bitmaps [currentpos]; <br/> offsetx = 0; <br/> offsety = 0; </P> <p> return currbitmap; <br/>}< br/>

 

In the middle, using myanimation to implement Custom Animation mainly overrides the applytransformation method.



As well as some detailed operations on the onanimationend method after the animation ends, the logic is a little messy ~~

 

Create an activity, create a view, and input the image resource.

 

Import android. app. activity; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> Import android. OS. bundle; <br/> Import android. util. displaymetrics; <br/> Import android. view. gesturedetector; <br/> Import android. view. motionevent; <br/> Import android. view. gesturedetector. ongesturelistener; <br/> public class flingimagedemo extends activity implements ongesturelistener {</P> <p> private flingview; </P> <p> private gesturedetector mygesture; </P> <p> Public static int screen_width; <br/> Public static int screen_height; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {</P> <p> displaymetrics dm = new displaymetrics (); <br/> getwindowmanager (). getdefadisplay display (). getmetrics (DM); </P> <p> // obtains the mobile phone's bandwidth and height in pixels. <br/> screen_width = DM. widthpixels; <br/> screen_height = DM. heightpixels; <br/> // do not have less than two bitmap resources <br/> bitmap [] bitmaps ={< br/> bitmapfactory <br/>. decoderesource (getresources (), R. drawable. g1), <br/> bitmapfactory <br/>. decoderesource (getresources (), R. drawable. g3), <br/> bitmapfactory <br/>. decoderesource (getresources (), R. drawable. g4), <br/> // bitmapfactory <br/> //. decoderesource (getresources (), R. drawable. g7), <br/> bitmapfactory <br/>. decoderesource (getresources (), R. drawable. g8) };</P> <p> super. oncreate (savedinstancestate); <br/> flingview = new flingview (this, bitmaps); <br/> setcontentview (flingview); <br/> mygesture = new gesturedetector (this ); <br/>}< br/> @ override <br/> Public Boolean ontouchevent (motionevent event) {</P> <p> switch (event. getaction () {<br/> case motionevent. action_up: <br/> flingview. onfling (0); <br/> break; <br/>}</P> <p> return mygesture. ontouchevent (event); <br/>}< br/> @ override <br/> Public Boolean ondown (motionevent E) {<br/> // todo auto-generated method stub <br/> return false; <br/>}< br/> // call after gesture completion <br/> @ override <br/> Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, <br/> float velocityy) {<br/> flingview. onfling (INT)-velocityx); <br/> return true; <br/>}< br/> @ override <br/> Public void onlongpress (motionevent E) {<br/> // todo auto-generated method stub </P> <p >}< br/> // The sliding process is always called. <br/> @ override <br /> Public Boolean onscroll (motionevent E1, motionevent E2, float distancex, <br/> float distancey) {<br/> flingview. handlescroll (-1 * (INT) distancex); <br/> return true; <br/>}< br/> @ override <br/> Public void onshowpress (motionevent E) {<br/> // todo auto-generated method stub </P> <p >}< br/> @ override <br/> Public Boolean onsingletapup (motionevent E) {<br/> // todo auto-generated method stub <br/> return false; <br/>}< br/>

 

The ongesturelistener interface is called in Methods onfling and onscroll respectively.

 

Just add this function later.

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.