侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

微信(小程序)  /  管理员 发布于 5年前   312

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:

import android.animation.Animator;import android.animation.AnimatorListenerAdapter;import android.animation.AnimatorSet;import android.animation.ValueAnimator;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.support.annotation.Nullable;import android.support.v4.view.GestureDetectorCompat;import android.support.v4.view.MotionEventCompat;import android.util.AttributeSet;import android.view.GestureDetector;import android.view.MotionEvent;import android.view.View;import android.view.animation.LinearInterpolator;/** * Created by CKZ on 2017/8/9. */public class TakePhotoButton extends View {  private float circleWidth;//外圆环宽度  private int outCircleColor;//外圆颜色  private int innerCircleColor;//内圆颜色  private int progressColor;//进度条颜色  private Paint outRoundPaint = new Paint(); //外圆画笔  private Paint mCPaint = new Paint();//进度画笔  private Paint innerRoundPaint = new Paint();  private float width; //自定义view的宽度  private float height; //自定义view的高度  private float outRaduis; //外圆半径  private float innerRaduis;//内圆半径  private GestureDetectorCompat mDetector;//手势识别  private boolean isLongClick;//是否长按  private float startAngle = -90;//开始角度  private float mmSweepAngleStart = 0f;//起点  private float mmSweepAngleEnd = 360f;//终点  private float mSweepAngle;//扫过的角度  private int mLoadingTime;  public TakePhotoButton(Context context) {    this(context,null);}  public TakePhotoButton(Context context, @Nullable AttributeSet attrs) {    this(context, attrs,0);  }  public TakePhotoButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);    init(context,attrs);  }  private void init(Context context,AttributeSet attrs){    TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.TakePhotoButton);    outCircleColor = array.getColor(R.styleable.TakePhotoButton_outCircleColor,Color.parseColor("#E0E0E0"));    innerCircleColor = array.getColor(R.styleable.TakePhotoButton_innerCircleColor,Color.WHITE);    progressColor = array.getColor(R.styleable.TakePhotoButton_readColor,Color.GREEN);    mLoadingTime = array.getInteger(R.styleable.TakePhotoButton_maxSeconds,10);    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);  }  private void resetParams() {    width = getWidth();    height = getHeight();    circleWidth = width*0.13f;    outRaduis = (float) (Math.min(width, height)/2.4);    innerRaduis = outRaduis -circleWidth;  }  @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    super.onMeasure(widthMeasureSpec, heightMeasureSpec);    int width = MeasureSpec.getSize(widthMeasureSpec);    int height = MeasureSpec.getSize(heightMeasureSpec);    if (width > height) {      setMeasuredDimension(height, height);    } else {      setMeasuredDimension(width, width);    }  }  @Override  protected void onDraw(Canvas canvas) {    resetParams();    //画外圆    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);    //画内圆    innerRoundPaint.setAntiAlias(true);    innerRoundPaint.setColor(innerCircleColor);    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);    }  }  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();        }      }    });  }  @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;  }  private OnProgressTouchListener listener;  public void setOnProgressTouchListener(OnProgressTouchListener listener) {    this.listener = listener;  }  /**   * 进度触摸监听   */  public interface OnProgressTouchListener {    /**     * 单击     * @param photoButton     */    void onClick(TakePhotoButton photoButton);    /**     * 长按     * @param photoButton     */    void onLongClick(TakePhotoButton photoButton);    /**     * 长按抬起     * @param photoButton     */    void onLongClickUp(TakePhotoButton photoButton);    void onFinish();  }}

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

总结

以上所述是小编给大家介绍的Android 自定义view仿微信相机单击拍照长按录视频按钮,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

您可能感兴趣的文章:

  • Android基于腾讯云实时音视频仿微信视频通话最小化悬浮
  • Android仿微信多人音视频通话界面
  • Android仿微信录制小视频
  • Android仿微信拍摄短视频
  • Android实现微信朋友圈发本地视频功能
  • Android 微信小视频录制功能实现详细介绍
  • Android采用消息推送实现类似微信视频接听


  • 上一条:
    微信小程序 扭蛋抽奖机css3动画实现详解
    下一条:
    微信小程序点击图片实现长按预览、保存、识别带参数二维码、转发等功能
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 微信模板消息改版后发送规则记录(微信订阅消息参数值内容限制说明)(1个评论)
    • 微信支付v3对接所需工具及命令(0个评论)
    • 2023年9月1日起:微信小程序必须备案才能上线运营(0个评论)
    • 腾讯官方客服回应了:微信好友上限约10000个!(1个评论)
    • 2023年做微信小程序的老铁注意:新增收费项、微信小程序获取手机号也收费了(2个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客