如何通过连续翻译图像来创建循环动画? [英] How to create a looping animation by continuous translating an image?

查看:88
本文介绍了如何通过连续翻译图像来创建循环动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用像这样的重复图像,

By using a repeating image like this,

是否可以创建这样的动画?

is it possible to create animation like this?

推荐答案

我知道了


MainActivity.java:

I figured it out


MainActivity.java:

public class MainActivity extends Activity {

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

        final int screenWidth = getScreenDimensions(this).x;
        final int waveImgWidth = getResources().getDrawable(R.drawable.wave).getIntrinsicWidth();
        int animatedViewWidth = 0;
        while (animatedViewWidth < screenWidth) {
            animatedViewWidth += waveImgWidth;
        }
        animatedViewWidth += waveImgWidth;


        View animatedView = findViewById(R.id.animated_view);
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) animatedView.getLayoutParams();
        layoutParams.width = animatedViewWidth;
        animatedView.setLayoutParams(layoutParams);


        Animation waveAnimation = new TranslateAnimation(0, -waveImgWidth, 0, 0);
        waveAnimation.setInterpolator(new LinearInterpolator());
        waveAnimation.setRepeatCount(Animation.INFINITE);
        waveAnimation.setDuration(2500);

        animatedView.startAnimation(waveAnimation);
    }

    public static Point getScreenDimensions(Context context) {
        int width = context.getResources().getDisplayMetrics().widthPixels;
        int height = context.getResources().getDisplayMetrics().heightPixels;
        return new Point(width, height);
    }

}


activity_main.xml:


activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <View
        android:id="@+id/animated_view"
        android:layout_width="match_parent"
        android:layout_height="74dp"
        android:background="@drawable/wave_repeating_bg" />

</FrameLayout>


wave_repeating_bg.xml:


wave_repeating_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/wave"
    android:tileMode="repeat" />


drawable-xxhdpi/wave.jpg:


drawable-xxhdpi/wave.jpg:

这篇关于如何通过连续翻译图像来创建循环动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆