Ping App动画Android

时间:2015-02-13 05:37:57

标签: android animation

我只是好奇。当我从其他活动/片段中启动/替换一个活动/片段时,如何在ping app中实现动画。在IOS中,我们可以添加椭圆形的遮罩层并修改alpha值。我如何在android中实现这个?

1 个答案:

答案 0 :(得分:2)

这是解决方案:

创建shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="@android:color/black"/>
</shape>

将其设置为ImageView

<ImageView
    android:layout_width="60dp" //or another
    android:layout_height="60dp" //or another
    android:id="@+id/ping"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/shape" />

然后创建两个动画scale_up.xmlscale_down.xml

scale_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="1.5"
        android:toYScale="1.5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"
        android:fillBefore="true" />
</set>

scale_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:fromXScale="1.5"
        android:fromYScale="1.5"
        android:toXScale="1.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"
        android:fillBefore="true" />
</set>

最后代码中的startAnimation()

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ImageView ping = (ImageView) findViewById(R.id.ping);

    final Animation scaleDown = AnimationUtils.loadAnimation(this, R.anim.scale_down);
    final Animation scaleUp = AnimationUtils.loadAnimation(this, R.anim.scale_up);

    scaleDown.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            ping.startAnimation(scaleUp);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

    scaleUp.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            ping.startAnimation(scaleDown);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

    ping.startAnimation(scaleUp);
}