Android 相機拍照按鈕縮放動畫

Android 相機拍照按鈕縮放動畫

前言

之前一直想做一個關于相機按鈕的動態(tài)縮放動畫,正好最近有時間整理了以下

演示

演示.gif

正文

round_border.xml

首先,第一步,我們完成其外部的圓形線,使用shape即可。

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <stroke
    android:width="2px"
    android:color="@color/white"/>

    <solid
        android:width="96px"
        android:height="96px"
        android:color="@color/black" />
</shape>
round_border.png

round_button.xml

第二步,我們來完成內(nèi)部的白色實心圓

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="oval">
    <solid android:color="@color/white" />
    <size
        android:width="76px"
        android:height="76px" />
</shape>
round_button.png

activity_main.xml

第三步,設置按鈕
使用FrameLayout布局,進行嵌套兩個shape,因為在動畫進行時,外部是不動的,所以直接區(qū)分開來寫。

<?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:background="@android:color/background_dark"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">

        <View
            android:layout_width="96px"
            android:layout_height="96px"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/round_border" />

        <Button
            android:id="@+id/take_photo"
            android:layout_width="76px"
            android:layout_height="76px"
            android:layout_gravity="center_horizontal|center_vertical"
            android:background="@drawable/round_button" />
    </FrameLayout>
</RelativeLayout>

MainActivity

public class MainActivity extends AppCompatActivity {
    private Button take_photo_btn;
    private Animation animation = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化按鈕
        take_photo_btn = findViewById(R.id.take_photo);
        //按鈕觸摸事件
        take_photo_btn.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    //按下時啟動動畫
                    take_photo_btn.startAnimation(animation);
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    //抬起或取消動作時,清除動畫
                    take_photo_btn.clearAnimation();
                    break;
            }
            return false;
        });
    }

    /**
     * 界面加載完成觸發(fā),進行初始化動畫
     *
     * @date: 2021/1/27 18:45
     * @author: SiYuan Jiao
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus) {
            initAnimation();
        }
    }

    /**
     * 初始化動畫
     *
     * @date: 2021/1/27 18:46
     * @author: SiYuan Jiao
     */
    private void initAnimation() {
        /*
        * 倒數(shù)第二個和最后一個參數(shù)分別為按鈕中心的X,Y點
        * 以按鈕中心點進行縮放
        * */
        animation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f, take_photo_btn.getWidth() / 2f,
                take_photo_btn.getHeight() / 2f);
        //動畫持續(xù)時間
        animation.setDuration(200);
        //設置為true,控件將會保持在動畫結束的樣子
        animation.setFillAfter(true);
    }
}

結束

經(jīng)過以上幾步,將會得到一個可以縮放的拍照按鈕了。

感謝

如果對你有用,請點個愛心給個贊吧~~

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
禁止轉載,如需轉載請通過簡信或評論聯(lián)系作者。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容