1. 程式人生 > >安卓Animation實現APP引導使用者點選動畫

安卓Animation實現APP引導使用者點選動畫

    首先宣告本人也是學生目前是實習生,不是技術大牛,只是分享一下自己寫過的小Demo,望大家指點,共同進步,謝謝。

第一次寫部落格 不知道,從哪裡入手,有點迷啊,我直接上程式碼和效果圖了

效果圖如下


首先我用到了約束佈局,如果低版本的開發工具需要匯入一個依賴(在Moudle:app)

compile 'com.android.support.constraint:constraint-layout:1.0.2'

再然後是佈局最外面是一個約束佈局    android.support.constraint.ConstraintLayout 

然後就是兩個ImageView

    <ImageView
        android:id="@+id/watch_record_ImageView_group"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/group"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />

    <ImageView
        android:id="@+id/watch_record_ImageView_finger"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/finger"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="70dp"/>

 我們看下佈局的效果圖



手勢和小白點都有了 接下來就要開始寫動畫了

我們在res目錄下建立一個anim檔案目錄再建立一個xml檔案(檔名自取) 然後寫上兩個動畫效果 一個透明度動畫,一個縮小放大的動畫,程式碼如下

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 透明度動畫-->
    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:toAlpha="0.1"/>


    <!--縮小放大的動畫-->
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="0.5"
        android:toXScale="1.4"
        android:fromYScale="0.5"
        android:toYScale="1.4"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"
        />
    <!--android:repeatCount="-1"-->
</set>


我在這裡說下我理解的這些屬性

android:duration="1000"  這個引數是動畫的持續時間 預設為300  以毫秒為單位,想要動畫效果快慢可改變次值
fromAlpha:透明度的起始值 toAlpha:透明度的結束值,我從1到0.1 就是讓動畫越來越淡
 fromXScale:水平縮放起始值:0
 toXScale:水平縮放結束值:1
 fromYScale:豎直方向起始值
 toYScale: 豎直方向結束值
 pivotX:縮放軸點X
 pivotY:縮放軸點Y

注:上面的動畫只是小白點的動畫,手勢的動畫我們將通過程式碼來實現

動畫布局寫完了 我們開始寫Activity的程式碼了

public class WatchRecordActivity extends AppCompatActivity {
    ImageView watch_record_ImageView_finger, watch_record_ImageView_group;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.watch_record);
//     找到圖片ID  開始設定動畫
        setViews();
    }

    private void setViews() {
        watch_record_ImageView_finger = (ImageView) findViewById(R.id.watch_record_ImageView_finger);
        watch_record_ImageView_group = (ImageView) findViewById(R.id.watch_record_ImageView_group);
//    呼叫小白點動畫 小白點在第一次進行先動
        scaleAndAlphaAnimation();
    }

    //  旋轉動畫  啟動動畫
    private void RotateAnimation() {
//        設定旋轉動畫引數 第一個是起始角度(15)  第二個結束角度原點(0)
        RotateAnimation rotate = new RotateAnimation(15, 0,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
//        設定動畫完成時間  毫秒為單位
        rotate.setDuration(1500);
//        setRepeatCount為-1  是無限迴圈,因為這裡要有先後順序 所有不用
//        rotate.setRepeatCount(-1);
//        啟動動畫
        watch_record_ImageView_finger.startAnimation(rotate);
        rotate.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
//                 監聽手勢動畫結束後顯示小白點,啟動小白點動畫
//                 相互迴圈呼叫,無限執行
                watch_record_ImageView_group.setVisibility(View.VISIBLE);
//                 呼叫小白點動畫
                scaleAndAlphaAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    //    小白點動畫(放大和透明度)
    private void scaleAndAlphaAnimation() {
//          因為小白點動畫有兩個動作 一個放大 一個透明度,所有把引數寫在了XML檔案裡
//         檔名watch_record_gestures_animation  在res目錄anim目錄下
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.watch_record_gestures_animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
//                在小白點動畫結束的方法裡監聽,小白點動畫結束呼叫手勢動畫開始執行
//                隱藏小白點
                watch_record_ImageView_group.setVisibility(View.INVISIBLE);
//                啟動手勢動畫
                RotateAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
//         啟動動畫
        watch_record_ImageView_group.setAnimation(animation);

    }

}
好了 一個簡單的引導使用者的點選動畫就完成了,請大家多多關照,本人是新手上路。本文章為本人原創,部落格ID 死在鍵盤上的程式猿 (ZWX原創)


注:本Demo圖片為公司資源,下載的原始碼裡面沒有圖片,請另找圖片,其他原始碼都有,請諒解

附上原始碼下載地址點選開啟連結