Android使用ViewFlipper和GestrueDetector共同实现滑屏效果实例
作者:Jacob-wj 时间:2023-11-11 10:49:06
本文实例讲述了Android使用ViewFlipper和GestrueDetector共同实现滑屏效果。分享给大家供大家参考,具体如下:
关于GestureDetector的相关知识,前面已经介绍过了,不懂的大家可以去了解一下。
1.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:background="@drawable/bg"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:persistentDrawingCache="animation"
android:flipInterval="1000"
android:inAnimation="@anim/push_left_in"
android:outAnimation="@anim/push_left_out"
>
<include android:id="@+id/lay1" layout="@layout/layout1"/>
<include android:id="@+id/lay2" layout="@layout/layout2"/>
</ViewFlipper>
</LinearLayout>
注:layout1和layout2 布局很简单,就是有一个textview和一个button,就不在这里写出了。其中包含了两个特效xml文件,放在res/anim下
1.push_left_in
<?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.0"
android:toAlpha="1.0"
android:duration="500" />
</set>
2 push_left_out
<?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.0"
android:duration="500" />
</set>
主类:
package com.wj.gesture;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class GestureDetectorTest extends Activity implements OnClickListener, OnTouchListener,OnGestureListener,OnDoubleTapListener{
GestureDetector gestureDetector;
private Button next = null,up = null;
private ViewFlipper flipper = null;
private static final int FLING_MIN_DISTANCE = 100;
private static final int FLING_MIN_VELOCITY = 200;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initview();
setListener();
}
private void initview(){
gestureDetector = new GestureDetector(this);
next = (Button)this.findViewById(R.id.button1);
up = (Button)this.findViewById(R.id.button2);
next.setOnClickListener(this);
up.setOnClickListener(this);
flipper = (ViewFlipper)this.findViewById(R.id.viewFlipper1);
flipper.setLongClickable(true);
}
private void setListener(){
flipper.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
//Toast.makeText(this, "ontouch", 1000).show();
return gestureDetector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// 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;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX()-e2.getX()>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY) {
flipper.setInAnimation(inFromRightAnimation());
flipper.setOutAnimation(outToLeftAnimation());
flipper.showNext();
}else if (e2.getX()-e1.getX()>FLING_MIN_DISTANCE&&Math.abs(velocityX) > FLING_MIN_VELOCITY) {
flipper.setInAnimation(inFromLeftAnimation());
flipper.setOutAnimation(outToRightAnimation());
flipper.showPrevious();
}
return false;
}
protected Animation inFromRightAnimation() {
Animation inFromRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromRight.setDuration(500);
inFromRight.setInterpolator(new AccelerateInterpolator());
return inFromRight;
}
protected Animation outToLeftAnimation() {
Animation outtoLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoLeft.setDuration(500);
outtoLeft.setInterpolator(new AccelerateInterpolator());
return outtoLeft;
}
protected Animation inFromLeftAnimation() {
Animation inFromLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
inFromLeft.setDuration(500);
inFromLeft.setInterpolator(new AccelerateInterpolator());
return inFromLeft;
}
protected Animation outToRightAnimation() {
Animation outtoRight = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, +1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outtoRight.setDuration(500);
outtoRight.setInterpolator(new AccelerateInterpolator());
return outtoRight;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onClick(View v) {
if (v==next) {
flipper.showNext();
}else{
flipper.showPrevious();
}
}
}
希望本文所述对大家Android程序设计有所帮助。
标签:Android,ViewFlipper,GestrueDetector
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android ListView和Adapter数据适配器的简单介绍
2023-04-16 04:07:12
![](https://img.aspxhome.com/file/2023/4/97854_0s.png)
Java编程常见内存溢出异常与代码示例
2021-12-14 14:45:08
C#中的委托和事件详解
2022-01-20 01:31:53
![](https://img.aspxhome.com/file/2023/2/113702_0s.png)
Java访问Hadoop分布式文件系统HDFS的配置说明
2021-11-20 05:53:42
适用于WebForm Mvc的Pager分页组件C#实现
2022-05-11 22:11:34
Android中ProgressBar用法简单实例
2022-09-21 05:27:55
浅谈spring security入门
2023-02-20 04:48:58
![](https://img.aspxhome.com/file/2023/2/63782_0s.png)
Java 动态模拟操作系统进程调度算法
2023-04-10 15:09:20
![](https://img.aspxhome.com/file/2023/9/125669_0s.png)
C# PictureBox图片控件实现图片交换
2022-12-05 17:09:10
![](https://img.aspxhome.com/file/2023/5/126025_0s.png)
C# 线程同步详解
2021-12-30 04:50:03
![](https://img.aspxhome.com/file/2023/0/94630_0s.png)
Android解决viewpager嵌套滑动冲突并保留侧滑菜单功能
2023-09-05 11:36:33
Java开发环境配置方法
2022-05-25 02:02:29
![](https://img.aspxhome.com/file/2023/0/69340_0s.jpg)
C# 多网卡 Server Listen
2022-05-30 16:09:18
java如何实现自动生成数据库设计文档
2023-08-07 19:01:28
![](https://img.aspxhome.com/file/2023/3/61323_0s.jpg)
Mybatis之#{}与${}的区别使用详解
2023-03-24 01:55:21
Spring的@Value如何从Nacos配置中心获取值并自动刷新
2022-03-11 02:17:03
输出的文本实现对齐的方法(超简单)
2023-06-19 03:57:09
![](https://img.aspxhome.com/file/2023/1/66581_0s.jpg)
OkHttp3中默认不保持Cookie的解决方法
2021-10-25 15:20:50
C# 游戏外挂实现核心代码
2021-12-28 14:47:40
详解用RxJava实现事件总线(Event Bus)
2022-02-13 16:43:18
![](https://img.aspxhome.com/file/2023/6/109156_0s.png)