在Android中更改动画后的实际位置

时间:2017-04-11 10:18:25

标签: android android-animation

我在我的应用中使用动画。有一个图像我在哪里实现动画 但是在动画之后我无法点击图像。我搜索并阅读了关于此的stackoverflow答案,最后发现动画只是戏剧性的原始像素。所以,我需要移动实际位置的观点。我发现这可以通过在动画后设置布局参数来实现。但我不知道如何做到这一点。这是我用于动画的代码。

anim.xml

 <?xml version="1.0" encoding="utf-8"?>
 <set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">

<translate
    android:fromXDelta="0%p"
    android:toXDelta="-50%p"
    android:duration="500" />
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0.45"
    android:toYScale="0.45" >
</scale>

 </set> 

activity_main.xml中

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <ImageView
    android:id="@+id/btnCapturePicture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="17dp"
    app:srcCompat="@drawable/capture_picture" />

 </RelativeLayout>

MainActivity.java

  ImageView btnCameraPic = (ImageView) findViewById(R.id.btnCapturePicture);
  Animation animation= AnimationUtils.loadAnimation(this, R.anim.animation);
  btnCameraPic.startAnimation(animation);

  btnCameraPic.setAnimationListener(new Animation.AnimationListener()
{
@Override
public void onAnimationStart(Animation arg0) 
{
}           
@Override
public void onAnimationRepeat(Animation arg0) 
{
}           
@Override
public void onAnimationEnd(Animation arg0) 
{
}
});     

如何在动画之后设置新的布局参数因为我不知道什么是新的新参数是否有任何方法可以从 animation.xml 文件中获取这些新参数并将其设置为在AnimationListener btnCameraPic

我在stackoverflow上阅读了很多问题但主要是在java类而不是xml文件中使用动画。任何人都可以让我知道动画后的新参数是什么?

2 个答案:

答案 0 :(得分:0)

尝试在anim.xml文件中添加以下行。

android:fillAfter = "true"

答案 1 :(得分:0)

简答:

您应该使用属性动画

<强>原因:

  

属性动画如何与视图动画不同   视图动画系统提供仅为View对象设置动画的功能,因此如果要为非View对象设置动画,则必须实现自己的代码才能执行此操作。视图动画系统也受到限制,因为它仅将View对象的一些方面暴露给动画,例如视图的缩放和旋转,而不是背景颜色。

     

视图动画系统的另一个缺点是它只修改了绘制视图的位置,而不是实际的视图本身。例如,如果您设置了一个按钮以在屏幕上移动,则该按钮会正确绘制,但您可以单击该按钮的实际位置不会更改,因此您必须实现自己的逻辑来处理此问题。

     

使用属性动画系统,可以完全删除这些约束,并且可以为任何对象(视图和非视图)的任何属性设置动画,并且实际修改了对象本身。属性动画系统在执行动画方面也更加强大。在较高级别,您可以将动画师分配给要设置动画的属性,例如颜色,位置或大小,并可以定义动画的各个方面,例如多个动画师的插值和同步。

这是链接:

https://developer.android.com/guide/topics/graphics/prop-animation.html