android官方的demo中android:animateLayoutChanges="true"的应用

来源:互联网 发布:游戏本品牌 知乎 编辑:程序博客网 时间:2024/06/07 08:01

layout动画在每次布局发生变化的时候系统调用的一个预加载动画效果,使用layout动画可以让布局的变化过度看起来更自然。使用起来很简单,只需在控件中添加一个属性android:animateLayoutChanges="true"就可以了,系统默认是不会启动layout动画的。

以下是官方的演示效果:


布局文件R.layout.activity_layout_changes:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <!-- A vertical LinearLayout in a ScrollView. This emulates a ListView (and is lighter weight         than a ListView when there aren't many rows). -->    <ScrollView android:layout_width="match_parent"        android:layout_height="match_parent">        <!-- Note that this LinearLayout has the "animateLayoutChanges" property set to true.             This tells the framework to automatically animate child views (in this case, rows)             as they are added to and removed from the LinearLayout. -->        <LinearLayout android:id="@+id/container"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical"            android:showDividers="middle"            android:divider="?android:dividerHorizontal"            android:animateLayoutChanges="true"            android:paddingLeft="16dp"            android:paddingRight="16dp" />    </ScrollView>    <!-- The "empty" view to show when there are no items in the "list" view defined above. -->    <TextView android:id="@android:id/empty"        style="?android:textAppearanceSmall"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:padding="32dp"        android:text="@string/message_empty_layout_changes"        android:textColor="?android:textColorSecondary" /></FrameLayout>
strings.xml里的
<string name="message_empty_layout_changes">Add an item above to get started.</string>

<string name="action_remove_item">Remove item</string>

菜单资源R.menu.activity_layout_changes:

<menu xmlns:android="http://schemas.android.com/apk/res/android">    <!-- Adds an item to the list. -->    <item android:id="@+id/action_add_item"        android:title="@string/action_add_item"        android:icon="@drawable/ic_action_new"        android:showAsAction="ifRoom" /></menu>

R.layout.list_item_example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="?android:listPreferredItemHeightSmall"    android:orientation="horizontal"    android:showDividers="middle"    android:divider="?android:dividerVertical"    android:dividerPadding="8dp"    android:gravity="center">    <!-- Dummy text view that will display the name of a random country. -->    <TextView android:id="@android:id/text1"        style="?android:textAppearanceMedium"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"        android:paddingLeft="?android:listPreferredItemPaddingLeft" />    <!-- A button that, when pressed, will delete this list item row from its container. -->    <ImageButton android:id="@+id/delete_button"        android:layout_width="48dp"        android:layout_height="match_parent"        android:src="@drawable/ic_list_remove"        android:background="?android:selectableItemBackground"        android:contentDescription="@string/action_remove_item" /></LinearLayout>

activity文件:

public class LayoutChangesActivity extends Activity {    /**     * The container view which has layout change animations turned on. In this sample, this view     * is a {@link android.widget.LinearLayout}.     */    private ViewGroup mContainerView;    /**     * A static list of country names.     */    private static final String[] COUNTRIES = new String[]{            "Belgium", "France", "Italy", "Germany", "Spain",            "Austria", "Russia", "Poland", "Croatia", "Greece",            "Ukraine",    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_layout_changes);                mContainerView = (ViewGroup) findViewById(R.id.container);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        super.onCreateOptionsMenu(menu);        getMenuInflater().inflate(R.menu.activity_layout_changes, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        switch (item.getItemId()) {            case android.R.id.home:                // Navigate "up" the demo structure to the launchpad activity.                // See http://developer.android.com/design/patterns/navigation.html for more.                NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));                return true;            case R.id.action_add_item:                // Hide the "empty" view since there is now at least one item in the list.                findViewById(android.R.id.empty).setVisibility(View.GONE);                addItem();                return true;        }        return super.onOptionsItemSelected(item);    }    private void addItem() {        // Instantiate a new "row" view.        final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(                R.layout.list_item_example, mContainerView, false);        // Set the text in the new row to a random country.        ((TextView) newView.findViewById(android.R.id.text1)).setText(                COUNTRIES[(int) (Math.random() * COUNTRIES.length)]);        // Set a click listener for the "X" button in the row that will remove the row.        newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                // Remove the row from its parent (the container view).                // Because mContainerView has android:animateLayoutChanges set to true,                // this removal is automatically animated.                mContainerView.removeView(newView);                // If there are no rows remaining, show the empty view.                if (mContainerView.getChildCount() == 0) {                    findViewById(android.R.id.empty).setVisibility(View.VISIBLE);                }            }        });        // Because mContainerView has android:animateLayoutChanges set to true,        // adding this view is automatically animated.        mContainerView.addView(newView, 0);    }    }



0 0