CardView displays artifacts when RotationY > 60

2019-04-27 05:30发布

问题:

I'm trying to use Android's new CardView in a rotation animation. However, the when the CardView's rotationY property is set high enough, some really bad things happen on Lollipop devices. Here's some example code that is pretty easy to compile:

MainActivity.java:

public class MainActivity extends Activity {

    private CardView mTestView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTestView = (CardView) findViewById(R.id.test_view);

        mTestView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mTestView.animate()
                        .rotationY(360)
                        .setDuration(3000);
            }
        });
    }
}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

    <android.support.v7.widget.CardView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/test_view">
    </android.support.v7.widget.CardView>
</RelativeLayout>

After clicking on the view, you'll see a neat rotation animation complete with the Material Design shadows and everything. But once the card's rotation angle gets above approximately 60, it will either stop rendering, or render several frames without clearing the previous frame, or some mixture of the two. This continues until the card's angle is back below this threshold.

Now, this animation is set to take 3 seconds - which is ridiculous. At a more realistic animation speed (say, animation duration 300ms) this is still visible but really just looks like a temporary microstutter.

It seems like a possible bug in the framework - but it could also be that I need to call some additional method to allow the rotation to go off without a problem. Ideas?

Note: Setting the camera distance by calling View.setCameraDistance() doesn't solve the problem; in fact if you just compile the above code with an insanely high (~100,000) distance, the issue still happens, but with a higher angle threshold (somewhere around 80-85, if I had to guess.)

回答1:

Here is the solution on following post.

Android View Disappearing When Go Outside Of Parent

Given a setting that not to clip child view on the ViewGroup the animation views are belong to.

android:clipChildren="false"
android:clipToPadding="false"