查看动画在其他布局后面

use*_*366 14 android android-animation

我正在尝试将视图设置为另一个布局的动画,但我遇到了一个问题,因为我的视图位于布局后面.我尝试过android:animateLayoutChanges="true",但没有成功.
我的代码:
layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true">
    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New RadioButton" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:animateLayoutChanges="true"
        android:background="#ffffff">

    </LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我的活动课:

public class MainActivity extends AppCompatActivity {

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

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

        float x = 300;
        float y = 300;
        RadioButton button = (RadioButton) findViewById(R.id.radioButton);
        button.animate().setDuration(10000).x(x).y(y);
    }
}
Run Code Online (Sandbox Code Playgroud)

Tre*_*ers 10

项目的绘制顺序与布局中的顺序相同.对于您的布局,将首先绘制RadioButton,然后绘制LinearLayout.如果它们重叠,则将在RadioButton上绘制LinearLayout.

解决方案部分#1: 翻转RadioButton和LinearLayout的顺序,以便始终最后绘制RadioButton.

<RelativeLayout
    android:id="@+id/final_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="30dp"
    android:background="#ffffff">
</RelativeLayout>

<RadioButton
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New RadioButton" />
Run Code Online (Sandbox Code Playgroud)

Android Developer网站上更严格的解释.

解决方案第2部分: 与我上面提出的观点无关,但可能同样重要,你需要启动动画师.更新后的来源:

public class MainActivity extends AppCompatActivity {

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

        mRadioButton = (RadioButton) findViewById(R.id.radioButton);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

        float x = 300;
        float y = 300;

        ViewPropertyAnimator animator = mRadioButton.animate().setDuration(10000).x(x).y(y);
        animator.setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {}
            @Override
            public void onAnimationEnd(Animator animation) {
                Log.d(TAG, "onAnimationEnd");
            }

            @Override
            public void onAnimationCancel(Animator animation) {}
            @Override
            public void onAnimationRepeat(Animator animation) {}
        })
        animator.start();
    }

    private static final String TAG = MainActivity.class.getSimpleName();
}
Run Code Online (Sandbox Code Playgroud)

在Android 5.1中的应用启动时测试,动画.


May*_*ara 5

只需将所有xml布局元素属性作为 android:clipChildren="false"&android:clipToPadding="false".并且不要忘了 animatedView.bringToFront()

在月之前有相同的问题,并在努力工作几天后找到解决方案.找到我的阙.这里