125 lines
4.2 KiB
Java
125 lines
4.2 KiB
Java
|
|
package com.xscm.moduleutil.widget;
|
||
|
|
|
||
|
|
import android.content.Context;
|
||
|
|
import android.content.res.ColorStateList;
|
||
|
|
import android.content.res.TypedArray;
|
||
|
|
import android.graphics.Canvas;
|
||
|
|
import android.graphics.Color;
|
||
|
|
import android.graphics.Outline;
|
||
|
|
import android.graphics.Paint;
|
||
|
|
import android.graphics.Rect;
|
||
|
|
import android.graphics.RectF;
|
||
|
|
import android.util.AttributeSet;
|
||
|
|
import android.view.View;
|
||
|
|
import android.view.ViewOutlineProvider;
|
||
|
|
|
||
|
|
import androidx.annotation.ColorInt;
|
||
|
|
import androidx.annotation.DimenRes;
|
||
|
|
import androidx.annotation.NonNull;
|
||
|
|
import androidx.annotation.Nullable;
|
||
|
|
import androidx.appcompat.widget.AppCompatImageView;
|
||
|
|
|
||
|
|
import com.xscm.moduleutil.R;
|
||
|
|
|
||
|
|
|
||
|
|
public class GifAvatarOvalView extends AppCompatImageView {
|
||
|
|
public GifAvatarOvalView(@NonNull Context context) {
|
||
|
|
super(context);
|
||
|
|
init(null);
|
||
|
|
}
|
||
|
|
|
||
|
|
public GifAvatarOvalView(@NonNull Context context, @Nullable AttributeSet attrs) {
|
||
|
|
super(context, attrs);
|
||
|
|
init(attrs);
|
||
|
|
}
|
||
|
|
|
||
|
|
public GifAvatarOvalView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
||
|
|
super(context, attrs, defStyleAttr);
|
||
|
|
init(attrs);
|
||
|
|
}
|
||
|
|
|
||
|
|
protected int mWidth;
|
||
|
|
protected int mHeight;
|
||
|
|
protected float mRadius;
|
||
|
|
|
||
|
|
public static final float DEFAULT_BORDER_WIDTH = 0f;
|
||
|
|
public static final int DEFAULT_BORDER_COLOR = Color.WHITE;
|
||
|
|
protected float mBorderWidth = DEFAULT_BORDER_WIDTH;
|
||
|
|
protected ColorStateList mBorderColor = ColorStateList.valueOf(DEFAULT_BORDER_COLOR);
|
||
|
|
protected RectF mBorderRect = new RectF();
|
||
|
|
protected Paint mBorderPaint;
|
||
|
|
|
||
|
|
private void init(@Nullable AttributeSet attrs) {
|
||
|
|
if (attrs != null) {
|
||
|
|
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.GifAvatarOvalView);
|
||
|
|
mBorderWidth = a.getDimensionPixelSize(R.styleable.GifAvatarOvalView_gav_border_width, -1);
|
||
|
|
if (mBorderWidth < 0) {
|
||
|
|
mBorderWidth = DEFAULT_BORDER_WIDTH;
|
||
|
|
}
|
||
|
|
mBorderColor = a.getColorStateList(R.styleable.GifAvatarOvalView_gav_border_color);
|
||
|
|
if (mBorderColor == null) {
|
||
|
|
mBorderColor = ColorStateList.valueOf(DEFAULT_BORDER_COLOR);
|
||
|
|
}
|
||
|
|
a.recycle();
|
||
|
|
}
|
||
|
|
mBorderPaint = new Paint();
|
||
|
|
mBorderPaint.setStyle(Paint.Style.STROKE);
|
||
|
|
mBorderPaint.setAntiAlias(true);
|
||
|
|
mBorderPaint.setColor(mBorderColor.getDefaultColor());
|
||
|
|
mBorderPaint.setStrokeWidth(mBorderWidth);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||
|
|
mWidth = mHeight = Math.min(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
|
||
|
|
mRadius = mWidth / 2.0f;
|
||
|
|
mBorderRect.set(0, 0, mWidth, mHeight);
|
||
|
|
setMeasuredDimension(mWidth, mHeight);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
||
|
|
super.onLayout(changed, left, top, right, bottom);
|
||
|
|
setClipToOutline(true);
|
||
|
|
setOutlineProvider(new ViewOutlineProvider() {
|
||
|
|
@Override
|
||
|
|
public void getOutline(View view, Outline outline) {
|
||
|
|
Rect selfRect = new Rect(0, 0, mWidth, mHeight);
|
||
|
|
outline.setRoundRect(selfRect, mRadius);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
protected void onDraw(Canvas canvas) {
|
||
|
|
super.onDraw(canvas);
|
||
|
|
if (mBorderWidth > 0) {
|
||
|
|
canvas.drawOval(mBorderRect, mBorderPaint);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void setBorderWidth(@DimenRes int resId) {
|
||
|
|
setBorderWidth(getResources().getDimension(resId));
|
||
|
|
}
|
||
|
|
|
||
|
|
public void setBorderWidth(float width) {
|
||
|
|
if (mBorderWidth == width) { return; }
|
||
|
|
mBorderWidth = width;
|
||
|
|
mBorderPaint.setStrokeWidth(mBorderWidth);
|
||
|
|
invalidate();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void setBorderColor(@ColorInt int color) {
|
||
|
|
setBorderColor(ColorStateList.valueOf(color));
|
||
|
|
}
|
||
|
|
|
||
|
|
public void setBorderColor(ColorStateList colors) {
|
||
|
|
if (mBorderColor.equals(colors)) { return; }
|
||
|
|
mBorderColor = (colors != null) ? colors : ColorStateList.valueOf(DEFAULT_BORDER_COLOR);
|
||
|
|
mBorderPaint.setColor(mBorderColor.getDefaultColor());
|
||
|
|
if (mBorderWidth > 0) {
|
||
|
|
invalidate();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|