Android 相機(jī)拍照按鈕縮放動(dòng)畫
前言
之前一直想做一個(gè)關(guān)于相機(jī)按鈕的動(dòng)態(tài)縮放動(dòng)畫,正好最近有時(shí)間整理了以下
演示
演示.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)部的白色實(shí)心圓
<?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
第三步,設(shè)置按鈕
使用FrameLayout布局,進(jìn)行嵌套兩個(gè)shape摇予,因?yàn)樵趧?dòng)畫進(jìn)行時(shí)目代,外部是不動(dòng)的,所以直接區(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:
//按下時(shí)啟動(dòng)動(dòng)畫
take_photo_btn.startAnimation(animation);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
//抬起或取消動(dòng)作時(shí),清除動(dòng)畫
take_photo_btn.clearAnimation();
break;
}
return false;
});
}
/**
* 界面加載完成觸發(fā),進(jìn)行初始化動(dòng)畫
*
* @date: 2021/1/27 18:45
* @author: SiYuan Jiao
*/
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
initAnimation();
}
}
/**
* 初始化動(dòng)畫
*
* @date: 2021/1/27 18:46
* @author: SiYuan Jiao
*/
private void initAnimation() {
/*
* 倒數(shù)第二個(gè)和最后一個(gè)參數(shù)分別為按鈕中心的X,Y點(diǎn)
* 以按鈕中心點(diǎn)進(jìn)行縮放
* */
animation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f, take_photo_btn.getWidth() / 2f,
take_photo_btn.getHeight() / 2f);
//動(dòng)畫持續(xù)時(shí)間
animation.setDuration(200);
//設(shè)置為true,控件將會(huì)保持在動(dòng)畫結(jié)束的樣子
animation.setFillAfter(true);
}
}
結(jié)束
經(jīng)過以上幾步,將會(huì)得到一個(gè)可以縮放的拍照按鈕了绸吸。
感謝
如果對(duì)你有用,請(qǐng)點(diǎn)個(gè)愛心給個(gè)贊吧~~