直播平臺原始碼,Android自定義View實現呼吸燈效果
直播平臺原始碼,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實現呼吸燈效果, 更多內容歡迎關注之後的文章
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2938456/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 直播系統程式碼,Android自定義View實現呼吸燈效果AndroidView
- 直播平臺搭建,自定義View實現loading動畫載入View動畫
- Android 自定義View 點贊效果AndroidView
- 基於CPLD/FPGA的呼吸燈效果實現(附全部verilog原始碼)FPGA原始碼
- Android技術分享|【自定義View】實現Material Design的Loading效果AndroidViewMaterial Design
- Android自定義View實現流式佈局(熱門標籤效果)AndroidView
- 直播平臺搭建,自定義氣泡效果(BubbleView)View
- 直播平臺原始碼,Android中常用Dialog彈窗效果原始碼Android
- 直播平臺原始碼,Flutter 自定義 虛線 分割線原始碼Flutter
- Android自定義View 雷達掃描效果AndroidView
- 直播平臺搭建原始碼,bootstrap實現圖片輪播效果原始碼boot
- 影片直播原始碼,Android TextView設定跑馬燈效果原始碼AndroidTextView
- 直播平臺搭建原始碼,qt自定義滑動按鈕原始碼QT
- Android自定義View——從零開始實現書籍翻頁效果(二)AndroidView
- 直播平臺原始碼,Android實現密碼顯示與隱藏原始碼Android密碼
- 線上直播原始碼,自定義氣泡效果(BubbleView)原始碼View
- Android自定義View:View(二)AndroidView
- Android自定義View教你一步一步實現即刻點贊效果AndroidView
- 直播平臺原始碼,自定義下拉重新整理控制元件原始碼控制元件
- 直播平臺原始碼,el-button自定義圖片顯示原始碼
- 直播平臺原始碼,自定義設定 View 四個角的圓角 以及邊框的設定原始碼View
- Android自定義view之實現帶checkbox的SnackbarAndroidView
- Android自定義View:快遞時間軸實現AndroidView
- Android 自定義 View 實現橫行時間軸AndroidView
- Android自定義View---驗證碼AndroidView
- Android 自定義 View 實戰之 PuzzleViewAndroidView
- Android自定義View整合AndroidView
- CSS3呼吸燈效果CSSS3
- 影片直播app原始碼,自定義View 線型EditText輸入框APP原始碼View
- 短視訊原始碼,在Android 中opengl es實現燈光效果原始碼Android
- 【朝花夕拾】Android自定義View篇之(四)自定義View的三種實現方式及自定義屬性詳解AndroidView
- 直播平臺搭建,實現自定義設定登入頁面
- 直播平臺軟體開發,實現自定義標題欄
- Android自定義View之(一)View繪製流程詳解——向原始碼要答案AndroidView原始碼
- Android自定義view-自繪ViewAndroidView
- 直播平臺原始碼,FlinkSQL實現行轉列原始碼SQL
- Flutter自定義View的實現FlutterView
- 直播原始碼網站,自定義平臺介面,完成各項內容更改原始碼網站