最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 自定義view仿微信相機(jī)單擊拍照長(zhǎng)按錄視頻按鈕

 更新時(shí)間:2019年07月19日 16:34:55   作者:Java魑魅魍魎  
這篇文章主要介紹了Android 自定義view仿微信相機(jī)單擊拍照長(zhǎng)按錄視頻按鈕,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下

Android仿微信相機(jī)的拍照按鈕單擊拍照,長(zhǎng)按錄視頻。先上效果圖。

這里寫(xiě)圖片描述
這里寫(xiě)圖片描述

項(xiàng)目地址:https://github.com/c786909486/PhotoButton2/tree/v1.0

添加依賴(lài)

allprojects {
    repositories {
      ...
      maven { url 'https://jitpack.io' }
    }
  }

dependencies {
      compile compile 'com.github.c786909486:PhotoButton2:v1.1'
  }

長(zhǎng)按效果分析

判斷是否為長(zhǎng)按,如果是,則擴(kuò)大外圓,縮小內(nèi)圓。由于要擴(kuò)大外圓,所以在繪制常態(tài)的外圓時(shí)不可將圓的直徑設(shè)置為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);
      //畫(huà)外原環(huán)
      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);
    }

然后通過(guò)手勢(shì)識(shí)別判斷單擊、長(zhǎng)按、長(zhǎng)按抬起。

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) {
        //長(zhǎng)按
        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;
  }

自定義接口對(duì)各個(gè)狀態(tài)進(jìn)行監(jiān)聽(tīng)

public interface OnProgressTouchListener {
    /**
     * 單擊
     * @param photoButton
     */
    void onClick(TakePhotoButton photoButton);
    /**
     * 長(zhǎng)按
     * @param photoButton
     */
    void onLongClick(TakePhotoButton photoButton);
    /**
     * 長(zhǎng)按抬起
     * @param photoButton
     */
    void onLongClickUp(TakePhotoButton photoButton);
    void onFinish();
  }

最后,給外圓弧添加動(dòng)畫(huà)

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();
      }
    });
    //這里是時(shí)間獲取和賦值
    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中給控件設(shè)置監(jiān)聽(tīng)即可。

buttontake.setOnProgressTouchListener(new TakePhotoButton.OnProgressTouchListener() {
      @Override
      public void onClick(TakePhotoButton photoButton) {
        Toast.makeText(MainActivity.this,"單機(jī)",Toast.LENGTH_SHORT).show();
      }
      @Override
      public void onLongClick(TakePhotoButton photoButton) {
        Toast.makeText(MainActivity.this,"長(zhǎng)按",Toast.LENGTH_SHORT).show();
        buttontake.start();
      }
      @Override
      public void onLongClickUp(TakePhotoButton photoButton) {
        onFinish();
      }
      @Override
      public void onFinish() {
        Toast.makeText(MainActivity.this,"錄制結(jié)束",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;//外圓環(huán)寬度
  private int outCircleColor;//外圓顏色
  private int innerCircleColor;//內(nèi)圓顏色
  private int progressColor;//進(jìn)度條顏色
  private Paint outRoundPaint = new Paint(); //外圓畫(huà)筆
  private Paint mCPaint = new Paint();//進(jìn)度畫(huà)筆
  private Paint innerRoundPaint = new Paint();
  private float width; //自定義view的寬度
  private float height; //自定義view的高度
  private float outRaduis; //外圓半徑
  private float innerRaduis;//內(nèi)圓半徑
  private GestureDetectorCompat mDetector;//手勢(shì)識(shí)別
  private boolean isLongClick;//是否長(zhǎng)按
  private float startAngle = -90;//開(kāi)始角度
  private float mmSweepAngleStart = 0f;//起點(diǎn)
  private float mmSweepAngleEnd = 360f;//終點(diǎn)
  private float mSweepAngle;//掃過(guò)的角度
  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) {
        //長(zhǎng)按
        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();
    //畫(huà)外圓
    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);
    //畫(huà)內(nèi)圓
    innerRoundPaint.setAntiAlias(true);
    innerRoundPaint.setColor(innerCircleColor);
    if (isLongClick){
      canvas.drawCircle(width/2,height/2, innerRaduis /2.0f, innerRoundPaint);
      //畫(huà)外原環(huán)
      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();
      }
    });
    //這里是時(shí)間獲取和賦值
    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;
  }
  /**
   * 進(jìn)度觸摸監(jiān)聽(tīng)
   */
  public interface OnProgressTouchListener {
    /**
     * 單擊
     * @param photoButton
     */
    void onClick(TakePhotoButton photoButton);
    /**
     * 長(zhǎng)按
     * @param photoButton
     */
    void onLongClick(TakePhotoButton photoButton);
    /**
     * 長(zhǎng)按抬起
     * @param photoButton
     */
    void onLongClickUp(TakePhotoButton photoButton);
    void onFinish();
  }
}

項(xiàng)目地址:https://github.com/c786909486/PhotoButton2/tree/v1.0

總結(jié)

以上所述是小編給大家介紹的Android 自定義view仿微信相機(jī)單擊拍照長(zhǎng)按錄視頻按鈕,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

  • 了解一點(diǎn)js的Eval函數(shù)

    了解一點(diǎn)js的Eval函數(shù)

    之前只知道eval可以解析字符串,剛剛網(wǎng)上看了又了解了一點(diǎn),這里貼出來(lái),不懂的也看看哈
    2012-07-07
  • JavaScript設(shè)計(jì)模式之命令模式

    JavaScript設(shè)計(jì)模式之命令模式

    這篇文章主要介紹了JavaScript設(shè)計(jì)模式之命令模式,命令設(shè)計(jì)模式是由發(fā)令者、執(zhí)行者、命令對(duì)象三部分構(gòu)成,文章由此展開(kāi)詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-06-06
  • JavaScript職責(zé)鏈模式概述

    JavaScript職責(zé)鏈模式概述

    這篇文章主要為大家詳細(xì)介紹了JavaScript職責(zé)鏈模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • js/jq仿window文件夾框選操作插件

    js/jq仿window文件夾框選操作插件

    這篇文章主要介紹了js/jq仿window文件夾框選操作插件,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-03-03
  • js使用ajax傳值給后臺(tái),后臺(tái)返回字符串處理方法

    js使用ajax傳值給后臺(tái),后臺(tái)返回字符串處理方法

    今天小編就為大家分享一篇js使用ajax傳值給后臺(tái),后臺(tái)返回字符串處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • JS中如何比較兩個(gè)Json對(duì)象是否相等實(shí)例代碼

    JS中如何比較兩個(gè)Json對(duì)象是否相等實(shí)例代碼

    這篇文章主要介紹了JS中如何比較兩個(gè)Json對(duì)象是否相等實(shí)例代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • 淺談javascript的call()、apply()、bind()的用法

    淺談javascript的call()、apply()、bind()的用法

    這篇文章主要為大家詳細(xì)介紹了javascript的call()、apply()、bind()的用法,探討JavaScript中函數(shù)的一些特殊用法,感興趣的小伙伴們可以參考一下
    2016-02-02
  • JavaScript實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼及校驗(yàn)

    JavaScript實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼及校驗(yàn)

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼及校驗(yàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • JS實(shí)現(xiàn)字符串翻轉(zhuǎn)的方法分析

    JS實(shí)現(xiàn)字符串翻轉(zhuǎn)的方法分析

    這篇文章主要介紹了JS實(shí)現(xiàn)字符串翻轉(zhuǎn)的方法,結(jié)合實(shí)例形式分析了javascript字符串使用reverse方法、字符串遍歷方法以及針對(duì)輸入字符串的遍歷、逆序輸出等方法實(shí)現(xiàn)字符串反轉(zhuǎn)相關(guān)操作技巧,需要的朋友可以參考下
    2018-08-08
  • javascript實(shí)現(xiàn)獲取cookie過(guò)期時(shí)間的變通方法

    javascript實(shí)現(xiàn)獲取cookie過(guò)期時(shí)間的變通方法

    這篇文章主要介紹了javascript實(shí)現(xiàn)獲取cookie過(guò)期時(shí)間的變通方法,因?yàn)閏ookie過(guò)期時(shí)間是由瀏覽器控制的,所以想獲取過(guò)期時(shí)間只能通過(guò)本文的變通方法來(lái)實(shí)現(xiàn),需要的朋友可以參考下
    2014-08-08

最新評(píng)論

虹口区| 常山县| 南部县| 博湖县| 崇仁县| 盐山县| 民乐县| 霸州市| 永丰县| 腾冲县| 江山市| 徐汇区| 高要市| 尚志市| 历史| 松阳县| 江华| 夹江县| 福州市| 天镇县| 沐川县| 清河县| 石首市| 彩票| 白水县| 云浮市| 新蔡县| 临夏县| 新津县| 缙云县| 北安市| 漳平市| 镇远县| 于都县| 新化县| 腾冲县| 文山县| 湖南省| 普兰县| 四川省| 四川省|