一、代码实现动画:
Activity中核心代码:
int[] location = new int[2]; viewObj.getLocationOnScreen(location);//获取视图位置 int x = location[0]; int y = location[1]; TranslateAnimation tAnim = new TranslateAnimation(x, x, y, y+15);//设置视图上下移动的位置 tAnim .setDuration(1000); tAnim .setRepeatCount(Animation.INFINITE); tAnim .setRepeatMode(Animation.REVERSE); viewObj.setAnimation(alphaAnimation2); tAnim .start();
二、XML实现动画:
Activity中核心代码:
Animation shakeAnim = AnimationUtils.loadAnimation(this, R.anim.shake_y); viewObj.startAnimation(shakeAnim);
shake_y的XML文件代码:
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:fromYDelta="0" android:interpolator="@anim/cycle" android:toYDelta="10" android:repeatCount="infinite"> </translate>
cycle 的XML文件代码:
<?xml version="1.0" encoding="utf-8"?> <cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="2" />
以上2种均可以实现上下动画效果,如果要取消动画就调用cancel()方法,要清楚动画就用clearAnimation()方法!