Android编程实现左右滑动切换背景的方法
程序员文章站
2024-02-20 08:22:22
本文实例讲述了android编程实现左右滑动切换背景的方法。分享给大家供大家参考,具体如下:
最近想做一个左右滑动切换背景图片的应用,特地将自己的研究分享一下:
这个需...
本文实例讲述了android编程实现左右滑动切换背景的方法。分享给大家供大家参考,具体如下:
最近想做一个左右滑动切换背景图片的应用,特地将自己的研究分享一下:
这个需要继承2个监听接口 ongesturelistener, ontouchlistener
关于这2个接口大家可以在网上查一下
同时需要设置2个属性
bglayout.setontouchlistener(this); bglayout.setlongclickable(true);
并且在这个函数中有如下这几句话
public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub return this.mgesture.ontouchevent(event); }
附送代码:
public class switcheractivity extends activity implements ongesturelistener, ontouchlistener { /** called when the activity is first created. */ linearlayout bglayout = null; private gesturedetector mgesture = null; private int flag = 3; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mgesture = new gesturedetector(this); bglayout = (linearlayout) findviewbyid(r.id.bg); bglayout.setbackgroundresource(r.drawable.bg3); bglayout.setontouchlistener(this); bglayout.setlongclickable(true); } public boolean ondown(motionevent e) { // todo auto-generated method stub return false; } public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { // 处理左右滑动 if (e1.getx() - e2.getx() > 100) { // 向左滑动 if (flag == 3) { bglayout.setbackgroundresource(r.drawable.bg4); flag = 4; return true; } if (flag == 4) { bglayout.setbackgroundresource(r.drawable.bg5); flag = 5; return true; } if (flag == 1) { bglayout.setbackgroundresource(r.drawable.bg2); flag = 2; return true; } if (flag == 2) { bglayout.setbackgroundresource(r.drawable.bg3); flag = 3; return true; } } else if (e1.getx() - e2.getx() < -100) { // 向右滑动 if (flag == 3) { bglayout.setbackgroundresource(r.drawable.bg2); flag = 2; return true; } if (flag == 2) { bglayout.setbackgroundresource(r.drawable.bg1); flag = 1; return true; } if (flag == 5) { bglayout.setbackgroundresource(r.drawable.bg4); flag = 4; return true; } if (flag == 4) { bglayout.setbackgroundresource(r.drawable.bg3); flag = 3; return true; } } return false; } public void onlongpress(motionevent e) { // todo auto-generated method stub } public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { // todo auto-generated method stub return false; } public void onshowpress(motionevent e) { // todo auto-generated method stub } public boolean onsingletapup(motionevent e) { // todo auto-generated method stub return false; } public boolean ontouch(view v, motionevent event) { // todo auto-generated method stub return this.mgesture.ontouchevent(event); } }
更多关于android图片与特效相关内容感兴趣的读者可查看本站专题:《android开发动画技巧汇总》和《android图形与图像处理技巧总结》
希望本文所述对大家android程序设计有所帮助。
上一篇: Mysql判断表字段或索引是否存在
下一篇: PHP多维数组怎么替换键名和键值