Android 自定义view仿微信相机单击拍照长按录视频

Android仿微信相机的拍照按钮单击拍照,长按录视频。先上效果图。

这里写图片描述


这里写图片描述

项目地址:https://github.com/c786909486/PhotoButton2/tree/v1.0

添加依赖

allprojects { repositories { ... maven { url 'https://jitpack.io' } } } dependencies { compile compile 'com.github.c786909486:PhotoButton2:v1.1' }

长按效果分析

判断是否为长按,如果是,则扩大外圆,缩小内圆。由于要扩大外圆,所以在绘制常态的外圆时不可将圆的直径设置为view的宽度或高度。

outRoundPaint.setAntiAlias(true); outRoundPaint.setColor(outCircleColor); if (isLongClick){ canvas.scale(1.2f,1.2f,width/2,height/2); } canvas.drawCircle(width/2,height/2, outRaduis, outRoundPaint); if (isLongClick){ canvas.drawCircle(width/2,height/2, innerRaduis /2.0f, innerRoundPaint); //画外原环 mCPaint.setAntiAlias(true); mCPaint.setColor(progressColor); mCPaint.setStyle(Paint.Style.STROKE); mCPaint.setStrokeWidth(circleWidth/2); RectF rectF = new RectF(0+circleWidth,0+circleWidth,width-circleWidth,height-circleWidth); canvas.drawArc(rectF,startAngle,mSweepAngle,false,mCPaint); }else { canvas.drawCircle(width/2,height/2, innerRaduis, innerRoundPaint); }

然后通过手势识别判断单击、长按、长按抬起。

mDetector = new GestureDetectorCompat(context, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onSingleTapConfirmed(MotionEvent e) { //单击 isLongClick = false; if (listener != null) { listener.onClick(TakePhotoButton.this); } return super.onSingleTapConfirmed(e); } @Override public void onLongPress(MotionEvent e) { //长按 isLongClick = true; postInvalidate(); if (listener != null) { listener.onLongClick(TakePhotoButton.this); } } }); mDetector.setIsLongpressEnabled(true); @Override public boolean onTouchEvent(MotionEvent event) { mDetector.onTouchEvent(event); switch(MotionEventCompat.getActionMasked(event)) { case MotionEvent.ACTION_DOWN: isLongClick = false; break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: if (isLongClick) { isLongClick = false; postInvalidate(); if (this.listener != null) { this.listener.onLongClickUp(this); } } break; } return true; }

自定义接口对各个状态进行监听

public interface OnProgressTouchListener { /** * 单击 * @param photoButton */ void onClick(TakePhotoButton photoButton); /** * 长按 * @param photoButton */ void onLongClick(TakePhotoButton photoButton); /** * 长按抬起 * @param photoButton */ void onLongClickUp(TakePhotoButton photoButton); void onFinish(); }

最后,给外圆弧添加动画

public void start() { ValueAnimator animator = ValueAnimator.ofFloat(mmSweepAngleStart, mmSweepAngleEnd); animator.setInterpolator(new LinearInterpolator()); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { mSweepAngle = (float) valueAnimator.getAnimatedValue(); //获取到需要绘制的角度,重新绘制 invalidate(); } }); //这里是时间获取和赋值 ValueAnimator animator1 = ValueAnimator.ofInt(mLoadingTime, 0); animator1.setInterpolator(new LinearInterpolator()); animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { int time = (int) valueAnimator.getAnimatedValue(); } }); AnimatorSet set = new AnimatorSet(); set.playTogether(animator, animator1); set.setDuration(mLoadingTime * 1000); set.setInterpolator(new LinearInterpolator()); set.start(); set.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); clearAnimation(); isLongClick = false; postInvalidate(); if (listener != null) { listener.onFinish(); } } }); }

最后,在activity中给控件设置监听即可。

buttontake.setOnProgressTouchListener(new TakePhotoButton.OnProgressTouchListener() { @Override public void onClick(TakePhotoButton photoButton) { Toast.makeText(MainActivity.this,"单机",Toast.LENGTH_SHORT).show(); } @Override public void onLongClick(TakePhotoButton photoButton) { Toast.makeText(MainActivity.this,"长按",Toast.LENGTH_SHORT).show(); buttontake.start(); } @Override public void onLongClickUp(TakePhotoButton photoButton) { onFinish(); } @Override public void onFinish() { Toast.makeText(MainActivity.this,"录制结束",Toast.LENGTH_SHORT).show(); } });

button.s

下面贴上完整的代码

TakePhotoButton:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/df9a4938ca25443378b77033998001c3.html