欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android ViewFlipper简单用法解析

程序员文章站 2024-03-06 08:01:19
viewflipper和viewpager挺像的,都是一个view容器。内部可以添加多个view,只是viewpager可以通过左右滑动来切换view,而viewflipp...

viewflipper和viewpager挺像的,都是一个view容器。内部可以添加多个view,只是viewpager可以通过左右滑动来切换view,而viewflipper则没有这个功能,所以需要在它上面监听手势。比较方便的是它不用使用适配器就能添加view,所以比较方便。

 Android ViewFlipper简单用法解析

首先在布局文件中定义这个控件: 

<?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"
  android:background="#ffffff">
  
  <viewflipper android:id="@+id/viewflipper01"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/>
  
</linearlayout>

2.在代码中找到并且添加view 

  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);

    detector = new gesturedetector(this);
    flipper = (viewflipper) this.findviewbyid(r.id.viewflipper01);

    flipper.addview(addtextview("page 1"));//添加view到flipper中,这里添加5个textview
    flipper.addview(addtextview("page 2"));
    flipper.addview(addtextview("page 3"));
    flipper.addview(addtextview("page 4"));
    flipper.addview(addtextview("page 5"));
  }


/**
   * @param text
   * @return 一个textview对象
   */
  private view addtextview(string text) {
    textview tv = new textview(this);
    tv.settext(text);
    tv.settextsize(40);
    tv.settextcolor(0xff000000);
    tv.setgravity(gravity.center);
    return tv;
  }

3.通过touch监听手势来切换view 

@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();//切换到下一个view
      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();
      return true;
    }
    return false;
  }

下面是activity的完整代码: 

package com.xu81.testflip;

import android.app.activity;
import android.app.activitymanager.recenttaskinfo;
import android.os.bundle;
import android.view.gesturedetector;
import android.view.gravity;
import android.view.motionevent;
import android.view.view;
import android.view.gesturedetector.ongesturelistener;
import android.view.animation.animationutils;
import android.widget.textview;
import android.widget.viewflipper;

public class testflip extends activity implements ongesturelistener {

  private viewflipper flipper;

  private gesturedetector detector;

  /** called when the activity is first created. */
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);

    detector = new gesturedetector(this);
    flipper = (viewflipper) this.findviewbyid(r.id.viewflipper01);

    flipper.addview(addtextview("page 1"));//添加view到flipper中,这里添加5个textview
    flipper.addview(addtextview("page 2"));
    flipper.addview(addtextview("page 3"));
    flipper.addview(addtextview("page 4"));
    flipper.addview(addtextview("page 5"));
  }

  /**
   * @param text
   * @return 一个textview对象
   */
  private view addtextview(string text) {
    textview tv = new textview(this);
    tv.settext(text);
    tv.settextsize(40);
    tv.settextcolor(0xff000000);
    tv.setgravity(gravity.center);
    return tv;
  }

  @override
  public boolean ontouchevent(motionevent event) {
    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.setinanimation(animationutils.loadanimation(this, r.anim.push_left_in));
      this.flipper.setoutanimation(animationutils.loadanimation(this, r.anim.push_left_out));
      this.flipper.shownext();//切换到下一个view
      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();
      return true;
    }
    return false;
  }

  @override
  public void onlongpress(motionevent e) {
    // todo 自动生成的方法存根
    
  }

  @override
  public boolean onscroll(motionevent e1, motionevent e2, float distancex,
      float distancey) {
    // todo 自动生成的方法存根
    return false;
  }

  @override
  public void onshowpress(motionevent e) {
    // todo 自动生成的方法存根
    
  }

  @override
  public boolean onsingletapup(motionevent e) {
    // todo 自动生成的方法存根
    return false;
  }


}

动画文件: 

push_left_in.xml 

<?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="500" />
  <alpha android:fromalpha="0.1" android:toalpha="1.0"
    android:duration="500" />
</set>

push_left_out.xml 

<?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="500" />
  <alpha android:fromalpha="1.0" android:toalpha="0.1"
    android:duration="500" />
</set>

push_right_in.xml 

<?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="500" />
  <alpha android:fromalpha="0.1" android:toalpha="1.0"
    android:duration="500" />
</set>

push_right_out.xml 

<?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="500" />
  <alpha android:fromalpha="1.0" android:toalpha="0.1"
    android:duration="500" />
</set>

源码下载:http://xiazai.jb51.net/201609/yuanma/androidviewflip(jb51.net).rar

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