Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

AnimCheckBox:還原支付寶轉賬成功那顆打勾動畫

編輯:關於 Android 開發

這個有趣的勾選動畫由開源庫 AnimCheckBox 實現,專案地址:https://github.com/lguipeng/AnimCheckBox。點擊時圓圈填色、勾號描繪的過渡相當細緻,很適合用在「操作成功」這類狀態回饋上。

版面配置

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- app:circle_color:點擊後的填色 -->
    <!-- app:stroke_color:線條與勾號顏色 -->
    <!-- app:stroke_width:線條寬度 -->

    <com.github.lguipeng.library.animcheckbox.AnimCheckBox
        android:id="@+id/checkBox"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        app:circle_color="#1976D2"
        app:stroke_width="4dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/checkBox"
        android:layout_centerHorizontal="true"
        android:paddingTop="20dp"
        android:text="button" />

</RelativeLayout>

Activity 程式碼

package com.zzw.testanimcheckbox;

import com.github.lguipeng.library.animcheckbox.AnimCheckBox;
import com.github.lguipeng.library.animcheckbox.AnimCheckBox.OnCheckedChangeListener;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {

    private boolean temp = true;
    private AnimCheckBox checkBox;

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

        checkBox = (AnimCheckBox) findViewById(R.id.checkBox);

        // 設定初始狀態:勾選或空圈
        checkBox.setChecked(temp);

        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onChange(boolean checked) {
                if (checked) {
                    Toast.makeText(getApplicationContext(), "true", 0).show();
                } else {
                    Toast.makeText(getApplicationContext(), "false", 0).show();
                }
            }
        });

        findViewById(R.id.button).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                temp = !temp;
                // 第二個參數 true 代表播放動畫;
                // 若傳入 false,狀態會改變但不顯示動畫效果
                checkBox.setChecked(temp, true);
            }
        });
    }
}

使用重點

setChecked(boolean, boolean animate) 的第二個參數是關鍵:設為 true 才會播放描繪動畫,設為 false 則直接跳轉狀態。實務上很適合替換「轉賬成功」「任務完成」這類需要正向回饋的場景,比單純彈 Toast 更有記憶點。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved