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仿微信相机单击拍照长按录视频按钮,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]

