android - 动画图像裁剪

标签 android android-layout android-animation android-ui

我在使用 Android 动画 API 时遇到了很多麻烦。

我需要在屏幕 (600px * 1024px) 上从左下角到顶部对图像 (400px * 600px) 进行动画处理。此外,在动画开始时,大部分图像应该位于屏幕之外。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/common_background">

<ImageView android:id="@+id/hand"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:scaleType="matrix"
           android:src="@drawable/howto_throw_hand" />

</RelativeLayout>

Activity 的逻辑:

public class MyActivity extends Activity
{
    ImageView hand;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        hand = (ImageView)findViewById(R.id.hand);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

        //initial positioning
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                new ViewGroup.MarginLayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT
                )
        );
        layoutParams.setMargins(400, 600, 0, 0);
        hand.setLayoutParams(layoutParams);

        //making animation
        TranslateAnimation animation = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE, -100,
            Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE, -1000
        );

        animation.setInterpolator(new LinearInterpolator());
        animation.setDuration(3000);

        //applying animation
        hand.startAnimation(animation);
    }
}

初始状态截图: enter image description here

问题是在动画图像被裁剪的过程中,就像这个视频:video with animation

我错过了很多时间却没能解决问题 =(

可以做些什么来解决这个问题?

提前致谢!

~~~~~~~~~~~~~~~~~~解决了~~~~~~~~~~~~~~~~~~

正如 ben75 所说“......问题在于初始位置。当你执行 TranslateAnimation 时:图像不会移动:它会以不同的翻译重绘”和“动画只是翻译第一个裁剪的图像。”

我是如何解决这个问题的:

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/common_background">

<ImageView android:id="@+id/hand"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:visibility="invisible"
           android:src="@drawable/howto_throw_hand" />

</RelativeLayout>

Activity 的逻辑:

public class MyActivity extends Activity
{
    ImageView hand;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        hand = (ImageView)findViewById(R.id.hand);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

        //initial positioning — removed

        //making animation
        TranslateAnimation animation = new TranslateAnimation(400, 300, 600, 0);

        animation.setInterpolator(new LinearInterpolator());
        animation.setDuration(3000);

        //applying animation
        hand.startAnimation(animation);
    }
}

宾果!

最佳答案

我觉得是初始位置的问题。当您执行 TranslateAnimation 时:图像不会移动:它会以不同的翻译重绘。 您的代码中指定的边距使得图像最初部分超出屏幕,因此图像最初被裁剪。动画只是翻译了第一张裁剪后的图像。

所以我建议是这样的:

    layoutParams.setMargins(0, 0, 0, 0); //left, top are the final position of the image
    hand.setLayoutParams(layoutParams);

    //making animation
    TranslateAnimation animation = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, 400, Animation.ABSOLUTE, 0,
        Animation.RELATIVE_TO_SELF, 600, Animation.ABSOLUTE, 0
    );

关于android - 动画图像裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13722659/

相关文章:

android - 动画时在 RecyclerView 内折叠 CardView

c# - Intent 帮助 Xamarin C# Android?

java - 如何使 ImageView 缩放以为其下方的控件留出空间?

java - 在 GraphView 中向 Y 轴添加左填充

android - StackOverflowError,我不知道为什么

android - 列数更改时动画回收器 View 网格

android - 适用于 Android 的 WebP

Android:点击保存后相机崩溃

android - 如何将 alpha channel 添加到位图

android - Android 上的 Alpha 淡入淡出方向