Android 相机拍照按钮缩放动画

Android 相机拍照按钮缩放动画

前言

之前一直想做一个关于相机按钮的动态缩放动画,正好最近有时间整理了以下

演示

演示.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

第二步,我们来完成内部的白色实心圆

<?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,因为在动画进行时,外部是不动的,所以直接区分开来写。

<?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;
        });
    }

    /**
     * 界面加载完成触发,进行初始化动画
     *
     * @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() {
        /*
        * 倒数第二个和最后一个参数分别为按钮中心的X,Y点
        * 以按钮中心点进行缩放
        * */
        animation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f, take_photo_btn.getWidth() / 2f,
                take_photo_btn.getHeight() / 2f);
        //动画持续时间
        animation.setDuration(200);
        //设置为true,控件将会保持在动画结束的样子
        animation.setFillAfter(true);
    }
}

结束

经过以上几步,将会得到一个可以缩放的拍照按钮了。

感谢

如果对你有用,请点个爱心给个赞吧~~# Android 相机拍照按钮缩放动画

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jiaosiyuan785

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值