首页 > 其他分享 >直播平台源代码,Android自定义View实现呼吸灯效果

直播平台源代码,Android自定义View实现呼吸灯效果

时间:2023-03-07 14:34:53浏览次数:45  
标签:MeasureSpec mCirclePaint 自定义 mMaxCircleRadius circleAlphaValueAnimator BreathVie

直播平台源代码,Android自定义View实现呼吸灯效果

自定义View

自定义 BreathView 的Kotlin代码如下:

 


import android.animation.ValueAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.view.animation.Animation
import android.view.animation.LinearInterpolator
class BreathView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr), AnimatorUpdateListener {
    private val mCenterCircleRadius: Float
    private var mMaxCircleRadius: Float
    private val mCirclePaint: Paint
    private var mAlphaValue = 0
    init {
        val a = context.obtainStyledAttributes(attrs, R.styleable.BreathView)
        mCenterCircleRadius = a.getDimension(R.styleable.BreathView_centerCircleRadius, 5f)
        mMaxCircleRadius = a.getDimension(R.styleable.BreathView_maxCircleRadius, 10f)
        if (mCenterCircleRadius >= mMaxCircleRadius) {
            mMaxCircleRadius = mCenterCircleRadius * 2
        }
        val circleColor = a.getColor(R.styleable.BreathView_circleColor, Color.GREEN)
        a.recycle()
        mCirclePaint = Paint()
        mCirclePaint.isAntiAlias = true
        mCirclePaint.style = Paint.Style.FILL
        mCirclePaint.color = circleColor
        val circleAlphaValueAnimator = ValueAnimator.ofInt(0, 255)
        circleAlphaValueAnimator.duration = BREATH_TIME
        circleAlphaValueAnimator.repeatCount = Animation.INFINITE
        circleAlphaValueAnimator.repeatMode = ValueAnimator.REVERSE
        circleAlphaValueAnimator.interpolator = LinearInterpolator()
        circleAlphaValueAnimator.addUpdateListener(this)
        circleAlphaValueAnimator.start()
    }
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var width = MeasureSpec.getSize(widthMeasureSpec)
        var height = MeasureSpec.getSize(heightMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(widthMeasureSpec)
        if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
            width = Math.max(width.toFloat(), mMaxCircleRadius * 2).toInt()
        }
        if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
            height = Math.max(width.toFloat(), mMaxCircleRadius * 2).toInt()
        }
        setMeasuredDimension(width, height)
    }
    override fun draw(canvas: Canvas) {
        super.draw(canvas)
        val centerX = width / 2.0f
        val centerY = height / 2.0f
        mCirclePaint.alpha = 255
        canvas.drawCircle(centerX, centerY, mCenterCircleRadius, mCirclePaint)
        mCirclePaint.alpha = mAlphaValue
        canvas.drawCircle(centerX, centerY, mMaxCircleRadius, mCirclePaint)
    }
    override fun onAnimationUpdate(valueAnimator: ValueAnimator) {
        mAlphaValue = valueAnimator.animatedValue as Int
        invalidate()
    }
    companion object {
        private const val BREATH_TIME: Long = 1000 //动画执行时间/呼吸速率
    }

自定义 BreathView 的Java代码如下:

 


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.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
public class BreathView extends View implements ValueAnimator.AnimatorUpdateListener {
private static final long BREATH_TIME = 1000; //动画执行时间/呼吸速率
private final float mCenterCircleRadius;
private float mMaxCircleRadius;
private final Paint mCirclePaint;
private int mAlphaValue;
public BreathView(Context context) {
this(context, null);
}
public BreathView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BreathView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BreathView);
mCenterCircleRadius = a.getDimension(R.styleable.BreathView_centerCircleRadius, 5f);
mMaxCircleRadius = a.getDimension(R.styleable.BreathView_maxCircleRadius, 10f);
if (mCenterCircleRadius >= mMaxCircleRadius) {
mMaxCircleRadius = mCenterCircleRadius * 2;
}
int circleColor = a.getColor(R.styleable.BreathView_circleColor, Color.GREEN);
a.recycle();
mCirclePaint = new Paint();
mCirclePaint.setAntiAlias(true);
mCirclePaint.setStyle(Paint.Style.FILL);
mCirclePaint.setColor(circleColor);
ValueAnimator circleAlphaValueAnimator = ValueAnimator.ofInt(0, 255);
circleAlphaValueAnimator.setDuration(BREATH_TIME);
circleAlphaValueAnimator.setRepeatCount(Animation.INFINITE);
circleAlphaValueAnimator.setRepeatMode(ValueAnimator.REVERSE);
circleAlphaValueAnimator.setInterpolator(new LinearInterpolator());
circleAlphaValueAnimator.addUpdateListener(this);
circleAlphaValueAnimator.start();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(widthMeasureSpec);
if(widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
width = (int) Math.max(width, mMaxCircleRadius * 2);
}
if(heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
            height = (int) Math.max(width, mMaxCircleRadius * 2);
        }
setMeasuredDimension(width, height);
}
@Override
public void draw(Canvas canvas) {
        super.draw(canvas);
float centerX = getWidth() / 2.0f;
float centerY = getHeight() / 2.0f;
mCirclePaint.setAlpha(255);
canvas.drawCircle(centerX, centerY, mCenterCircleRadius, mCirclePaint);
mCirclePaint.setAlpha(mAlphaValue);
canvas.drawCircle(centerX, centerY, mMaxCircleRadius, mCirclePaint);
}
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
mAlphaValue = (int) valueAnimator.getAnimatedValue();
invalidate();
}
}

 

 以上就是直播平台源代码,Android自定义View实现呼吸灯效果, 更多内容欢迎关注之后的文章

 

标签:MeasureSpec,mCirclePaint,自定义,mMaxCircleRadius,circleAlphaValueAnimator,BreathVie
From: https://www.cnblogs.com/yunbaomengnan/p/17188003.html

相关文章

  • EF7数据库提供者的自定义值生成器
    本文将讲解提供者使用值生成器的原理。因代码太多,本文只摘要重要代码,并且删除了代码中的注释。如果您不了解,如何使用EF的值生成器?见这篇文章:《EF7创建模型值生成篇》?SqlS......
  • HANA-BW-改造自定义数据源
    1HANA-BW-改造自定义数据源22020年5月13日312:534ZMM_TRAN_KONV改为hana触发器增量上载5源代码:6OPENCURSORWITHHOLDS_CURSORFOR7......
  • Android异常重启并进入Recovery(恢复)模式的原因之一
    1、init.rc简述init.rc文件由系统第一个启动的init程序解析,此文件由语句组成,主要包含了四种类型的语句:Action,Commands,Services,Options.2、服务(services)服务是指那些需......
  • Android SDK Manager无法显示可供下载的未安装SDK解决方案
    AndroidSDKManager无法显示可供下载的未安装SDK解决规划2012-10-14AndroidSDKManager无法显示可供下载的未安装SDK解决规划据说dl-ssl.google.com在大陆被强了,巨大的......
  • 【android】Android定位功能开发(1)——获取位置
    基于位置的服务包括三个方面:获取位置、地图服务、地理编码服务。获取位置有两种方式,一种是通过GPS获得位置,精度高,耗电高,另一种是通过WLAN或通讯基站获得位置,精度低,耗电低......
  • Android systrace命令行工具
    命令行工具systrace(SystemTrace)跟踪的是系统级的内容,如CPU各核心调度,SurfaceFlinger、VSync(垂直同步)、BufferQueue。通过收集系统事件和App逻辑中插入的自定义事件的组合......
  • 关于Android Studio的Activity的页面跳转完成
    第一种方式Intentintent=newIntent();intent.setClass(this,MainActivity3.class);startActivity(intent);第二种方式Intentintent=newIntent();intent.setClas......
  • 5 odoo 自定义界面
    本文以项目模块自定义工作台为例模块名称:my_projectodoo的自定义界面实现主要的两个工具:Qweb、js正式开始之前在需要自定义内容的模块中定义几个文件:static/js/proje......
  • 解决Android SDK Manager无法更新以及更新缓慢问题
    打开..\Android\sdk\SDKManager.exe按下图进行设置如果更新缓慢,可以更换HTTPProxyServer(详见http://www androiddevtools.cn,目前mirrors.neusoft.edu.cn......
  • 【Android Studio】通过编辑setting.gradle文件,添加阿里仓库
    本人对AndroidStudio的了解非常初级,这篇blog主要是自用备忘性质。因为众所周知的原因,国外仓库访问很不方便,影响项目构建。所以需要添加国内仓库,而阿里云仓库属于比较知名......