我在Android中有一些png序列,我需要从屏幕的顶部到底部动画他们的x和y位置转换。在动画发生时,我需要对象接收点击事件。
我知道这在3.0之前的Android版本中效果不佳,因为对象的显示与实际对象位于不同的位置,因此您不会获得点击事件-animation(这是我需要的),因为你没有点击对象,只在显示器上。
我正在做这样的事情,动画看起来很好,但我从未进入点击处理程序:
Animation animation = new TranslateAnimation(0, 20,0, 300);
animation.setDuration(1000);
ticket.startAnimation(animation);
ticket.startAnimation(animation);
ticket.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
//I don't get into this click event during animation
view.setVisibility(View.GONE);
}
});
在3.0之前支持设备的交互式动画的解决方法是什么?是否有一个自定义动画类,它将使用后台线程来动画对象的x和y位置?
- 更新--- 我尝试使用NineOldAndroid的解决方案,动画看起来很好,但我没有在动画期间获得onclick事件。这是我更新的代码。所以这在ICS上工作得很好,但在2.3上却没有。我做错了吗?或者NineOldWorld的解决方案实际上并没有改变引擎盖下的布局余量?
ImageView ticket = new ImageView(myActivity);
ticket.setImageResource(R.drawable.ticket);
ticket.setVisibility(View.INVISIBLE);
ticketHolder.addView(ticket);
ValueAnimator animation = ObjectAnimator.ofFloat(ticket, "y",
-200f, ticketHolder.getHeight());
animation.setDuration(3000);
animation.start();
animation.setStartDelay(i*1000);
animationsMap.put(animation, ticket);
ticket.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
view.setVisibility(View.GONE);
}
});
animation.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
// tickets.remove(((ObjectAnimator)animation).getTarget());
ImageView t = (ImageView) ((ObjectAnimator) animation).getTarget();
t.setVisibility(View.GONE);
animationsMap.remove(animation);
if(animationsMap.size() == 0){
ticketsAnimating = false;
scrim.setVisibility(View.VISIBLE);
}
}
@Override
public void onAnimationStart(Animator animation){
ImageView t = (ImageView) ((ObjectAnimator) animation).getTarget();
t.setVisibility(View.VISIBLE);
}
});
- 更新#2 --- 我也尝试了路径动画,但在动画期间和之后,我没有得到按钮点击事件。谁知道我做错了什么?
mButton = (Button) findViewById(R.id.delete_me);
mButtonProxy = AnimatorProxy.wrap(mButton);
// Set up the path we're animating along
AnimatorPath path = new AnimatorPath();
path.moveTo(0, 0);
path.lineTo(0, 300);
path.curveTo(100, 0, 300, 900, 400, 500);
// Set up the animation
final ObjectAnimator anim = ObjectAnimator.ofObject(this, "buttonLoc",
new PathEvaluator(), path.getPoints().toArray());
anim.setDuration(10000);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("united", "button clicked");
anim.start();
}
});
答案 0 :(得分:4)
你是对的,问题在于蜂窝前,只有动画视图的视觉方面正在发生。
这意味着除了执行“可视”动画之外,还必须设置动画视图的框架/ LayoutParams动画。
对于pre-Honeycomb设备,我发现http://nineoldandroids.com/非常有用于收听动画,您可以使用它来适当地设置视图的LayoutParams,但也有其他示例,例如这个在SO上:https://stackoverflow.com/a/3952933/429047
答案 1 :(得分:1)
这就是我最终做的事情,效果非常好。
MyTranslateAnimation animation = new MyTranslateAnimation(ticket, 10, 100, -500, ticketHolder.getHeight());
这是我的班级:
public class MyTranslateAnimation extends Animation {
private float fromXDelta;
private float fromYDelta;
private float toXDelta;
private float toYDelta;
private View view;
public MyTranslateAnimation(View view, float fromXDelta,
float toXDelta, float fromYDelta, float toYDelta) {
this.view = view;
this.fromXDelta = fromXDelta;
this.toXDelta = toXDelta;
this.fromYDelta = fromYDelta;
this.toYDelta = toYDelta;
}
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
Log.d("united", "time " + interpolatedTime);
float newX = (toXDelta - fromXDelta) * interpolatedTime;
float newY = (toYDelta - fromYDelta) * interpolatedTime;
LayoutParams p = (LayoutParams) view.getLayoutParams();
p.leftMargin = (int) newX;
p.topMargin = (int) newY;
if (interpolatedTime > 0.0 && view.getVisibility() == View.GONE) {
view.setVisibility(View.VISIBLE);
}
view.requestLayout();
}
}