Android编程之界面跳动提示动画效果实现方法
程序员文章站
2023-12-15 10:27:28
本文实例讲述了android编程之界面跳动提示动画效果实现方法。分享给大家供大家参考,具体如下:
上一个效果图:
先上布局:
本文实例讲述了android编程之界面跳动提示动画效果实现方法。分享给大家供大家参考,具体如下:
上一个效果图:
先上布局:
<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" > <relativelayout android:id="@+id/red" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff0000" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/switch2blue" android:layout_centerhorizontal="true" android:text="首页" /> <button android:id="@+id/switch2blue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:text="置换位蓝色" /> </relativelayout> <relativelayout android:id="@+id/blue" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0000ff" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/switch2red" android:layout_centerhorizontal="true" android:text="第二页" /> <button android:id="@+id/switch2red" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:text="置换位红色" /> </relativelayout> </relativelayout>
代码如下:
import java.lang.reflect.field; import android.app.activity; import android.os.bundle; import android.os.handler; import android.util.displaymetrics; import android.view.gesturedetector; import android.view.motionevent; import android.view.view; import android.view.gesturedetector.ongesturelistener; import android.view.view.onclicklistener; import android.view.view.ontouchlistener; import android.view.animation.acceleratedecelerateinterpolator; import android.view.animation.accelerateinterpolator; import android.view.animation.animation; import android.view.animation.animationset; import android.view.animation.translateanimation; import android.view.animation.animation.animationlistener; import android.widget.button; import android.widget.relativelayout; public class mainactivity extends activity implements onclicklistener, ontouchlistener, ongesturelistener { private relativelayout red, blue; private button switch2blue, switch2red; private float thisdelta = 0.05f; private static boolean haseverpulled = false; private boolean pulled = false; private static int height; private gesturedetector gesturedetector; private static int statusheight = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); displaymetrics metrics = getresources().getdisplaymetrics(); height = metrics.heightpixels; statusheight = getstatusheight(); initview(); // 跳动提示可以上拉 final handler handler = new handler(); handler.postdelayed(new runnable() { @override public void run() { jump(thisdelta); // handler.postdelayed(this, 3000); } }, 3000); } private void initview() { red = (relativelayout) findviewbyid(r.id.red); blue = (relativelayout) findviewbyid(r.id.blue); switch2blue = (button) findviewbyid(r.id.switch2blue); switch2red = (button) findviewbyid(r.id.switch2red); switch2blue.setonclicklistener(this); switch2red.setonclicklistener(this); blue.setontouchlistener(this); blue.setlongclickable(true); gesturedetector = new gesturedetector(this, this); } @override public void onclick(view arg0) { // todo auto-generated method stub switch (arg0.getid()) { case r.id.switch2blue: red2blueui(); break; case r.id.switch2red: blue2redui(); break; } } // 从红页面转到蓝页面 private void red2blueui() { blue.bringtofront(); blue.requestlayout(); blue.invalidate(); } // 从蓝页面转到红页面 private void blue2redui() { red.bringtofront(); red.requestlayout(); red.invalidate(); } // 获取状态栏的高度 private int getstatusheight() { class<?> c = null; object obj = null; field field = null; int x = 0; int height = 0; try { c = class.forname("com.android.internal.r$dimen"); obj = c.newinstance(); field = c.getfield("status_bar_height"); x = integer.parseint(field.get(obj).tostring()); height = getresources().getdimensionpixelsize(x); } catch (exception e) { e.printstacktrace(); } return height; } // 主页面跳动 private void jump(float delta) { if (thisdelta - 0.03f < 0.001f) { thisdelta = 0.05f; return; } thisdelta = delta; if (haseverpulled) { return; } playjumpanimation(thisdelta); } // 上拉/下拉主页面 private void pull(boolean upward) { if (upward && pulled) { return; } if (!upward && !pulled) { return; } float originaly; float finaly; if (!pulled) { originaly = 0; finaly = (float) (0 - height + 0.4 * height); } else { originaly = (float) (0 - height + 0.4 * height); finaly = 0; } pulled = !pulled; animationset animationset = new animationset(true); animationset.addanimation(new translateanimation(0, 0, originaly, finaly)); animationset.setduration(300); animationset.setinterpolator(new acceleratedecelerateinterpolator()); animationset.setfillafter(true); animationset.setanimationlistener(new animationlistener() { @override public void onanimationstart(animation animation) { if (!pulled) { red2blueui(); } } @override public void onanimationrepeat(animation animation) { } @override public void onanimationend(animation animation) { // if (pulled) { // blue2redui(); // } } }); blue.startanimation(animationset); haseverpulled = true; } // 跳起动画 private void playjumpanimation(final float delta) { float originaly = 0; float finaly = 0 - height * delta; animationset animationset = new animationset(true); animationset.addanimation(new translateanimation(0, 0, originaly, finaly)); animationset.setduration(300); animationset.setinterpolator(new acceleratedecelerateinterpolator()); animationset.setfillafter(true); animationset.setanimationlistener(new animationlistener() { @override public void onanimationstart(animation animation) { } @override public void onanimationrepeat(animation animation) { } @override public void onanimationend(animation animation) { playlandanimation(delta); } }); blue.startanimation(animationset); } // 落下动画 private void playlandanimation(final float delta) { float originaly = 0 - height * delta; float finaly = 0; animationset animationset = new animationset(true); animationset.addanimation(new translateanimation(0, 0, originaly, finaly)); animationset.setduration(200); animationset.setinterpolator(new accelerateinterpolator()); animationset.setfillafter(true); animationset.setanimationlistener(new animationlistener() { @override public void onanimationstart(animation animation) { } @override public void onanimationrepeat(animation animation) { } @override public void onanimationend(animation animation) { jump(0.03f); } }); blue.startanimation(animationset); } @override public boolean ondown(motionevent e) { pull(false); return false; } @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { // 手势滑动达到100才触发 if (e1.gety() - e2.gety() > 100) { pull(true); } else if (e2.gety() >= e1.gety()) { pull(false); } return false; } @override public void onlongpress(motionevent e) { } @override public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { return false; } @override public void onshowpress(motionevent e) { } @override public boolean onsingletapup(motionevent e) { return false; } @override public boolean ontouch(view v, motionevent event) { if (pulled) { // 首张页可触控点 if (event.gety() > height * 0.4 - statusheight) { return false; } } return gesturedetector.ontouchevent(event); } }
希望本文所述对大家android程序设计有所帮助。