使用android防止某些视图被动画化:animateLayoutChanges =“true”

时间:2015-06-22 19:20:47

标签: android

我有一个水平线性布局,有3个视图。打开和关闭按钮和文本视图。加载时,关闭按钮设置为

setVisibility(View.GONE);

所以它只显示一个imageview,open和带有一些文本的textview。我也在使用

android:animateLayoutChanges="true"

在我的父布局上,在打开和关闭时为textview设置动画。一切正常,除非我打开/关闭时我可以看到两个按钮的动画分别设置为GONE和VISIBLE。我不想在这两个ImageView上播放动画。关于如何删除这两个视图上的动画的任何想法?提前谢谢。

XML文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/CuFScrollView"
style="@style/ScrollView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#A0A0A0"
>
<LinearLayout
    style="@style/LinearLayoutVertical"
    android:paddingTop="20dp"

    >

    <TextView
        android:text="Basic Management Plan for Tactical Field Care"
        android:id="@+id/header"
        style="@style/Header"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#606060"
        android:padding="5dp"
        android:animateLayoutChanges="true"
        >

        <ImageView
            android:id="@+id/open"
            android:src="@drawable/open"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:onClick="open"
            android:layout_gravity="center_vertical"
            android:animateLayoutChanges="false"
            />

        <ImageView
            android:id="@+id/close"
            android:src="@drawable/close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:onClick="close"
            android:layout_gravity="center_vertical"
            android:animateLayoutChanges="false"
            />

        <TextView
            android:id="@+id/stepOne"
            android:text="Step 1:"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="0dp"

            style="@style/Bullet"
            />

    </LinearLayout>

</LinearLayout>

Java文件

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

public class Careunderfirestudy extends AppCompatActivity{

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

    ImageView close = (ImageView)findViewById(R.id.close);

    close.setVisibility(View.GONE);

    }

public void open(View view){
    TextView stepOne = (TextView)findViewById(R.id.stepOne);
    stepOne.setText("Casualties should be extricated from burning vehicles or buildings and moved to places of relative safety. Do what is necessary to stop the burning process.");
    ImageView close = (ImageView)findViewById(R.id.close);
    ImageView open = (ImageView)findViewById(R.id.open);
    close.setVisibility(View.VISIBLE);
    open.setVisibility(View.GONE);
}

public void close(View view){
    TextView stepOne = (TextView)findViewById(R.id.stepOne);
    stepOne.setText("Step 1:");
    ImageView close = (ImageView)findViewById(R.id.close);
    ImageView open = (ImageView)findViewById(R.id.open);
    close.setVisibility(View.GONE);
    open.setVisibility(View.VISIBLE);
}

}

2 个答案:

答案 0 :(得分:9)

问题是:使用android防止某些视图动画:animateLayoutChanges =“true”我在上一个答案中没有看到这个问题的答案。

我遇到了同样的问题,我解决了它禁用临时LayoutTransition的出现和消失类型。

这里的例子。

在不动画的情况下隐藏视图:

LayoutTransition lt = ((ViewGroup)view.getParent()).getLayoutTransition();
lt.disableTransitionType( LayoutTransition.DISAPPEARING );
view.setVisibility(View.GONE);
lt.enableTransitionType( LayoutTransition.DISAPPEARING );

在不动画的情况下显示视图:

LayoutTransition lt = ((ViewGroup)view.getParent()).getLayoutTransition();
lt.disableTransitionType( LayoutTransition.APPEARING );
view.setVisibility(View.VISIBLE);
lt.enableTransitionType( LayoutTransition.APPEARING );

这不会阻止其他视图因动态更改为视图可见性更改(它们在LayoutTransition.CHANGE_APPEARING和LayoutTransition.CHANGE_DISAPPEARING转换类型下设置动画)。

可以为了安全而对布局转换进行非空检查。

答案 1 :(得分:0)

您只需使用1个按钮即可更改图像:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#606060"
    android:padding="5dp"
    android:animateLayoutChanges="true"
    android:id="@+id/parent"
    >
    <ImageView
            android:id="@+id/toggleStep"
            android:src="@drawable/open"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:onClick="toggle"
            android:layout_gravity="center_vertical"
            />
protected void onCreate(Bundle savedInstanceState) {
    ...

    LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
    parent.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);

    ...
}

private boolean mOpen = false;

public void toggle(View view){
        TextView stepOne = (TextView)findViewById(R.id.stepOne);
        ImageView toggle = (ImageView)findViewById(R.id.toggleStep);

        if(mOpen){
            stepOne.setText("Step 1:");
            toggle.setImageResource(R.drawable.open);
        }
        else{
            stepOne.setText("Casualties should be extricated from burning vehicles or buildings and moved to places of relative safety. Do what is necessary to stop the burning process.");
            toggle.setImageResource(R.drawable.close);
        }
        mOpen = !mOpen;
    }