直播平台搭建,自定义气泡效果(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),更多内容欢迎关注之后的文章
标签:自定义,int,private,BubbleView,import,android,气泡 From: https://www.cnblogs.com/yunbaomengnan/p/17861837.html