如何在视图上应用带缩放动画的平移动画。我必须同时缩小视图到另一个视图位置。
如何将视图从其位置缩放到另一个视图位置(第二个视图不固定)?
startView - 翻译视图
finishView - 动画完成的地方。
**代码**
private void startAnimation(View startView, View finishView) {
int startX = startView.getLeft() + startView.getWidth() / 2;
int startY = startView.getTop() + startView.getHeight() / 2;
int startViewLocation[]=new int[2];
startView.getLocationInWindow(startViewLocation);
int finishViewLocation[]=new int[2];
finishView.getLocationInWindow(finishViewLocation);
int endX = finishViewLocation[0];
int endY=finishViewLocation[1];
System.out.println("statX " + startX + " " + (startView.getLeft() + startView.getWidth() / 2));
System.out.println("statY " + startY+" "+(startView.getTop() + startView.getHeight() / 2));
System.out.println("endX " + endX+" "+finishViewLocation[0]);
System.out.println("endY " + endY+" "+finishViewLocation[1]);
TranslateAnimation translateAnimation = new TranslateAnimation(0, 0,
TranslateAnimation.ABSOLUTE, endX, 0, 0,
TranslateAnimation.ABSOLUTE, endY);
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f,
0.0f);
AnimationSet set = new AnimationSet(true);
set.addAnimation(translateAnimation);
set.addAnimation(scaleAnimation);
set.setFillAfter(true);
set.setDuration(2000);
set.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
});
startView.startAnimation(set);
}
答案 0 :(得分:4)
private void scaleAnimation(final View startView, View finishView,final TextView cartItems) {
int startViewLocation[] = new int[2];
startView.getLocationInWindow(startViewLocation);
int finishViewLocation[] = new int[2];
finishView.getLocationInWindow(finishViewLocation);
int startX = startViewLocation[0] + startView.getWidth() / 2;
int startY = startViewLocation[1] + startView.getHeight() / 2;
int endX = finishViewLocation[0] + finishView.getWidth() / 2;
int endY = finishViewLocation[1] + finishView.getHeight() / 2;
ScaleAnimation animation = new ScaleAnimation(1f, 0f, 1, 0f,
Animation.ABSOLUTE, endX - startX + startView.getWidth() / 2,
Animation.ABSOLUTE, endY - startY + startView.getHeight() / 2);
// animation.scaleCurrentDuration(6000);
animation.setDuration(2000);
// animation.setStartOffset(50);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
startView.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
startView.setVisibility(View.GONE);
cartItems.setText(String.valueOf(totlaCartsItems));
}
});
startView.startAnimation(animation);
}
答案 1 :(得分:2)
单独缩放动画就足以让人感觉到,同时从一个点扩展到一个点它也会产生翻译效果。 试试这段代码,
ScaleAnimation animation = new ScaleAnimation(1, 1, 0, 1, 1,
Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF, 0f);
animation.scaleCurrentDuration(6000);
animation.setDuration(300);
animation.setStartOffset(50);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
});
view.startAnimation(animation);
此代码从上到下缩放视图。