使用ViewFlipper实现两张图片切换效果,废话不多说,直接上代码。

java源码:

package com.example.viewflipper;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.ViewFlipper; public class MainActivity extends Activity { private ViewFlipper viewflipper;
private float startx,endx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewflipper=(ViewFlipper) findViewById(R.id.viewFlipper1);
} public boolean onTouchEvent(MotionEvent event) {
// TODO 自动生成的方法存根
int action=event.getAction();
if(action==MotionEvent.ACTION_DOWN)
{
startx=event.getX();
}else if(action==MotionEvent.ACTION_UP){
endx=event.getX();
if(startx-endx>){
viewflipper.setInAnimation(this,android.R.anim.fade_in);
viewflipper.setOutAnimation(this,android.R.anim.fade_out);
/*viewflipper.setInAnimation(this,R.layout.in_rightleft);
viewflipper.setOutAnimation(this,R.layout.out_rightleft);*/
viewflipper.showNext();
}else if(endx-startx>){
/*viewflipper.setInAnimation(this,R.layout.in_leftright);
viewflipper.setOutAnimation(this,R.layout.out_leftright);*/
viewflipper.setInAnimation(this,android.R.anim.fade_in);
viewflipper.setOutAnimation(this,android.R.anim.fade_out);
viewflipper.showPrevious();
}
}
return super.onTouchEvent(event);
} }

使用ViewFlipper控件布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/item1"
android:scaleType="centerCrop" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/item2"
android:scaleType="centerCrop" />
</LinearLayout>
</ViewFlipper> </RelativeLayout>

运行ok。

最新文章

  1. 实现简单sed替换功能的python脚本
  2. JSON.stringify////////////////////////////////zzzzzzzzzzzzzz
  3. window 常用快捷键
  4. centos6.5 用户管理
  5. Atitit.论图片类型&#160;垃圾文件的识别与清理&#160;&#160;流程与设计原则&#160;与api概要设计&#160;v2&#160;pbj
  6. Codeforces724D [字符串][乱搞][贪心]
  7. 用VMware 8安装Ubuntu 12.04详细过程(图解)
  8. 【Spring源码分析】原型Bean实例化过程、byName与byType及FactoryBean获取Bean源码实现
  9. 075、配置Virtualbox backend(2019-04-22 周一)
  10. 微信小游戏跳一跳简单手动外挂(基于adb 和 python)
  11. 最全的JS判断是否为中文的方法
  12. day19(乱码解决方案)
  13. qt坐标系统与布局的简单入门
  14. 7 python 类的组合
  15. [Luogu4182][USACO18JAN]Lifeguards P[单调队列]
  16. JavaScript 数组some()和filter()
  17. javascript预编译和执行过程总结
  18. 中国MOOC_面向对象程序设计——Java语言_第1周 类与对象
  19. Web.Config详细说明
  20. Start activity with App Chooser or not ?

热门文章

  1. php中的curl】php中curl的详细解说
  2. 理解css中的position-static\relative\fixed\absolute
  3. Dive into python学习笔记
  4. C#中八皇后问题的递归解法——N皇后
  5. 修改idea的运行内存
  6. 6-JS函数(二)
  7. js友好提示是否继续,post提交
  8. uTenux&mdash;&mdash;重新整理底层驱动库
  9. winform继承窗体,无法修改父窗体控件问题处理笔记
  10. [原创] 使用LP Wizard 10.5 制作 Allegro PCB封装