Android实现人脸识别动画效果(1)

本文介绍了如何在Android应用中使用CanvasAPI创建带有旋转效果的内外圆圈图片,并添加提示文字。通过ValueAnimator实现顺时针和逆时针旋转,适用于面试中常见的Android开发问题。
摘要由CSDN通过智能技术生成

* 画圆圈外部的圆圈图片

*/

private void drawBitmapCircle(Canvas canvas) {

if(mInnerCircleBitmap == null){

int dstWidthAndHeight = (int) (getWidth() / 1.5f + getWidth() / 1.5f / 4);

mInnerCircleBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_checkface_innercircle);

mInnerCircleBitmap = Bitmap.createScaledBitmap(mInnerCircleBitmap,dstWidthAndHeight,dstWidthAndHeight,true);

}

if(mOutCircleBitmap == null){

int dstWidthAndHeight = (int) (getWidth() / 1.5f + getWidth() / 1.5f / 4);

mOutCircleBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_checkface_outcircle);

mOutCircleBitmap = Bitmap.createScaledBitmap(mOutCircleBitmap,dstWidthAndHeight,dstWidthAndHeight,true);

}

int left = (getWidth() - mInnerCircleBitmap.getWidth()) / 2;

int top = (int) (getWidth() / 2 - getWidth() / 3 - getWidth() / 1.5f / 8);

canvas.drawBitmap(mInnerCircleBitmap,left,top,mPaint);

canvas.drawBitmap(mOutCircleBitmap,left,top,mPaint);

}

这时效果如下

3.实现旋转动画效果

接下来我们就可以通过ValueAnimator来实现圆圈的旋转效果了,从文章开头的效果我们可以看出两个圆圈的旋转方向是不一样的,因此我们逻辑上也要注意一个是顺时针旋转另一个是逆时针旋转,代码如下

private float mDegress = 0;//旋转角度

private void init() {

//定义动画

valueAnimator = ValueAnimator.ofFloat(0f, 360f);

valueAnimator.setRepeatCount(ValueAnimator.INFINITE);

valueAnimator.setRepeatMode(ValueAnimator.RESTART);

valueAnimator.setInterpolator(new LinearInterpolator());

valueAnimator.setDuration(6000);

valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

mDegress = (float) animation.getAnimatedValue();

postInvalidate();

}

});

valueAnimator.start();

}

/**

* 画圆圈外部的圆圈图片

*/

private void drawBitmapCircle(Canvas canvas) {

if(mInnerCircleBitmap == null){

int dstWidthAndHeight = (int) (getWidth() / 1.5f + getWidth() / 1.5f / 4);

mInnerCircleBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_checkface_innercircle);

mInnerCircleBitmap = Bitmap.createScaledBitmap(mInnerCircleBitmap,dstWidthAndHeight,dstWidthAndHeight,true);

}

if(mOutCircleBitmap == null){

int dstWidthAndHeight = (int) (getWidth() / 1.5f + getWidth() / 1.5f / 4);

mOutCircleBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_checkface_outcircle);

mOutCircleBitmap = Bitmap.createScaledBitmap(mOutCircleBitmap,dstWidthAndHeight,dstWidthAndHeight,true);

}

int left = (getWidth() - mInnerCircleBitmap.getWidth()) / 2;

int top = (int) (getWidth() / 2 - getWidth() / 3 - getWidth() / 1.5f / 8);

//顺时针旋转

canvas.save();

canvas.rotate(mDegress,getWidth() / 2, getWidth() / 2);

canvas.drawBitmap(mInnerCircleBitmap,left,top,mPaint);

canvas.restore();

//逆时针旋转

canvas.save();

canvas.rotate(-mDegress,getWidth() / 2, getWidth() / 2);

canvas.drawBitmap(mOutCircleBitmap,left,top,mPaint);

canvas.restore();

}

这时效果如下

4.增加提示文字

绘制文字就比较简单了,主要是用于提示用户需要进行的操作,代码如下

canvas.drawText(“请把脸移入圈内”,getWidth() / 2, (float) (getWidth() * 1.2),mTextPaint);

最终效果如下

案例源码

最后

针对于上面的问题,我总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料。
(包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。

image

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 29
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值