android开发中为activity增加左右手势识别,如右滑关闭当前页面。
上述就是android开发分享android开发之为activity增加左右手势识别示例的全部内容,如果对大家有所用处且需要了解更多关于Android学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)
* for左右手势
* 1.复制下面的内容到目标activity
* 2.目标activity的oncreate()调用initgesture()
* 3.目标activity需implements ontouchlistener, ongesturelistener
*/
private gesturedetector mgesturedetector;
private int verticalmindistance = 180;
private int minvelocity = 0;
private void initgesture() {
mgesturedetector = new gesturedetector((ongesturelistener) this);
}
public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) {
if (e1.getx() – e2.getx() > verticalmindistance && math.abs(velocityx) > minvelocity) {
// 切换activity
// intent intent = new intent(viewsnsactivity.this, updatestatusactivity.class);
// startactivity(intent);
//toast.maketext(this, “向左手势”, toast.length_short).show();
} else if (e2.getx() – e1.getx() > verticalmindistance && math.abs(velocityx) > minvelocity) {
// 切换activity
// intent intent = new intent(viewsnsactivity.this, updatestatusactivity.class);
// startactivity(intent);
//toast.maketext(this, “向右手势”, toast.length_short).show();
finish();
overridependingtransition(r.anim.push_right_in, r.anim.push_right_out);
}
return false;
}
@override
public void onlongpress(motionevent arg0) {
// todo auto-generated method stub
}
@override
public boolean onscroll(motionevent arg0, motionevent arg1, float arg2,
float arg3) {
// todo auto-generated method stub
return false;
}
@override
public void onshowpress(motionevent arg0) {
// todo auto-generated method stub
}
@override
public boolean onsingletapup(motionevent arg0) {
// todo auto-generated method stub
return false;
}
@override
public boolean ontouch(view v, motionevent event) {
// todo auto-generated method stub
return mgesturedetector.ontouchevent(event);
}
@override
public boolean ondown(motionevent arg0) {
// todo auto-generated method stub
return false;
}
@override
public boolean dispatchtouchevent(motionevent ev) {
mgesturedetector.ontouchevent(ev);
return super.dispatchtouchevent(ev);
}
push_right_in.xml
<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
<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>
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请点击右边联系管理员删除。
如若转载,请注明出处:https://www.ctvol.com/addevelopment/940184.html