Android技术——视图切换(一)~(四)项目的源代码在:https://github.com/YongYuIT/MeiNv_Liulanqi
上文《Android技术——视图切换(三)》实现的图片切换,虽然切换时有动画效果,但是却需要使用按钮才能切换。这个实例中,将尝试用手势识别代替按钮来实现图片切换。
这个实例也是基于前三篇文章里的项目添加而来的。
/MeiNv_Liulanqi/res/layout/activity_view_switcher_huadong.xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- 定义一个ViewSwitcher组件 -->
<ViewSwitcher
android:id="@+id/viewSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<!-- 定义滚动到上一屏的按钮 -->
</RelativeLayout>
/MeiNv_Liulanqi/src/com/example/meinv_liulanqi/ViewSwitcherHuadongActivity.java文件:
package com.example.meinv_liulanqi;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher;
import android.widget.ViewSwitcher.ViewFactory;
public class ViewSwitcherHuadongActivity extends Activity
{
private int screenNo = -1;
private int screenNum;
public ViewSwitcher switcher;
public int[] img_ids;
private GestureDetector myDesDet;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_switcher_huadong);
// GestureDetector用于从OnTouchListener的onTouch函数中接收数据由此判别手势,
// 识别出手势后,由OnGestureListener响应。
myOnGestureListener listener = new myOnGestureListener(this);
myDesDet = new GestureDetector(listener);
img_ids = new int[] { R.drawable.linzhiling, R.drawable.liuyan,
R.drawable.yangmi };
screenNum = img_ids.length;
// 为ViewSwitcher提供视图工厂
switcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
myViewFactory factory = new myViewFactory(this.getLayoutInflater(),
myDesDet);
switcher.setFactory(factory);
// 初始化
getNext(switcher, img_ids);
}
public void getNext(ViewSwitcher _switcher, int[] _img_ids)
{
if (screenNo < screenNum - 1)
{
screenNo++;
// 设置视图切换的动画效果
_switcher.setInAnimation(ViewSwitcherHuadongActivity.this,
R.anim.slide_in_right);
_switcher.setOutAnimation(ViewSwitcherHuadongActivity.this,
R.anim.slide_out_left);
// 获取下一个视图的实例
LinearLayout lil = (LinearLayout) _switcher.getNextView();
ImageView img = (ImageView) lil.findViewById(R.id.img_meinv);
img.setImageResource(_img_ids[screenNo]);
// 切换视图
_switcher.showNext();
}
}
public void getPrev(ViewSwitcher _switcher, int[] _img_ids)
{
if (screenNo > 0)
{
screenNo--;
// 设置视图切换的动画效果
_switcher.setInAnimation(ViewSwitcherHuadongActivity.this,
R.anim.slide_in_lef);
_switcher.setOutAnimation(ViewSwitcherHuadongActivity.this,
R.anim.slide_out_right);
// 获取下一个视图的实例
LinearLayout lil = (LinearLayout) _switcher.getNextView();
ImageView img = (ImageView) lil.findViewById(R.id.img_meinv);
img.setImageResource(_img_ids[screenNo]);
// 切换视图
_switcher.showPrevious();
}
}
class myViewFactory implements ViewFactory
{
private LayoutInflater inflater;
private GestureDetector desDet;
public myViewFactory(LayoutInflater _inf, GestureDetector _desDet)
{
inflater = _inf;
desDet = _desDet;
}
@Override
public View makeView()
{
// 提供下一个视图的实例
View v = inflater.inflate(R.layout.fragment_layout, null);
ImageView img = (ImageView) v.findViewById(R.id.img_meinv);
// 为图片注册触摸事件监听器
img.setOnTouchListener(new myOnTouchListener(desDet));
return v;
}
}
// 扩展得到自己的触摸事件监听器
class myOnTouchListener implements OnTouchListener
{
private GestureDetector desDet;
public myOnTouchListener(GestureDetector _desDet)
{
desDet = _desDet;
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
// 将监听数据传到GestureDetector,GestureDetector可以从中判别手势
return desDet.onTouchEvent(arg1);
}
}
}
/MeiNv_Liulanqi/src/com/example/meinv_liulanqi/myOnGestureListener.java文件:
package com.example.meinv_liulanqi;
import java.util.Date;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
public class myOnGestureListener implements OnGestureListener
{
private ViewSwitcherHuadongActivity act;
private static Date lastTime = null;
public myOnGestureListener(ViewSwitcherHuadongActivity a)
{
act = a;
}
@Override
// 第三个参数是:滑动手势在水平方向上的位移量
// 第四个参数是:滑动手势在垂直方向上的位移量
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY)
{
// 获取当前时间
Date curDate = new Date(System.currentTimeMillis());
float shijiancha = 1000;
if (lastTime != null)
{
shijiancha = curDate.getTime() - lastTime.getTime();
}
// 对比两次滑动手势之间的时间间隔。
// 由于对屏幕的一次滑动可能会被GestureDetector识别到好几次手势,所以需要过滤掉时间间隔太短的手势。
if (shijiancha > 500)
{
if (distanceX > 10)
{
act.getNext(act.switcher, act.img_ids);
}
if (distanceX < -10)
{
act.getPrev(act.switcher, act.img_ids);
}
}
lastTime = curDate;
return true;
}
@Override
public boolean onDown(MotionEvent arg0)
{
// TODO Auto-generated method stub
return true;
}
@Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3)
{
// TODO Auto-generated method stub
return true;
}
@Override
public void onLongPress(MotionEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public void onShowPress(MotionEvent arg0)
{
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent arg0)
{
// TODO Auto-generated method stub
return true;
}
}
实现的效果:
美女滑动中...