java - 动画横幅从左向右滑动(屏幕外到屏幕上)

标签 java android android-animation android-banner

我正在尝试通过单击按钮将文本横幅从线性布局外部设置为线性布局屏幕上的动画,但通知文本横幅在应用程序启动时仍保留在屏幕上。

带有通知横幅文本的我的 Activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:background="@color/white"
    android:clipChildren="false"
    android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary" >
    <ImageButton
        android:id="@+id/go_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="13dp"
        android:layout_alignLeft="@id/title"
        android:layout_alignParentLeft="true"
        android:background="@mipmap/back" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View Product"
        android:textStyle="bold"
        android:textSize="25sp"
        android:layout_centerInParent="true"
        android:paddingBottom="5dp"
        android:textAlignment="center"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:textColor="@color/white"/>

    <include android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/title"
        android:layout_alignParentRight="true"
        layout="@layout/notification_layout"/>
</RelativeLayout>

<include android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/loader"/>

<include android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignRight="@id/title"
    android:layout_alignParentRight="true"
    layout="@layout/alert_banner"/>
<ImageView
    android:id="@+id/product_image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    app:srcCompat="@drawable/slider_item_2" />

<TextView
    android:id="@+id/product_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Nike Air Mag - Limited Edition Sneakers"
    android:textStyle="bold"
    android:textSize="25sp"
    android:textAlignment="center"
    android:layout_margin="4dp"
    android:textColor="@color/print"/>

<Button
    android:id="@+id/add_to_cart"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableRight="@mipmap/add"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp"
    android:layout_marginTop="30dp"
    android:textColor="@color/white"
    android:textStyle="bold"
    android:textSize="20sp"
    android:layout_marginBottom="10dp"
    android:background="@drawable/button_rounded_corners"
    android:paddingRight="10dp"
    android:text="Add to Cart" />

在我的 java 类中,我用下面的代码开始动画

  //This is done when initializing the view
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
            product_image.getLayoutParams();
    params.setMargins(-1 * alert_info.getWidth(), 0, 0, 0);
    alert_info.setLayoutParams(params);

//when the button is clicked
ObjectAnimator animX = ObjectAnimator.ofFloat(alert_info,
                    View.TRANSLATION_X, -1* alert_info.getWidth(), 0);
            animX.setDuration(500);
            // alert_info.setVisibility(View.VISIBLE);
            animX.start();

但它不起作用,应用程序启动时 View 仍保留在屏幕上

最佳答案

这里是 View 滑动动画的代码。

在FromRightAnimation

private Animation inFromRightAnimation() {
    Animation inFromRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    inFromRight.setDuration(500);
    inFromRight.setInterpolator(new AccelerateInterpolator()); 
    return inFromRight; 
}

outToLeft动画

private Animation outToLeftAnimation() {
    Animation outtoLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    outtoLeft.setDuration(500);
    outtoLeft.setInterpolator(new AccelerateInterpolator());
    return outtoLeft;
} 

在FromLeftAnimation

private Animation inFromLeftAnimation() {
    Animation inFromLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    inFromLeft.setDuration(500);
    inFromLeft.setInterpolator(new AccelerateInterpolator());
    return inFromLeft;
}

outToRight动画

private Animation outToRightAnimation() {
    Animation outtoRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
    outtoRight.setDuration(500);
    outtoRight.setInterpolator(new AccelerateInterpolator());
    return outtoRight;
}

现在在 View 上开始动画

View.startAnimation(inFromRightAnimation());

关于java - 动画横幅从左向右滑动(屏幕外到屏幕上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49450871/

相关文章:

java - 添加自定义背景时 JButton 和 JLabel 消失

android - 无法使用 Android 模拟器收听音频

android - 如何从 android.webkit.CookieManager 获取所有 cookie 或 cookie 的 url

android - 基于android路径的自动旋转路径动画

Android fragment 动画onResume

android - 前 L 上类似 L 释放的 Touch Ripple 动画

Java:将 double 型转换为字符串

java - 根据点击的链接设置请求属性

java - 在 osx 和 ubuntu 中安装 onos 失败

android - 显示为正方形的 float 操作按钮