VIEwFlipper和VIEwPager挺像的,都是一个vIEw容器。内部可以添加多个vIEw,只是vIEwpager可以通过左右滑动来切换vIEw,而vIEwFlipper则没有这个功能,所以需要在它上面监听手势。比较方便的是它不用使用适配器就能添加vIEw,所以比较方便。
首先在布局文件中定义这个控件:
<?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,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,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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android ViewFlipper简单用法解析全部内容,希望文章能够帮你解决Android ViewFlipper简单用法解析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)