13
2014
05

Android开发学习:图片左右滑动效果-自动切换图片到最后一张,点击最后这张图片调整到下一个Activity

Android开发学习

图片左右滑动效果,第四种:自动切换图片,点击最后这张图片调整到下一个Activity,使用控件ViewFlipper

新建xml布局文件:activity_main.xml,代码内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ViewFlipper
        android:id="@+id/ViewFlipper1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY" >
    </ViewFlipper>

</LinearLayout>

使用线性布局LinearLayout,资源文件中的图片index1.jpg,index2.jpgindex3.jpgindex4.jpg

实现FourActivity.java代码:

package com.zhengdecai.slidingeffect;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

/**
 * 图片左右滑动效果-自动切换图片到最后一张,点击最后这张图片调整到下一个Activity
 *
 * @author 郑德才
 *
 */
public class FourActivity extends Activity implements OnGestureListener {
 private ViewFlipper flipper;
 private GestureDetector detector;
 private Handler nextHandler;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  detector = new GestureDetector(this);
  flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper1);

  flipper.addView(addTextView(R.drawable.index1));
  flipper.addView(addTextView(R.drawable.index2));
  flipper.addView(addTextView(R.drawable.index3));
  flipper.addView(addTextView(R.drawable.index4));

  Timer timer = new Timer();
  timer.scheduleAtFixedRate(new TimerTask() {
   @Override
   public void run() {
    // TODO Auto-generated method stub
    nextHandler.sendMessage(new Message());
   }
  }, 2000, 2000);
  nextHandler = new Handler() {
   @Override
   public void handleMessage(Message msg) {
    // TODO Auto-generated method stub
    super.handleMessage(msg);
    flipper.showNext();
   }
  };
 }

 /**
  * 同时判断点击第四张图片时进入首页
  * */
 private View addTextView(int id) {
  ImageView iv = new ImageView(this);
  iv.setImageResource(id);
  if (id == R.drawable.index4) {
   iv.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
     Intent mainIntent = new Intent();
     mainIntent.setClass(FourActivity.this, ThreeActivity.class);
     startActivity(mainIntent);
     FourActivity.this.finish();
    }
   });
  }
  return iv;
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  // TODO Auto-generated method stub
  return this.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() > 100) {
   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() < -100) {
   this.flipper.setInAnimation(AnimationUtils.loadAnimation(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-generated 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;
 }

}

运行效果图:

« 上一篇下一篇 »

评论列表:

1.雪龙  2014/5/14 17:53:56 回复该留言
学习了!!!

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。