如何在线性布局中添加动画?

发布于 2024-11-08 23:36:48 字数 188 浏览 3 评论 0原文

我正在使用线性布局从 .net 服务器获取带有图像的消息。

当新消息到来时,布局的位置增加,新消息被一条一条地添加到布局的顶部。

问题是当新消息到来时,新消息突然添加到布局中。

我想将动画应用到布局,并使我的应用程序就像新消息出现时消息会慢慢添加到布局中一样。意味着之前的消息缓慢向下移动,新消息添加到布局的顶部。

I am using linear layout to get the message with image from .net server.

when the new message is come the position of the layout is increased and the new message is added to the top of the layout one by one.

the problem is when the new message will come,the new message is added to layout suddenly.

I want to apply animation to the layout and make my app like when the new message is come the message is added to the layout slowly. means the previous messages move down slowly and new message is added top of the layout.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抽个烟儿 2024-11-15 23:36:48

在应保存数据的 LinearLayout 上使用 android:animateLayoutChanges。添加新内容时这将触发动画。首先将旧数据下移,为更多内容腾出空间。然后是第二步,新数据将淡出到可用空间中。

示例代码

ma​​in.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/baseLL"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>

    <!-- button used to add data -->
    <Button
        android:layout_width="192dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Add Content"
        android:onClick="onAddContentClick" />

    <!-- button used to remove data -->
    <Button
        android:layout_width="192dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Remove Content"
        android:onClick="onRemoveContentClick" />

    <!-- data will be added to this LinearLayout at run time -->
    <LinearLayout
        android:id="@+id/dataLL"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:animateLayoutChanges="true"
    >

    </LinearLayout>

</LinearLayout>

basicanimation.java

package com.test.animation.basic;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class BasicAnimationActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onAddContentClick(View v) {
        LinearLayout dataLL = (LinearLayout) findViewById(R.id.dataLL);
        int dataCount = dataLL.getChildCount();
        TextView newDataTV = generateData(dataCount);
        dataLL.addView(newDataTV, 0);
    }

    public void onRemoveContentClick(View v) {
        LinearLayout dataLL = (LinearLayout) findViewById(R.id.dataLL);
        if (dataLL.getChildCount() > 0) {
            dataLL.removeViewAt(0);
        }
    }

    private TextView generateData(int dataCount) {
        TextView TV = new TextView(this);
        TV.setText("Data " + dataCount);
        TV.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
        return TV;
    }
}

Use android:animateLayoutChanges on the LinearLayout that shall hold the data. This will trigger an animation when adding new content. It starts by moving the old data down making room for more content. Then follows a second step where the new data will fade into the free space.

Example code

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/baseLL"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>

    <!-- button used to add data -->
    <Button
        android:layout_width="192dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Add Content"
        android:onClick="onAddContentClick" />

    <!-- button used to remove data -->
    <Button
        android:layout_width="192dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Remove Content"
        android:onClick="onRemoveContentClick" />

    <!-- data will be added to this LinearLayout at run time -->
    <LinearLayout
        android:id="@+id/dataLL"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:animateLayoutChanges="true"
    >

    </LinearLayout>

</LinearLayout>

basicanimation.java

package com.test.animation.basic;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class BasicAnimationActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onAddContentClick(View v) {
        LinearLayout dataLL = (LinearLayout) findViewById(R.id.dataLL);
        int dataCount = dataLL.getChildCount();
        TextView newDataTV = generateData(dataCount);
        dataLL.addView(newDataTV, 0);
    }

    public void onRemoveContentClick(View v) {
        LinearLayout dataLL = (LinearLayout) findViewById(R.id.dataLL);
        if (dataLL.getChildCount() > 0) {
            dataLL.removeViewAt(0);
        }
    }

    private TextView generateData(int dataCount) {
        TextView TV = new TextView(this);
        TV.setText("Data " + dataCount);
        TV.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
        return TV;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文