Android自定义气泡布局
功能
- 支持4个方向
- 设置三角形位置
- 设置阴影颜色,大小设置圆角
- 自定义背景颜色
核心代码
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.FrameLayout;
public class BubbleLayout extends FrameLayout {
public static final int LEFT = 1;
public static final int TOP = 2;
public static final int RIGHT = 3;
public static final int BOTTOM = 4;
@IntDef({
LEFT, TOP, RIGHT, BOTTOM})
private @interface Direction {
}
/**
* 圆角大小
*/
private int mRadius;
/**
* 三角形的方向
*/
@Direction
private int mDirection;
/**
* 三角形的底边中心点
*/
private Point mDatumPoint;
/**
* 三角形位置偏移量(默认居中)
*/
private int mOffset;
private Paint mBorderPaint;
private Path mPath;
private RectF mRect;
public BubbleLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BubbleLayout);
//背景颜色
int backGroundColor = ta.getColor(R.styleable.BubbleLayout_background_color, Color.WHITE);
//阴影颜色
int shadowColor = ta.getColor(R.styleable.Bubbl