我知道我的问题可能会被嘲笑,但我想给我的
添加动画效果。例如,当我点击按钮时,它可以从底部出现并向上移动,就像这张图片一样。
![ImageView](https://istack.dev59.com/JEvSn.webp?s=32&g=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. 创建 activity_animation.xml
用于显示 Button
和 ImageView
。
<?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>
3. 你的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
}
}
输出:
希望这能有所帮助~