【问题标题】:How to animate a wide image (right to left) as the background of my activity?如何将宽图像(从右到左)设置为我的活动背景?
【发布时间】:2016-12-04 12:38:20
【问题描述】:

我需要将我的宽幅图像 (1800x1201px) 设置为我的活动背景,这样我就可以在上方放置应用徽标和登录/注册按钮。

我尝试使用FrameLayoutImageView。动画正在运行,但图像没有填满整个 FrameLayout 高度。

活动

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/splash_base_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="br.com.myapp.app.activities.SplashActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/splash_bg_image"
        android:src="@drawable/splash_background"
        android:contentDescription=""
        android:scaleType="matrix"
        android:adjustViewBounds="false"
        android:cropToPadding="false" />
</FrameLayout>

活动类

public class SplashActivity extends AppCompatActivity {

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

        animateBackground();
    }

    private void animateBackground() {
        final ImageView splashImage = (ImageView) findViewById(R.id.splash_bg_image);

        splashImage.startAnimation(outToLeftAnimation());
    }

    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(20000L);
        return outtoLeft;
    }
}

以下是它目前在 Android Studio 上的预览方式:

我尝试将layout_height 设置为match_parentwrap_content,但找不到正确的scaleType 使其填充屏幕高度并创建从右到左的动画。

更新

我为 xxx-hdpi 设备创建了一个更大的图像 (3840x2560),现在预览会以我需要的方式显示图像,但是当我运行应用程序时,它的顶部和底部都有空白。

我还需要创建一种“回旋镖”动画,这样当图像从右向左移动时,它会将动画从左向右反转,依此类推。

更新图片:

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    android:adjustViewBounds="false" 更改为android:adjustViewBounds="true" 并使其高度为match_parent,就完成了。

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 也没有用:imgur.com/a/UQfEh。我尝试了 FrameLayout 和 RelativeLayout 容器。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 2020-02-03
      相关资源
      最近更新 更多