直播平臺搭建,自定義氣泡效果(BubbleView)

zhibo系統開發發表於2023-11-28

直播平臺搭建,自定義氣泡效果(BubbleView)

package com.example.myapplication;
import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class BubbleView extends View {
    private int mBubbleMaxRadius = 15;          // 氣泡最大半徑 px
    private int mBubbleMinRadius = 8;           // 氣泡最小半徑 px
    private int mBubbleMaxSize = 50;            // 氣泡數量
    private int mBubbleRefreshTime = 50;        // 重新整理間隔
    private int mBubbleMaxSpeedY = 2;           // 氣泡速度
    private int mBubbleMaxSpeedX = 4;           // 氣泡速度
    private int mBubbleAlpha = 128;             // 氣泡畫筆
    private float mContentWidth;                 // 瓶子寬度
    private float mContentHeight;                // 瓶子高度
    private RectF mContentRectF;                // 實際可用內容區域
    private Paint mBubblePaint;                 // 氣泡畫筆
    public BubbleView(Context context) {
        this(context, null);
    }
    public BubbleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public BubbleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContentWidth = dp2px(130);
        mContentHeight = dp2px(260);
        mBubblePaint = new Paint();
        mBubblePaint.setColor(Color.GREEN);
        mBubblePaint.setAlpha(mBubbleAlpha);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int width;
        int height;
        if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {
            width = widthSize;
            mContentWidth = width;
        } else {
            width = (int) mContentWidth;
        }
        if (heightSpecMode == MeasureSpec.EXACTLY || heightSpecMode == MeasureSpec.AT_MOST) {
            height = heightSize;
            mContentHeight = height;
        } else {
            height = (int) mContentHeight;
        }
        setMeasuredDimension(width, height);
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mContentRectF = new RectF(getPaddingLeft(), getPaddingTop(), w - getPaddingRight(), h - getPaddingBottom());
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBubble(canvas);
    }
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startBubbleSync();
    }
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopBubbleSync();
    }


目前為自定義屬性,佈局檔案中直接引用即可。

  <com.example.myapplication.BubbleView
        android:layout_width="300dp"
        android:layout_height="100dp"/>


以上就是 直播平臺搭建,自定義氣泡效果(BubbleView),更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2997689/,如需轉載,請註明出處,否則將追究法律責任。

相關文章