2017-04-21 91 views
1

我知道我的问题需要大量downvote,但有人帮助我,我想添加动画到我的imageview例如,当我单击按钮从底部出现并向上移动。这样图象动画图像开始自下而上

enter image description here

+0

请参阅下面的答案。我已添加工作代码。希望这会有所帮助 – FAT

回答

1

1.创建move.xml定义animation

<?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:fromYDelta="100%p" 
     android:toYDelta="0%p" 
     android:duration="1000" /> 
</set> 

2.用于示出ButtonImageView创建activity_animation.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_animation" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="16dp"> 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@mipmap/ic_launcher" 
     android:layout_centerHorizontal="true" 
     android:visibility="gone"/> 

    <Button 
     android:id="@+id/btnStart" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Start" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true"/> 

</RelativeLayout> 

AnimationActivity应该是这样的:

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.Toast; 

public class AnimationActivity extends AppCompatActivity implements Animation.AnimationListener { 

    ImageView imageIcon; 
    Button btnStart; 

    // Animation 
    Animation animMoveToTop; 

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

     imageIcon = (ImageView) findViewById(R.id.icon); 
     btnStart = (Button) findViewById(R.id.btnStart); 

     // load the animation 
     animMoveToTop = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move); 

     // set animation listener 
     animMoveToTop.setAnimationListener(this); 

     // button click event 
     btnStart.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       imageIcon.setVisibility(View.VISIBLE); 

       // start the animation 
       imageIcon.startAnimation(animMoveToTop); 
      } 
     }); 
    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     // Take any action after completing the animation 
     // check for move animation 
     if (animation == animMoveToTop) { 
      Toast.makeText(getApplicationContext(), "Animation Stopped", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onAnimationStart(Animation animation) { 
     // TODO Auto-generated method stub 
    } 
} 

OUTPUT:

enter image description here

希望这将有助于〜