Android ViewFlipper简单应用

Android ViewFlipper 简单应用,废话不多说,直接看代码

activity_guide.xml




  
  



GuideActivity.java

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class GuideActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {

  private GestureDetector detector;
  private ViewFlipper flipper;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_guide);
    //隐藏标题栏
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();

    detector = new GestureDetector(this);
    flipper = (ViewFlipper) this.findViewById(R.id.Main_ViewFlipper);
    flipper.addView(addImageView(R.mipmap.guide1));
    flipper.addView(addImageView(R.mipmap.guide2));
    flipper.addView(addImageView(R.mipmap.guide3)); 
  }

  private View addImageView(int id) {
    ImageView iv = new ImageView(this); 
    //ImageView拉伸,充满整个ViewFlipper
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
    iv.setImageResource(id);
    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() > 120) {
      this.flipper.showNext();
    }
    if (e1.getX() - e2.getX() < -120) {
      this.flipper.showPrevious();
    }
    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;
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Android ViewFlipper简单应用)