Android to achieve picture left and right sliding effect _android

Source: Internet
Author: User
Tags stub

As for the sliding effect, which is used more in Android, the slide effect of this example is implemented using Viewflipper, and of course it can be implemented using other view. Then let's start to achieve this effect.

Let's take a look at the program Structure chart:

Mainactivity code in File:

Package com.android.flip;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.GestureDetector;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.GestureDetector.OnGestureListener;
Import Android.view.animation.AnimationUtils;
Import Android.widget.ImageView;

Import Android.widget.ViewFlipper; /** * Android to achieve left and right sliding effect * @Description: Android to achieve left and right sliding effect * @File: Mainactivity.java * @Package Com.android.flip * @Au  Thor Hanyonglu * @Date 2012-02-12 a.m. 10:44:04 * @Version V1.0 * * * public class Mainactivity extends activity implements
  Ongesturelistener {private Viewflipper flipper;
  
  Private Gesturedetector detector; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    
    Setcontentview (R.layout.main);
    detector = new Gesturedetector (this);

    Flipper = (viewflipper) This.findviewbyid (r.id.viewflipper1); Flipper. AddView (Addimageview (R.drawable.one));
    Flipper.addview (Addimageview (r.drawable.two));
    Flipper.addview (Addimageview (R.drawable.three));
    Flipper.addview (Addimageview (R.drawable.four));
  Flipper.addview (Addimageview (r.drawable.five));
    Private View addimageview (int id) {ImageView IV = new ImageView (this);
    Iv.setimageresource (ID);
  return IV; @Override public boolean ontouchevent (Motionevent event) {//TODO auto-generated
  . Detector.ontouchevent (event);
  @Override public boolean Ondown (Motionevent e) {//TODO auto-generated method stub return false;  @Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {if (E1.getx ()-e2.getx () > 120)
      {This.flipper.setInAnimation (Animationutils.loadanimation (this, r.anim.push_left_in));
     This.flipper.setOutAnimation (Animationutils.loadanimation (this, r.anim.push_left_out)); This.flipper.showNext ();
    return true; else if (E1.getx ()-E2.getx () < -120) {this.flipper.setInAnimation (this, R.anim.
      push_right_in));
      This.flipper.setOutAnimation (Animationutils.loadanimation (this, r.anim.push_right_out));
      This.flipper.showPrevious ();
    return true;
  return false; 
  @Override public void onlongpress (Motionevent e) {//TODO auto-generated method stub} @Override public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey) {//TODO Auto-gene
  Rated method stub return false; 
  @Override public void onshowpress (Motionevent e) {//TODO auto-generated method stub} @Override
  public boolean onsingletapup (Motionevent e) {//TODO auto-generated method stub return false;
 }
}

The layout interface is relatively simple, we only need to join Viewflipper, the code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android "
  android:orientation=" vertical "
  android:layout_width=" fill_parent "
  android:layout_" height= "Fill_parent"
  >

  <viewflipper android:id= "@+id/viewflipper1" android:layout_width= "
    fill _parent " 
    android:layout_height=" fill_parent ">
  </ViewFlipper>
</LinearLayout>

To make it slide with certain effects, we need to add animation effect, speaking of animation, let's look at how to implement custom Animation in Android. Custom animation are defined in XML format, and the well-defined XML files are stored in Res/anim.

The general animation has the following four kinds of types:

    1. Alpha: Gradient Transparency Animation effect
    2. Scale: Gradient Size Telescopic animation effect
    3. Translate: Moving the animation effect in the position of the picture transformation
    4. Rotate: Moving the animation effect in the position of the picture transformation

Push_left_in.xml code in File:

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android ">
  <translate android:fromxdelta=" "100%p" android:toxdelta= "0"
    android:duration= "/>"
  <alpha android:fromalpha= "0.1" android:toalpha= "1.0" android:duration= "/>"
</set>
push_left_out.xml File code: 
<?xml version= "1.0" encoding= "Utf-8"?> <set "xmlns:android=" http:// 
Schemas.android.com/apk/res/android ">
  <translate android:fromxdelta=" 0 "android:toxdelta=" -100%p "
    android:duration= "/>" <alpha android:fromalpha= "1.0" android:toalpha= "0.1"
    android: Duration= "/>"
</set>

Push_right_in.xml code in File:

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android ">
  <translate android:fromxdelta=" " -100%p" android:toxdelta= "0"
    android:duration= "/>"
  <alpha android:fromalpha= "0.1" android:toalpha= "1.0" android:duration= "/>"
</set>

Push_right_out.xml code in File:

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android ">
  <translate android:fromxdelta=" "0" android:toxdelta= "100%p" android:duration= "/>"
  <alpha android:fromalpha= "1.0" android:toalpha= "0.1" android:duration= "/>"
</set>

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.