18
2014
05

Android开发学习:图片左右滑动效果-自动播放、加了4个点,同步切换

Android开发学习

图片左右滑动效果,第六种:自动播放、加了4个点,同步切换,使用控件ViewFlipper

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

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ViewFlipper
        android:id="@+id/viewflipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <LinearLayout
        android:id="@+id/ll_dot"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:paddingBottom="30dp" >

        <ImageView
            android:id="@+id/iv_1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginLeft="0dp"
            android:clickable="true"
            android:contentDescription="@string/dot"
            android:src="@layout/dot" />

        <ImageView
            android:id="@+id/iv_2"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginLeft="5dp"
            android:clickable="true"
            android:contentDescription="@string/dot"
            android:src="@layout/dot" />

        <ImageView
            android:id="@+id/iv_3"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginLeft="5dp"
            android:contentDescription="@string/dot"
            android:src="@layout/dot" />

        <ImageView
            android:id="@+id/iv_4"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginLeft="5dp"
            android:contentDescription="@string/dot"
            android:src="@layout/dot" />
    </LinearLayout>

</FrameLayout>

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

实现SixActivity.java代码:

package com.zhengdecai.slidingeffect;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
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.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

/**
 * 图片左右滑动效果-自动播放、加了4个点,同步切换
 *
 * @author 郑德才
 *
 */
public class SixActivity extends Activity implements OnGestureListener {
 private int[] imgID = { R.drawable.index1, R.drawable.index2,
   R.drawable.index3, R.drawable.index4 };
 private int[] ivID = { R.id.iv_1, R.id.iv_2, R.id.iv_3, R.id.iv_4 };
 private List<ImageView> ivs = new ArrayList<ImageView>();
 private ViewFlipper flipper;
 private GestureDetector detector;
 private Handler nextHandler;

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

  for (int i = 0; i < ivID.length; i++) {
   ImageView im_iv = (ImageView) findViewById(ivID[i]);
   ivs.add(im_iv);
  }

  detector = new GestureDetector(this);
  flipper = (ViewFlipper) findViewById(R.id.viewflipper);
  for (int i = 0; i < imgID.length; i++) {
   ImageView imageView = new ImageView(this);
   imageView.setImageResource(imgID[i]);
   imageView.setScaleType(ImageView.ScaleType.FIT_XY);
   flipper.addView(imageView, new LayoutParams(
     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  }
  ivs.get(0).setEnabled(false);

  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();
    dotChange(flipper.getDisplayedChild());
   }
  };
 }

 private void dotChange(int index) {
  for (int i = 0; i < ivs.size(); i++) {
   if (i == index) {
    ivs.get(i).setEnabled(false);
   } else {
    ivs.get(i).setEnabled(true);
   }
  }
 }

 @Override
 public boolean onDown(MotionEvent e) {
  return false;
 }

 @Override
 public void onShowPress(MotionEvent e) {

 }

 @Override
 public boolean onSingleTapUp(MotionEvent e) {
  return false;
 }

 @Override
 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
   float distanceY) {
  return false;
 }

 @Override
 public void onLongPress(MotionEvent e) {

 }

 @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));
   View view = flipper.getChildAt(imgID.length - 1);
   View view1 = flipper.getCurrentView();
   // flipper.getDisplayedChild();
   this.flipper.showNext();
   dotChange(flipper.getDisplayedChild());
   return true;
  }// 从右向左滑动
  else if (e1.getX() - e2.getX() < -120) {
   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();
   dotChange(flipper.getDisplayedChild());

   return true;
  }
  return true;
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  return this.detector.onTouchEvent(event);
 }

}

模拟器运行效果:

« 上一篇下一篇 »

评论列表:

1.皇冠现金网  2014/6/10 15:33:32 回复该留言
图片左右滑动效果-自动播放、加了4个点,同步切换

发表评论:

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