可以在 LinearLayout 内的 ViewGroup 内对 View 进行动画处理吗?
我有一个扩展 main.xml 中定义的 LinearLayout 的类。我以编程方式创建了 x 个扩展 ViewGroup 的类。我已重写 canAnimate() 以返回 true。其中还有一些以编程方式创建的视图。我想做的是点击这些嵌套子视图之一并使其动画化(y 轴旋转 ObjectAnimator)。在动画结束时,我将点击的视图隐藏在 onAnimationEnd 侦听器中。
触摸事件有效,我可以看到被点击的视图在给定的动画持续时间后隐藏。问题是视图实际上并没有动画。如果我在 ViewGroup 扩展上使用相同的动画代码,我可以看到您期望的动画。
看起来您无法为嵌套深度超过 2 层的子视图设置动画。任何人都可以确认这一点或给出如何对 LinearLayout 的 ViewGroup 的 View 进行动画处理的示例吗?
这是在蜂巢里。
谢谢。
更新: 我可以在 XML 中嵌套 4 个 LinearLayout 和一个视图,并查看它的动画效果。我更改了扩展 ViewGroup 的类来扩展 LinearLayout,但子视图仍然没有动画。可以设置什么来膨胀我没有以编程方式设置以允许儿童动画的视图?
public class MyViewGroup extends ViewGroup implements ViewGroup.OnTouchListener {
private View viewToAnimate;
public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setup(context);
}
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
setup(context);
}
public MyViewGroup(Context context) {
super(context);
setup(context);
}
private void setup(Context context)
{
viewToAnimate = new View(context);
viewToAnimate.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
viewToAnimate.setBackgroundColor(Color.YELLOW);
viewToAnimate.setOnTouchListener(this);
addView(viewToAnimate);
}
@Override
protected boolean canAnimate()
{
return true;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// Left out for brevity
}
public boolean onTouch(View v, MotionEvent event)
{
animateViewRotation();
return true;
}
private Interpolator accelerator = new AccelerateInterpolator();
private Interpolator decelerator = new DecelerateInterpolator();
private void animateViewRotation()
{
// Tried all of the following:
/*
ObjectAnimator rotationY = ObjectAnimator.ofFloat(viewToAnimate, "rotationY", 0f, 90f);
AnimatorSet translations = new AnimatorSet();
translations.setDuration(1000);
translations.play(rotationY);
translations.start();
*/
/*
RotateAnimation ta = new RotateAnimation(0f, 90f, 0, 100);
ta.setDuration(1000);
viewToAnimate.startAnimation(ta);
*/
// Copied/adapted from API Demos code
ObjectAnimator animation = ObjectAnimator.ofFloat(viewToAnimate, "rotationY", 0f, 90f);
animation.setDuration(1000);
//animation.setInterpolator(accelerator);
animation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator anim) {
viewToAnimate.setVisibility(View.GONE);
}
});
animation.start();
}
}
I have a class that extends LinearLayout defined in main.xml. Programmatically I create x number of classes that extend ViewGroup. I have overridden canAnimate() to return true. Inside of that there are some more views also created programmatically. What I am trying to do is tap on one of those nested child views and have it animate (y-axis rotation ObjectAnimator). At the end of animation I hide the tapped view in the onAnimationEnd listener.
The touch event works, and I can see the view that was tapped hide after the given duration of the animation. The problem is that the view doesn't actually animate. If I use the same animation code on the ViewGroup extension, I can see that animate as you would expect.
What it seems like is that you can't animate child views that are nested more than 2 levels deep. Can anyone confirm this or give an example of how to animate a View of a ViewGroup of a LinearLayout?
This is in Honeycomb.
Thanks.
Update:
I can nest 4 LinearLayouts and a View in XML and see it animate. I changed the class that extends ViewGroup to extend LinearLayout but still no animation with the child view. What could be set inflating a view that I'm not setting programmatically to allow animation of children?
public class MyViewGroup extends ViewGroup implements ViewGroup.OnTouchListener {
private View viewToAnimate;
public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setup(context);
}
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
setup(context);
}
public MyViewGroup(Context context) {
super(context);
setup(context);
}
private void setup(Context context)
{
viewToAnimate = new View(context);
viewToAnimate.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
viewToAnimate.setBackgroundColor(Color.YELLOW);
viewToAnimate.setOnTouchListener(this);
addView(viewToAnimate);
}
@Override
protected boolean canAnimate()
{
return true;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// Left out for brevity
}
public boolean onTouch(View v, MotionEvent event)
{
animateViewRotation();
return true;
}
private Interpolator accelerator = new AccelerateInterpolator();
private Interpolator decelerator = new DecelerateInterpolator();
private void animateViewRotation()
{
// Tried all of the following:
/*
ObjectAnimator rotationY = ObjectAnimator.ofFloat(viewToAnimate, "rotationY", 0f, 90f);
AnimatorSet translations = new AnimatorSet();
translations.setDuration(1000);
translations.play(rotationY);
translations.start();
*/
/*
RotateAnimation ta = new RotateAnimation(0f, 90f, 0, 100);
ta.setDuration(1000);
viewToAnimate.startAnimation(ta);
*/
// Copied/adapted from API Demos code
ObjectAnimator animation = ObjectAnimator.ofFloat(viewToAnimate, "rotationY", 0f, 90f);
animation.setDuration(1000);
//animation.setInterpolator(accelerator);
animation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator anim) {
viewToAnimate.setVisibility(View.GONE);
}
});
animation.start();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论