Android 3.2上的animate()。alpha怪异行为

时间:2014-05-19 23:05:18

标签: android jquery-animate tablelayout

我在最近发布的应用程序中进行了一些从4.0到3.2的更改。 它在4.0上运行良好,在阅读了与3.2的SDK差异并做了一些测试之后我终于编译没有错误。 我在所有活动中共享相同的编码,以显示/隐藏不同的布局,主屏幕,进度指示器屏幕,结果,屏幕,在同一活动内。 例如,这个管理2个布局

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void showProgress(final boolean show) {
    // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
    // for very easy animations. If available, use these APIs to fade-in
    // the progress spinner.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        int shortAnimTime = getResources().getInteger(
                android.R.integer.config_shortAnimTime);

        mLoginStatusView.setVisibility(View.VISIBLE);
        mLoginStatusView.animate().setDuration(shortAnimTime)
                .alpha(show ? 1 : 0)
                .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mLoginStatusView.setVisibility(show ? View.VISIBLE
                        : View.GONE);
            }
        });

        mLoginFormView.setVisibility(View.VISIBLE);
        mLoginFormView.animate().setDuration(shortAnimTime)
                .alpha(show ? 0 : 1)
                .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mLoginFormView.setVisibility(show ? View.GONE
                        : View.VISIBLE);
            }
        });
    } else {
        // The ViewPropertyAnimator APIs are not available, so simply show
        // and hide the relevant UI components.
        mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
        mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);


    }
}

使用此XML

<merge xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   tools:context=".LoginActivity" >

<!-- Login progress -->
<LinearLayout
    android:id="@+id/login_status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:visibility="gone" >

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp" />

    <TextView
        android:id="@+id/login_status_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="@string/txt_Iniciando"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

<!-- Login form -->

<ScrollView
    android:id="@+id/login_form"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none" >

    <LinearLayout
        style="@style/LoginFormContainer"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/inputUser"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:digits="0123456789"
            android:hint="@string/txt_Usuario"
            android:inputType="number"
            android:maxLength="12"
            android:maxLines="1"
            android:singleLine="true" />

        <EditText
            android:id="@+id/inputPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:digits="0123456789"
            android:hint="@string/txt_Password"
            android:imeActionId="@+id/login"
            android:imeOptions="actionGo"
            android:inputType="numberPassword"
            android:maxLength="4"
            android:maxLines="1"
            android:singleLine="true" />

        <Button
            android:id="@+id/btnChange"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginTop="16dp"
            android:onClick="btnLogin_OnClick"
            android:paddingLeft="32dp"
            android:paddingRight="32dp"
            android:text="@string/txt_Ingresar" />

    </LinearLayout>
</ScrollView>

</merge>

这在3.2和4.0中都可以正常工作,但下一个

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void showProgress(int form) { //lista, estado, resultado, null
    // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
    // for very easy animations. If available, use these APIs to fade-in
    // the progress spinner.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        int shortAnimTime = getResources().getInteger(
                android.R.integer.config_shortAnimTime);

        switch (form) {
            case NULL:
                mConfigStatusView.setVisibility(View.VISIBLE);
                mConfigStatusView.animate().setDuration(shortAnimTime)
                        .alpha(0)
                        .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mConfigStatusView.setVisibility(View.GONE);
                    }
                });
                break;
            case ESTADO:
                mConfigStatusView.setVisibility(View.VISIBLE);
                mConfigStatusView.animate().setDuration(shortAnimTime)
                        .alpha(1)
                        .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mConfigStatusView.setVisibility(View.VISIBLE);
                    }
                });
                break;
        }
    } else {
        // The ViewPropertyAnimator APIs are not available, so simply show
        // and hide the relevant UI components.
        switch (form) {
            case NULL:
                mConfigStatusView.setVisibility(View.GONE);
                break;
            case ESTADO:
                mConfigStatusView.setVisibility(View.VISIBLE);
                break;
        }
    }
}

使用此XML

<merge 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:orientation="vertical"
   tools:context=".ConfigActivity" >

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/lblConfig"
            android:gravity="center"
            android:text="@string/txt_interfaz"
            android:textSize="18sp" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/txtConfig"
            android:layout_width="109dp"
            android:text="@string/txt_address"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/editConfig"
            android:clickable="false"
            android:cursorVisible="false"
            android:ems="20"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:inputType="text"
            android:maxLength="20"
            android:textSize="18sp" >

            <requestFocus />
        </EditText>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <Button
            android:id="@+id/btnPair"
            style="?android:attr/buttonStyleSmall"
            android:layout_height="wrap_content"
            android:onClick="btnPair_OnClick"
            android:text="@string/txt_pair"
            android:textSize="18sp" />

    </TableRow>
</TableLayout>

<LinearLayout
    android:id="@+id/config_status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:visibility="gone" >

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp" />

    <TextView
        android:id="@+id/config_status_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="@string/txt_Iniciando"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</merge>

只是在3.2

上崩溃应用程序

LogCat是

05-19 17:45:14.685: E/AndroidRuntime(11473): Caused by: java.lang.NullPointerException
05-19 17:45:14.685: E/AndroidRuntime(11473):    at android.view.ViewPropertyAnimator.animatePropertyBy(ViewPropertyAnimator.java:614)
05-19 17:45:14.685: E/AndroidRuntime(11473):    at android.view.ViewPropertyAnimator.animateProperty(ViewPropertyAnimator.java:564)
05-19 17:45:14.685: E/AndroidRuntime(11473):    at android.view.ViewPropertyAnimator.alpha(ViewPropertyAnimator.java:504)
05-19 17:45:14.685: E/AndroidRuntime(11473):    at com.enable.certignv.android.ConfigActivity.showProgress(ConfigActivity.java:132)

几乎所有抛出此错误的活动都包含TableLayouts,在替换4.0 GridLayouts以与3.2兼容后,但我不确定这与我的问题有多大关系。

如果我对句子的.alpha()结尾进行评论,那么代码就不会崩溃,但是它既不能正常工作,也就是说,它不会改变布局的可见性,所以第一个总是在顶部而没有其他节目如期待。

经过几天这个特殊问题后,我没有任何其他的测试想法。

有人经历过这样的事吗?不幸的是,客户端有很多Android 3.2的平板电脑,他们没有正式升级到ICS,他们不希望他们的平板电脑被越狱到4.0。

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:0)

我认为你刚刚发现了这个Android 3.2问题:

Issue 18803: ViewPropertyAnimator crashes if called early