转载请注明出处:https://blog.youkuaiyun.com/a512337862/article/details/90602156
前言
因为最近的项目需要使用录音功能,开始的想法是Button+OnTouchListener+Dialog实现,在大部分手机中都没问题,只有MI8会偶尔无法触发MotionEvent.ACTION_UP,导致程序异常。所以就自己写了个自定义View来实现,主要也是通过监听
OnTouchListener+Dialog来实现。这里只实现了自定义View,并不涉及录音和播放。效果图如下:
代码
代码并不复杂,配合注释应该很容易理解。
/**
* Author : BlackHao
* Time : 2019/4/18 14:03
* Description : 自定义录音按钮布局界面
*/
public class PressedView extends View implements View.OnTouchListener {
private int normalRes;
private String normalText = "";
private int pressedRes;
private String pressedText = "";
//
private Paint paint;
private Rect rect;
//当前是否是按下状态
private boolean isPressed = false;
//
private PressCallback callback;
//按下的位置y坐标
private int pressedY = 0;
//当前是否是outSize
private boolean isOutSize = false;
//字体dp大小
private static int TEXT_SIZE = 20;
//对话框相关
private Dialog soundVolumeDialog = null;
//音量图片
private ImageView soundVolumeImg = null;
//对话框背景
private RelativeLayout soundVolumeLayout = null;
public PressedView(Context context) {
super(context);
init();
}
public PressedView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public PressedView(Contex