裁剪动画图像

Yul*_*kov 6 android android-ui android-animation android-layout

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>
Run Code Online (Sandbox Code Playgroud)

活动的逻辑:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

以及初始状态的屏幕截图: 在此输入图像描述

问题在于,在动画过程中,图像被裁剪为类似于此视频:带动画的视频

我错过了很多时间,但未能解决问题=(

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

先感谢您!

~~~~~~~~~~~~~~~~~~已解决~~~~~~~~~~~~~~~~~~

正如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>
Run Code Online (Sandbox Code Playgroud)

活动的逻辑:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

答对了!

ben*_*n75 3

我认为问题出在最初的立场上。当您执行 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
    );
Run Code Online (Sandbox Code Playgroud)