美文网首页
Android ViewFlipper

Android ViewFlipper

作者: 百里漫步 | 来源:发表于2017-10-21 15:23 被阅读0次

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
 
<ViewFlipper 
    android:id="@+id/flipper"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ></ViewFlipper>    

</LinearLayout>

MainActivity.java

package com.example.android_viewflipper2;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ViewFlipper;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

    private ViewFlipper flipper;
    private float startX;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        flipper = (ViewFlipper) findViewById(R.id.flipper);
        flipper.addView(getImageView(R.drawable.pic1));
        flipper.addView(getImageView(R.drawable.pic2));
        flipper.addView(getImageView(R.drawable.pic3));
        flipper.addView(getImageView(R.drawable.pic4));
        flipper.setBackgroundColor(Color.BLACK);
        //自动滚动轮播图
        // flipper.setFlipInterval(5000);
        // flipper.setInAnimation(this, R.anim.anim_in);
        // flipper.setOutAnimation(this, R.anim.anim_out);
        // flipper.startFlipping();
        // detector=new GestureDetector(this, this);
    }

    private ImageView getImageView(int resId) {
        ImageView image = new ImageView(this);
        image.setBackgroundResource(resId);
        return image;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            startX=event.getX();
            break;
        }
        
        case MotionEvent.ACTION_UP:
        {
            //向右滑动
            if(event.getX()-startX>50)
            {
                //添加动画效果
                 flipper.setInAnimation(this, R.anim.left_in);
                 flipper.setOutAnimation(this, R.anim.left_out);
                 flipper.showNext();
                
            }
            //向左滑动
            if(startX-event.getX()>50)
            {
                 flipper.setInAnimation(this, R.anim.right_in);
                 flipper.setOutAnimation(this, R.anim.right_out);
                 flipper.showPrevious();
            }
            break;
        }
        }

        return super.onTouchEvent(event);

    }

}

动画效果文件:
left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="2000"
        android:fromXDelta="-100%p"
        android:toXDelta="0" />
    <alpha 
        android:fromAlpha="0.5"
        android:toAlpha="1"
        android:duration="2000"
        />

</set>

left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:toXDelta="100%p" />

    <alpha
        android:duration="2000"
        android:fromAlpha="0.5"
        android:toAlpha="1" />

</set>

right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="2000"
        android:fromXDelta="100%p"
        android:toXDelta="0" />

    <alpha
        android:duration="2000"
        android:fromAlpha="0.5"
        android:toAlpha="1" />

</set>

right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />

    <alpha
        android:duration="2000"
        android:fromAlpha="0.5"
        android:toAlpha="1" />

</set>

效果演示:


ViewFlipper.gif

相关文章

网友评论

      本文标题:Android ViewFlipper

      本文链接:https://www.haomeiwen.com/subject/iqviuxtx.html