开发者

Android animation clipping when going between layouts

开发者 https://www.devze.com 2022-12-26 18:54 出处:网络
I have a fairly complex layout. I use a relative layout as the root and then inside of it I have a few table views and some further nesting. When I animate an imageview between these layouts my image

I have a fairly complex layout. I use a relative layout as the root and then inside of it I have a few table views and some further nesting. When I animate an imageview between these layouts my image clips. I have a background on the parent layout and the animation looks like it's going under it. I have set android:clipChildren="false" and android:clipToPadding="false" on all of the layouts. I've also set anim.setZAdjustment(Animation.ZORDER_TOP); on all of my animations. Wh开发者_StackOverflow中文版at am I doing wrong?

EDIT: I've found at least 2 bugs so far. android:background= with a hex color causes major animation issues. Table layouts also seem to have a regoin under them that causes clipping. I am also seeing the same thing with framelayouts. I'll add an answer once I actually get my stuff working the way I want it to. I have a feeling using only linear layouts is the solution.

Here is some sample code that produces the issue. If you remove the two child relative layouts then the animation completes as expected.


Java:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView imageViewtop = (ImageView) findViewById(R.id.ImageView01);
    imageViewtop.setOnClickListener(btnCardListener);
}

private OnClickListener btnCardListener = new OnClickListener() {
    public void onClick(View v) {

        ImageView ImageView03 = (ImageView) findViewById(R.id.ImageView03);
        ImageView ImageView05 = (ImageView) findViewById(R.id.ImageView05);

        int[] playLoc = { 0, 0 };
        int[] botLoc = { 0, 0 };
        ImageView05.getLocationOnScreen(playLoc);
        ImageView03.getLocationOnScreen(botLoc);
        int xMove = playLoc[0] - botLoc[0];
        int yMove = playLoc[1] - botLoc[1];

        AnimationSet animSet = new AnimationSet(true);

        RotateAnimation ranim = new RotateAnimation(0f, 180f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f); // , 200, 200); //
                                                    // canvas.getWidth()
        // 2, canvas.getHeight() / 2);
        ranim.setDuration(400);
        ranim.setInterpolator(new DecelerateInterpolator());

        TranslateAnimation transAnim = new TranslateAnimation(
                Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, xMove, // +80.0f,
                Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, yMove // -100.0f
        );
        transAnim.setInterpolator(new DecelerateInterpolator()); // AccelerateInterpolator
                                                                    // ,
                                                                    // LinearInterpolator
        transAnim.setZAdjustment(Animation.ZORDER_TOP);
        transAnim.setAnimationListener(new AnimationListener() {
            public void onAnimationStart(Animation animation) {
            }

            public void onAnimationEnd(Animation animation) {
                ImageView ImageView03 = (ImageView) findViewById(R.id.ImageView03);
                ImageView ImageView05 = (ImageView) findViewById(R.id.ImageView05);

                ImageView05.setVisibility(View.VISIBLE);
                ImageView03.setVisibility(View.INVISIBLE);
            }

            public void onAnimationRepeat(Animation animation) {
            }
        });
        transAnim.setDuration(1000);

        // Order matters...
        animSet.addAnimation(ranim);
        animSet.addAnimation(transAnim);

        ImageView03.startAnimation(animSet);
    }
};

XML:

<RelativeLayout android:id="@+id/relativeParentLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:clipToPadding="false">
    <RelativeLayout android:id="@+id/relativeParentLayout1"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="150dip" android:background="#440000"
        android:layout_alignParentTop="true" android:clipChildren="false"
        android:clipToPadding="false">
        <ImageView android:layout_height="wrap_content" android:id="@+id/ImageView01"
            android:layout_width="wrap_content" android:src="@drawable/card_back"
            android:layout_weight="0" android:layout_alignParentTop="true" />


        <ImageView android:layout_height="wrap_content" android:id="@+id/ImageView03"
            android:layout_width="wrap_content" android:src="@drawable/card_back"
            android:layout_weight="0" android:layout_gravity="center_vertical"
            android:layout_alignParentRight="true" />
    </RelativeLayout>
    <RelativeLayout android:id="@+id/relativeParentLayout2"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="150dip" android:background="#000044"
        android:layout_alignParentBottom="true" android:clipChildren="false"
        android:clipToPadding="false">

        <ImageView android:layout_height="wrap_content"
            android:layout_below="@+id/LinearLayout01" android:id="@+id/ImageView05"
            android:layout_width="wrap_content" android:src="@drawable/card_back"
            android:layout_weight="0" android:layout_alignParentBottom="true" />
    </RelativeLayout>
</RelativeLayout>


I had the same problem and after few days I found the solution ... thanx to:

http://www.mail-archive.com/android-developers@googlegroups.com/msg67535.html

I figured out a solution to this problem. The clue came from the fact that when showing the view, everything worked fine. Apparently, when the animation is running, the update that would be forced by the show happens in the background and doesn't cause the flicker. Adding a short animation to the back end of the onAnimationEnd() when we are hiding the view makes the flicker go away.

Here is the new onAndimationEnd() in the working code

    public void onAnimationEnd(Animation animation) {
             animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
                    animation.setDuration(1);
                    mPlayer0Panel.startAnimation(animation);
    }


the trick was setting setClipChildren to the layout that enclosed the view.


I wish I had a better answer for you, but I solved the problem by adding the object that I wanted to animate to the top level view, doing the animation there, and then hiding it. Not very satisfactory, have you found a better way?

0

精彩评论

暂无评论...
验证码 换一张
取 消