Android 如何在动画结束时在屏幕上显示部分图像

Android 如何在动画结束时在屏幕上显示部分图像,android,animation,Android,Animation,我正在创建一个动画,其中一个图像从一个方向转换到另一个方向,动画将图像的一半移出屏幕,但动画结束时图像显示为完整。我只想在动画后显示图像的一半 当前,我在动画开始时在图像视图中使用完整图像,并在动画结束时将其替换为半图像,但它显示图像更改反射,这看起来很尴尬,这是我的实际问题 下面是我的xml和类文件 动画代码 <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration

我正在创建一个动画,其中一个图像从一个方向转换到另一个方向,动画将图像的一半移出屏幕,但动画结束时图像显示为完整。我只想在动画后显示图像的一半

当前,我在动画开始时在图像视图中使用完整图像,并在动画结束时将其替换为半图像,但它显示图像更改反射,这看起来很尴尬,这是我的实际问题

下面是我的xml和类文件

动画代码

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration = "2000"
    android:fillBefore="true"
    android:fillEnabled="true"
    android:fromXDelta = "-300%"
    android:fromYDelta="200%"
    android:toXDelta="60%">

</translate>

animimv5.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                imv5.setBackgroundResource(R.drawable.img5);
            }
            @Override
            public void onAnimationRepeat(Animation animation) {

            }
            @Override
            public void onAnimationEnd(Animation animation) {
                imv5.setBackgroundResource(R.drawable.img5_2);
            }
        });

setAnimationListener(新的AnimationListener(){
@凌驾
onAnimationStart上的公共无效(动画){
imv5.setBackgroundResource(R.drawable.img5);
}
@凌驾
onAnimationRepeat上的公共无效(动画){
}
@凌驾
onAnimationEnd上的公共无效(动画){
imv5.setBackgroundResource(R.drawable.img5_2);
}
});

您可以尝试以下操作:

在xml中(将其添加到设置参数)
android:fillAfter
或使用类source
setFillAfter(布尔值)

设置为true时,动画变换将在 动画结束了。根据文件 设置为true时,动画变换将在 动画结束了。默认值为false。如果未启用fillEnabled 如果设置为true,则不会在视图上设置动画,则为fillAfter 假设是真的

有关更多提示和解释,请查看

希望能有帮助……)

给您,先生:

MainActivity :

    public class MainActivity extends Activity {

    private View animatedView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        animatedView = findViewById(R.id.view);
    }

    public void animate(View view) {
        int targetX = ((View) animatedView.getParent()).getWidth() - animatedView.getWidth() / 2;
        ObjectAnimator.ofFloat(animatedView, "x", 0, targetX).setDuration(1000).start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
布局XML:

    <RelativeLayout 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"
                tools:context=".MainActivity">

    <TextView
            android:id="@+id/view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#CC0000"
            android:padding="30dp"
            android:textColor="@android:color/white"
            android:text="@string/hello_world"/>
    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Animate"
            android:onClick="animate"
            android:id="@+id/button"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"/>

</RelativeLayout>


检查这篇文章:fillAfter=true fillEnabled=truetry使用
fillAfter(true)
你的目标是什么SDK?我可以使用ObjectAnimator向您发布一个完整的解决方案,但它只能从API级别11开始工作。我需要在API级别8上完成它。谢谢@Juangcg和Siddhesh,它工作得非常完美。