Android的View Animation(視圖動畫)中的Tween Animation效果,Tween Animation分爲4種動畫效果,分別是:alpha (透明變化) translate(位置移動) scale(縮放) rotate(旋轉),而本篇文章說的是translate(位置移動)的效果。
Translate動畫是非常好理解,就是定義一個開始的位置和一個結束位置,定義移動時間,然後就能自動產生移動動畫。Android的translate移動方向有 橫向(X) 豎向(Y)。
TranslateAnimation共有三個構造方法。除了第一個,另外兩個參數稍微有點區別
- fromXDelta:動畫起始時 X座標上的移動位置
- toXDelta: 動畫結束時 X座標上的移動位置
- fromYDelta: 動畫起始時Y座標上的移動位置
- oYDelta:動畫結束時Y座標上的移動位置
- fromXType:指定X軸的平移模式,可以取值Animation.ABSOLUTE,Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT。
- fromXValue:X平移起始值
- toXType:指定X軸的平移模式,可以取值Animation.ABSOLUTE,Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT。
- toXValue:X平移結束值
- fromYType:指定Y軸的平移模式,可以取值Animation.ABSOLUTE,Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT。
- fromTValue:Y平移起始值
- toYType:指定Y軸的平移模式,可以取值Animation.ABSOLUTE,Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT。
- toYValue:Y平移結束值
下面展示一個使用平移動畫來實現的一個dome按照慣例,我們先看看效果圖:
package com.example.myapplication;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private View view1;
private LinearLayout view2;
private int view1MaxTopMargin = 178;
private int view1MinTopMargin = 88;
private int view2MaxTopMargin = 210;
private int view2MinTopMargin = 56;
private int view2LeftMargin = 44;
private int view2RightMargin = 44;
private boolean canMovingUp = true;
private boolean canMovingDown = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view1 = (View)findViewById(R.id.view1);
view2 = (LinearLayout)findViewById(R.id.view2);
findViewById(R.id.btn_move_up).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!canMovingUp) {
return;
}
moveUpAnimation(view1, view1MinTopMargin, view1MaxTopMargin, 0, 0);
moveUpAnimation(view2, view2MinTopMargin, view2MaxTopMargin, view2LeftMargin, view2RightMargin);
}
});
findViewById(R.id.btn_move_down).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!canMovingDown) {
return;
}
moveDownAnimation(view1, view1MinTopMargin, view1MaxTopMargin, 0, 0);
moveDownAnimation(view2, view2MinTopMargin, view2MaxTopMargin, view2LeftMargin, view2RightMargin);
}
});
}
private void moveUpAnimation(final View view, final int minTopMargin, final int maxTopMargin, final int mLeftMargin, final int mRightMargin) {
AnimationSet mAnimatorSet = new AnimationSet(true);
TranslateAnimation moveUpAnimation = new TranslateAnimation(0F, 0F, 0, -dpToPx(maxTopMargin - minTopMargin));
moveUpAnimation.setDuration(300);//設置動畫變化的持續時間
moveUpAnimation.setFillEnabled(true);//使其可以填充效果從而不回到原地
moveUpAnimation.setFillAfter(true);//不回到起始位置
mAnimatorSet.addAnimation(moveUpAnimation);
view.startAnimation(mAnimatorSet);
mAnimatorSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
canMovingUp = false;
canMovingDown = true;
view.clearAnimation();
//動畫結束後更新view到終點位置
FrameLayout.LayoutParams ll = new FrameLayout.LayoutParams(view.getLayoutParams());
ll.setMargins(dpToPx(mLeftMargin), dpToPx(minTopMargin), dpToPx(mRightMargin), dpToPx(maxTopMargin - minTopMargin));
view.setLayoutParams(ll);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
private void moveDownAnimation(final View view, final int minMargin, final int maxMargin, final int mLeftMargin, final int mRightMargin) {
AnimationSet mAnimatorSet = new AnimationSet(true);
TranslateAnimation moveDownAnimation = new TranslateAnimation(0F, 0F, 0f, dpToPx(maxMargin - minMargin));
moveDownAnimation.setDuration(300);//設置動畫變化的持續時間
moveDownAnimation.setFillEnabled(true);//使其可以填充效果從而不回到原地
moveDownAnimation.setFillAfter(true);//不回到起始位置
mAnimatorSet.addAnimation(moveDownAnimation);
view.startAnimation(mAnimatorSet);
mAnimatorSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
canMovingUp = true;
canMovingDown = false;
view.clearAnimation();
//動畫結束後更新view到終點位置
FrameLayout.LayoutParams ll = new FrameLayout.LayoutParams(view.getLayoutParams());
ll.setMargins(dpToPx(mLeftMargin), dpToPx(maxMargin), dpToPx(mRightMargin), 0);
view.setLayoutParams(ll);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
/**
* dp轉換爲px
*/
public static int dpToPx(float dp) {
return (int) (dp * Resources.getSystem().getDisplayMetrics().density + 0.5f);
}
}
好了,本篇文章就這樣了,存在不足的地方還望指導,感謝
源碼下載