1. 程式人生 > >Android animation呼吸動畫 心形動畫

Android animation呼吸動畫 心形動畫

首先還是在res 下面建立一個anim的資料夾

然後建立一個resource 的檔案 名為size.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:duration="2000"
android:pivotX="50%"
android:pivotY="50%"
android:fromXScale="1"
android:fromYScale="1"
android:toXScale=
"1.2" android:toYScale="1.2" android:interpolator="@android:interpolator/bounce" android:repeatMode="reverse" android:repeatCount="infinite"/> <alpha android:repeatCount="infinite" android:duration="2000" android:fromAlpha="0.1" android:toAlpha="1" android:repeatMode="reverse" /> </set>

圖片如下:


1:佈局裡面

<?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:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ImageView android:layout_width="200dp" android:layout_height="200dp" android:layout_centerInParent="true" android:onClick="imageSize" android:src="@mipmap/heart" android:id="@+id/image_scale"/> </RelativeLayout>

2:MainActivity 檔案:

package tech.androidstudio.animationdemo;

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.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements Animation.AnimationListener {

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


    public void imageSize(View view) {
        ImageView image_scale = (ImageView) findViewById(R.id.image_scale);
        Animation animation = AnimationUtils.loadAnimation(this,R.anim.size);
        image_scale.startAnimation(animation);
    }
}