[英]How programmatically apply translate & scale animation simultaneously on a view
How apply translate animation with scale animation on a view. 如何在视图上应用带缩放动画的平移动画。 I have to move a view to another view location with zoom out simultaneously.
我必须同时缩小视图到另一个视图位置。
How scale a view from its position to another view position (Second view is not fixed)? 如何将视图从其位置缩放到另一个视图位置(第二个视图不固定)?
startView - view that translate startView - 翻译的视图
finishView - where animation finish. finishView - 动画完成的地方。
**Code ** **代码**
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);
}
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);
}
Scale animation alone is enough to get that feel, while expanding from a point to a point it gives translation effect too. 单独使用缩放动画就足以获得这种感觉,同时从一个点扩展到一个点它也会产生转换效果。 Try this code,
试试这段代码,
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);
this code scales the view from top to bottom. 此代码从上到下缩放视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.