简体   繁体   English

Android自定义视图和Alpha动画

[英]Android custom view and alpha animation

I have a custom view on top of other views which I am fading out using a ViewPropertyAnimator like this: 我在其他视图之上有一个自定义视图,我正在使用ViewPropertyAnimator淡出这样的图像:

mCustomView.animate().setDuration(sFadeDuration).alpha(0f).setListener(new Animator.AnimatorListener() { ... });

This works but the problem is that I have some views under the mCustomView which will only show after the animation has finished. mCustomView ,但是问题是我在mCustomView下有一些视图,这些mCustomView仅在动画完成后显示。

What I want is for them to be revealed while the view on top fades away. 我想要的是让它们显示出来,而顶部的视图逐渐消失。 Strangely, there is one view which behaves as expected, and that is a VideoView (see below for layout structure). 奇怪的是,有一个视图的行为与预期的一样,即一个VideoView (有关布局结构,请参见下文)。

So I see the VideoView fading in under the disappearing view but all the other views just pop up after the animation has finished. 因此,我看到VideoView在消失的视图下逐渐消失,但是所有其他视图在动画完成后才弹出。

The animated view is a custom view which overrides onDraw like this: 动画视图是一个自定义视图,它像这样覆盖onDraw

@Override
protected void onDraw(Canvas canvas) {
    TextPaint paint = new TextPaint();
    canvas.drawColor(Color.TRANSPARENT);

    Canvas c = new Canvas(mBitmap);
    c.drawRect(0, 0, mMetrics.widthPixels, mMetrics.heightPixels, paint);

    paint.setColor(mColor);

    canvas.drawBitmap(mBitmap, 0,0, paint);
}

When I do not override onDraw , everything works as expected. 当我不重写onDraw ,一切都会按预期进行。

Does anyone know why the other views are not rendered until the animation finishes? 有谁知道为什么动画结束之前不渲染其他视图? Is there a solution to avoid this? 有解决方案可以避免这种情况吗? Do I have to somehow apply the animation inside of the onDraw method? 我必须以某种方式在onDraw方法内部应用动画吗?

Here's the layout structure: 这是布局结构:

<RelativeLayout>
  <VideoView />
     <RelativeLayout>
            <LinearLayout>
            <TextView />
            <TextView />
            </LinearLayout>

            <LinearLayout>
            <View />
            </LinearLayout>

            <LinearLayout>
            <Button />
            <Button />
            </LinearLayout>

            <Button />

   </RelativeLayout>
   <CustomView />
</RelativeLayout>

VideoView is a subclass of SurfaceView. VideoView是SurfaceView的子类。 SurfaceView has special interactions, so that may explain why it is behaving differently. SurfaceView具有特殊的交互作用,因此可以解释为什么其行为有所不同。 From the SurfaceView docs : SurfaceView文档

"The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it. This can be used to place overlays such as buttons on top of the Surface, though note however that it can have an impact on performance since a full alpha-blended composite will be performed each time the Surface changes." “视图层次结构将负责正确地将通常出现在其顶部的任何SurfaceView兄弟姐妹与Surface正确合成。这可用于将叠加层(例如按钮)放置在Surface之上,尽管请注意,但是它可以具有这会影响性能,因为每次Surface更改时都会执行完整的alpha混合合成。”

Try overriding view.isOpaque to be false in your custom view. 尝试在自定义视图中覆盖view.isOpaque为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM