下面列出了怎么用android.view.animation.TranslateAnimation的API类实例代码及写法,或者点击链接到github查看源代码。
private AnimationSet getEntryAnimation(int inAnimationDuration) {
//In
AnimationSet mInAnimationSet = new AnimationSet(false);
TranslateAnimation mSlideInAnimation = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
TranslateAnimation.RELATIVE_TO_SELF, -1.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f);
mSlideInAnimation.setFillAfter(true);
AlphaAnimation mFadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
mFadeInAnimation.setFillAfter(true);
mInAnimationSet.addAnimation(mSlideInAnimation);
mInAnimationSet.addAnimation(mFadeInAnimation);
mInAnimationSet.setDuration(inAnimationDuration);
return mInAnimationSet;
}
void display(float x, float y) {
this.startPositionX = x;
this.startPositionY = y;
recordButtonFab.setVisibility(View.VISIBLE);
AnimationSet animation = new AnimationSet(true);
animation.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 0));
animation.addAnimation(new ScaleAnimation(.5f, 1f, .5f, 1f,
Animation.RELATIVE_TO_SELF, .5f,
Animation.RELATIVE_TO_SELF, .5f));
animation.setDuration(ANIMATION_DURATION);
animation.setInterpolator(new OvershootInterpolator());
recordButtonFab.startAnimation(animation);
}
void hide() {
recordButtonFab.setTranslationX(0);
recordButtonFab.setTranslationY(0);
if (recordButtonFab.getVisibility() != VISIBLE) return;
AnimationSet animation = new AnimationSet(false);
Animation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
Animation translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, lastOffsetX,
Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, lastOffsetY,
Animation.ABSOLUTE, 0);
scaleAnimation.setInterpolator(new AnticipateOvershootInterpolator(1.5f));
translateAnimation.setInterpolator(new DecelerateInterpolator());
animation.addAnimation(scaleAnimation);
animation.addAnimation(translateAnimation);
animation.setDuration(ANIMATION_DURATION);
animation.setInterpolator(new AnticipateOvershootInterpolator(1.5f));
recordButtonFab.setVisibility(View.GONE);
recordButtonFab.clearAnimation();
recordButtonFab.startAnimation(animation);
}
public static void hideViewFromBottom(final View view) {
if (view.getVisibility() == View.INVISIBLE) {
return;
}
int height = view.getHeight();
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, height);
translateAnimation.setDuration(ANIMATION_DURATION);
translateAnimation.setInterpolator(sAnimationInterpolator);
translateAnimation.setAnimationListener(new AnimationAdapter() {
@Override
public void onAnimationEnd(Animation animation) {
view.setVisibility(View.INVISIBLE);
}
});
view.startAnimation(translateAnimation);
}
public void display(float x) {
this.startPositionX = x;
this.lastPositionX = x;
recordButtonFab.setVisibility(View.VISIBLE);
AnimationSet animation = new AnimationSet(true);
animation.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, -.25f,
Animation.RELATIVE_TO_SELF, -.25f));
animation.addAnimation(new ScaleAnimation(.5f, 1f, .5f, 1f,
Animation.RELATIVE_TO_SELF, .5f,
Animation.RELATIVE_TO_SELF, .5f));
animation.setFillBefore(true);
animation.setFillAfter(true);
animation.setDuration(ANIMATION_DURATION);
animation.setInterpolator(new OvershootInterpolator());
recordButtonFab.startAnimation(animation);
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_capture);
scanPreview = (SurfaceView) findViewById(R.id.capture_preview);
scanContainer = (RelativeLayout) findViewById(R.id.capture_container);
scanCropView = (RelativeLayout) findViewById(R.id.capture_crop_view);
scanLine = (ImageView) findViewById(R.id.capture_scan_line);
inactivityTimer = new InactivityTimer(this);
beepManager = new BeepManager(this);
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT,
0.9f);
animation.setDuration(4500);
animation.setRepeatCount(-1);
animation.setRepeatMode(Animation.RESTART);
scanLine.startAnimation(animation);
}
private void initViews() {
autoFocusHandler = new Handler();
mCameraManager = new CameraManager(this);
try {
mCameraManager.openDriver();
} catch (IOException e) {
e.printStackTrace();
}
mCamera = mCameraManager.getCamera();
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
scanPreview.addView(mPreview);
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT,
0.85f);
animation.setDuration(3000);
animation.setRepeatCount(-1);
animation.setRepeatMode(Animation.REVERSE);
scanLine.startAnimation(animation);
}
@Override
public long calculateStatusBarTransitionStartTime() {
TranslateAnimation openTranslateAnimation = findTranslateAnimation(mAnimation);
if (openTranslateAnimation != null) {
// Some interpolators are extremely quickly mostly finished, but not completely. For
// our purposes, we need to find the fraction for which ther interpolator is mostly
// there, and use that value for the calculation.
float t = findAlmostThereFraction(openTranslateAnimation.getInterpolator());
return SystemClock.uptimeMillis()
+ openTranslateAnimation.getStartOffset()
+ (long)(openTranslateAnimation.getDuration() * t)
- STATUS_BAR_TRANSITION_DURATION;
} else {
return SystemClock.uptimeMillis();
}
}
@Override
public Animation createOptionDisappearAnimation(OptionButton optionButton, int index) {
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation= new TranslateAnimation(
0
,getYMenuButton().getX() - optionButton.getX()
,0
,getYMenuButton().getY() - optionButton.getY()
);
translateAnimation.setDuration(getOptionSD_AnimationDuration());
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(getOptionSD_AnimationDuration());
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(alphaAnimation);
//设置动画延时
animationSet.setStartOffset(60*(getOptionPositionCount() - index));
return animationSet;
}
/**
* 位置还原
*/
private void recoverLayout() {
if (!isMoved) {
return;//如果没有移动布局,则跳过执行
}
for (int i = 0; i < mMoveViews.size(); i++) {
if (mMoveRects.get(i) != null) {
TranslateAnimation anims = new TranslateAnimation(0, 0, mMoveViews.get(i).getTop(), mMoveRects.get(i).top);
anims.setDuration(ANIM_TIME);
mMoveViews.get(i).startAnimation(anims);
mMoveViews.get(i).layout(mMoveRects.get(i).left, mMoveRects.get(i).top, mMoveRects.get(i).right, mMoveRects.get(i).bottom);
}
}
TranslateAnimation anim = new TranslateAnimation(0, 0, childView.getTop() - originalRect.top, 0);
anim.setDuration(ANIM_TIME);
childView.startAnimation(anim);
childView.layout(originalRect.left, originalRect.top, originalRect.right, originalRect.bottom);
isMoved = false;
}
public static void setAdapterInsertAnimation(final View aCard, int row, int height) {
final int ANIMATION_DURATION = 650;
final int BASE_DELAY = 50;
TranslateAnimation translationAnimation = new TranslateAnimation(0, 0, height, 0);
AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
final AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(translationAnimation);
animationSet.addAnimation(alphaAnimation);
animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
animationSet.setFillAfter(true);
animationSet.setFillBefore(true);
animationSet.setDuration(ANIMATION_DURATION + row * BASE_DELAY);
aCard.setAnimation(animationSet);
}
private static AnimationSet startAlphaHideAnimation(final int DELAY, final View VIEW, boolean includeTransition) {
final int ANIMATION_DURATION = 300;
if (VIEW == null)
return null;
final Animation mAlphaAnimation = new AlphaAnimation(1f, 0f);
mAlphaAnimation.setDuration(ANIMATION_DURATION);
mAlphaAnimation.setFillAfter(true);
final AnimationSet mHideAnimations = new AnimationSet(true);
mHideAnimations.setInterpolator(new AccelerateDecelerateInterpolator());
mHideAnimations.setFillAfter(true);
mHideAnimations.addAnimation(mAlphaAnimation);
if (includeTransition) {
final Animation mTransitionAnimation = new TranslateAnimation(0, 0, 0, VIEW.getHeight() / 2);
mTransitionAnimation.setDuration(ANIMATION_DURATION);
mTransitionAnimation.setFillAfter(false);
mHideAnimations.addAnimation(mTransitionAnimation);
}
new Handler().postDelayed(() -> VIEW.startAnimation(mHideAnimations), DELAY);
return mHideAnimations;
}
public void animateToNext() {
et.setEnabled(false);
int totalWidth = getWidth();
int dvWidth = dv.getWidth();
int animDistance = (totalWidth - dvWidth) / 2 + dvWidth;
TranslateAnimation animOut = new TranslateAnimation(0, -animDistance, 0, 0);
animOut.setInterpolator(new AccelerateDecelerateInterpolator());
animOut.setFillAfter(true);
animOut.setDuration(AnimDuration);
TranslateAnimation animIn = new TranslateAnimation(animDistance, 0, 0, 0);
animIn.setInterpolator(new AccelerateDecelerateInterpolator());
animIn.setFillBefore(true);
animIn.setDuration(AnimDuration);
animIn.setAnimationListener(animateToNextListener);
dvNew.setVisibility(View.VISIBLE);
dv.startAnimation(animOut);
dvNew.startAnimation(animIn);
}
@Override
public Animation createOptionDisappearAnimation(OptionButton optionButton, int index) {
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation= new TranslateAnimation(
0
,getYMenuButton().getX() - optionButton.getX()
,0
,getYMenuButton().getY() - optionButton.getY()
);
translateAnimation.setDuration(getOptionSD_AnimationDuration());
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(getOptionSD_AnimationDuration());
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(alphaAnimation);
//为不同的Option设置延时
if (index % 2 == 0) {
animationSet.setStartOffset(getOptionSD_AnimationDuration()/2);
}
return animationSet;
}
@Override
public void initView() {
mActivity = this;
setViewFind(capturePreview, captureCropView, captureContainer);
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
animation.setDuration(1500);
animation.setRepeatCount(-1);
animation.setRepeatMode(Animation.RESTART);
captureScanLine.startAnimation(animation);
//setLineAnimation(captureScanLine);
setPresenter(new ScanLoginPresenter(this));
presenter.start();
}
/**
* 创建动画 平移动画直接传递偏移量
*
* @param startX
* @param startY
* @return
*/
private Animation createAnim(int startX, int startY) {
int[] des = new int[2];
imgCart.getLocationInWindow(des);
AnimationSet set = new AnimationSet(false);
Animation translationX = new TranslateAnimation(0, des[0] - startX, 0, 0);
translationX.setInterpolator(new LinearInterpolator());
Animation translationY = new TranslateAnimation(0, 0, 0, des[1] - startY);
translationY.setInterpolator(new AccelerateInterpolator());
Animation alpha = new AlphaAnimation(1, 0.5f);
set.addAnimation(translationX);
set.addAnimation(translationY);
set.addAnimation(alpha);
set.setDuration(500);
return set;
}
private void moveView(int fromPosition, int toPosition) {
if (DEBUG_LOG) {
L.d(TAG, "moveView from:" + fromPosition + ",to:" + toPosition);
}
final View from = getView(fromPosition);
final View to = getView(toPosition);
final Rect fromRect = new Rect();
getLayout(from, fromRect);
final Rect toRect = new Rect();
getLayout(to, toRect);
Animation translate = new TranslateAnimation(0, toRect.left
- fromRect.left, 0, toRect.top - fromRect.top);
translate.setDuration(150);
translate.setFillEnabled(true);
translate.setFillBefore(true);
translate.setFillAfter(true);
translate.setAnimationListener(new MoveViewAnimationListener(from, to
.getLeft(), to.getTop()));
from.startAnimation(translate);
}
private void useCodeAnimation(ViewAnimator pages) {
// 用代码定义一个动画
AnimationSet slideAnimationSet = new AnimationSet(true);
// 平移动画
TranslateAnimation slide = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_PARENT,
0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
// 缩放动画
ScaleAnimation scale = new ScaleAnimation(10, 1, 10, 1);
// 把平移和缩放动画加入动画集合
slideAnimationSet.addAnimation(slide);
slideAnimationSet.addAnimation(scale);
// 持续时间设置为1000ms
slideAnimationSet.setDuration(1000);
// 设置动画
pages.setInAnimation(slideAnimationSet);
}
public void translate(View v){
//����ƽ�Ʋ��䶯��
// TranslateAnimation ta = new TranslateAnimation(-100, 100, -50, 50);
ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -3, Animation.RELATIVE_TO_SELF, 3,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
//���ö�������ʱ��
ta.setDuration(2000);
//�����ظ����Ŵ���
ta.setRepeatCount(1);
//�����ظ�����ģʽ
ta.setRepeatMode(Animation.REVERSE);
//���ö���ͣ���ڽ���λ��
ta.setFillAfter(true);
iv.startAnimation(ta);
}
void moveTo(float offset) {
Animation animation = new TranslateAnimation(Animation.ABSOLUTE, offset,
Animation.ABSOLUTE, offset,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0);
animation.setDuration(0);
animation.setFillAfter(true);
animation.setFillBefore(true);
slideToCancelView.startAnimation(animation);
}
private void changeInterpolator(int position) {
Animation animation = new TranslateAnimation(0.0f,
mInterpolatorTargetParent.getWidth()-mInterpolatorTarget.getWidth()-mInterpolatorTargetParent.getPaddingLeft()-mInterpolatorTargetParent.getPaddingRight(),
0.0f, 0.0f);
animation.setDuration(1000);
animation.setStartOffset(300);
animation.setRepeatMode(Animation.RESTART);
animation.setRepeatCount(Animation.INFINITE);
switch (position) {
case 0:
animation.setInterpolator(AnimationUtils.loadInterpolator(this,
android.R.anim.accelerate_interpolator));
break;
case 1:
animation.setInterpolator(AnimationUtils.loadInterpolator(this,
android.R.anim.decelerate_interpolator));
break;
case 2:
animation.setInterpolator(AnimationUtils.loadInterpolator(this,
android.R.anim.accelerate_decelerate_interpolator));
break;
case 3:
animation.setInterpolator(AnimationUtils.loadInterpolator(this,
android.R.anim.anticipate_interpolator));
break;
case 4:
animation.setInterpolator(AnimationUtils.loadInterpolator(this,
android.R.anim.overshoot_interpolator));
break;
case 5:
animation.setInterpolator(AnimationUtils.loadInterpolator(this,
android.R.anim.anticipate_overshoot_interpolator));
break;
case 6:
animation.setInterpolator(AnimationUtils.loadInterpolator(this,
android.R.anim.bounce_interpolator));
break;
}
mInterpolatorTarget.startAnimation(animation);
}
public EditTextShakeHelper(Context context) {
// 初始化振动器
shakeVibrator = (Vibrator) context
.getSystemService(Service.VIBRATOR_SERVICE);
// 初始化震动动画
shakeAnimation = new TranslateAnimation(0, 10, 0, 0);
shakeAnimation.setDuration(300);
cycleInterpolator = new CycleInterpolator(8);
shakeAnimation.setInterpolator(cycleInterpolator);
}
/**
* 晃动动画
* @param counts 1秒钟晃动多少下
* @return
*/
public static Animation shakeAnimation(int counts){
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
translateAnimation.setInterpolator(new CycleInterpolator(counts));
translateAnimation.setDuration(1000);
return translateAnimation;
}
private void showStreamPopWindowForView(View view) {
this.animTopIn = new TranslateAnimation(1, 0.0f, 1, 0.0f, 1, -0.5f, 1, 0.0f);
this.animTopIn.setDuration(300);
this.layoutView.setAnimation(this.animTopIn);
int[] arrayOfInf = new int[2];
view.getLocationInWindow(arrayOfInf);
int x = arrayOfInf[0];
int y = arrayOfInf[1] + Util.dipToPx(45.0f);
this.mPopupWindow.setAnimationStyle(R.style.popwin_anim_style);
this.mPopupWindow.showAtLocation(view, 51, x, y);
}
/**
* Creates animation for the gallery and homePage Icons with down direction.
*/
public void createIconDownAnimation(float dy) {
iconDownAnimation = new TranslateAnimation(0, 0, 0, ((scale * 67.3f) + 0.5f + (dy * scale)) * iconDirection);
iconDownAnimation.setDuration(250);
iconDownAnimation.setFillAfter(false);
iconDownAnimation.setAnimationListener(iconDownAnimationListener);
}
private void initAnims() {
animShow = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1,
Animation.RELATIVE_TO_SELF, 0);
animShow.setDuration(300);
animHide = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1);
animHide.setDuration(300);
}
private TranslateAnimation getTranslateAnimator(float targetX, float targetY) {
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.ABSOLUTE, targetX,
Animation.RELATIVE_TO_SELF, 0f,
Animation.ABSOLUTE, targetY);
// RecyclerView默认移动动画250ms 这里设置360ms 是为了防止在位移动画结束后 remove(view)过早 导致闪烁
translateAnimation.setDuration(360);
translateAnimation.setFillAfter(true);
return translateAnimation;
}
private void showViewWithAnim(View view) {
mSmileyPicker.setVisibility(View.VISIBLE);
Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0);
animation.setDuration(150);
// animation.setFillAfter(true);
view.startAnimation(animation);
}
private void showFloatImage() {
isShowFloatImage = true;
TranslateAnimation ta = new TranslateAnimation(moveDistance,0,0,0);
ta.setDuration(300);
AlphaAnimation aa = new AlphaAnimation(0.5f, 1.0f);
aa.setDuration(300);
AnimationSet set = new AnimationSet(true);
//动画完成后不回到原位 true:persist 默认值为false
set.setFillAfter(true);
set.addAnimation(ta);
set.addAnimation(aa);
mIvCartView.startAnimation(set);
}
private void hideFloatImage(int moveDistance) {
isShowFloatImage = false;
TranslateAnimation ta = new TranslateAnimation(0, moveDistance,0,0);
ta.setDuration(300);
AlphaAnimation aa = new AlphaAnimation(1.0f, 0.5f);
aa.setDuration(300);
AnimationSet set = new AnimationSet(true);
//动画完成后不回到原位 true:persist 默认值为false
set.setFillAfter(true);
set.addAnimation(ta);
set.addAnimation(aa);
mIvCartView.startAnimation(set);
}