Android六边形图像
Glide.with(mContext).asBitmap().load(ranking.get(2).getGame_image().replace(":\\/", "/").toString()).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
DefineView defineView=new DefineView();
Bitmap nBM = scaleBitmap(resource, 1.5f);
defineView.setBitmap(nBM);
iv_racking.setBackground(defineView);
}
});
public class DefineView extends Drawable {
Rect mRect = new Rect();
Paint mPaint;
Path mPath;
BitmapShader mShader;
Bitmap mBitmap;
public DefineView() {
this(null);
}
public DefineView(Bitmap bitmap) {
init();
/* int w=bitmap.getWidth();
int h=bitmap.getHeight();
float sx=(float)w;//要强制转换,不转换我的在这总是死掉。
float sy=(float)h;
Matrix matrix = new Matrix();
matrix.postScale(sx, sy); // 长和宽放大缩小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, w,
h, matrix, true);
*/
setBitmap(bitmap);
}
public void setsView(Bitmap bitmap){
init();
setBitmap(bitmap);
}
private void init() {
initPaint();
initPath();
}
private void ensurePaint() {
if (mPaint == null) {
mPaint = new Paint();
}
}
private void ensurePath() {
if (mPath == null) {
mPath = new Path();
}
}
private void initPaint() {
ensurePaint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeWidth(3f);
}
public Bitmap getBitmap() {
return mBitmap;
}
// 设置Bitmap的时候初始化shader,并设置给paint
public void setBitmap(Bitmap bitmap) {
this.mBitmap = bitmap;
if (bitmap == null) {
mShader = null;
} else {
mShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(mShader);
}
}
private int mWidth;
private int mHeight;
private int mLength;
// 初始化好Path要走的路径
private void initPath() {
ensurePath();
float l = (float) (mRect.width() / 2);
float h = (float) (Math.sqrt(3) * l);
float top = (mRect.height() - h) / 2;
mPath.reset();
mPath.moveTo(l / 2, top);
mPath.lineTo(0, h / 2 + top);
mPath.lineTo(l / 2, h + top);
mPath.lineTo((float) (l * 1.5), h + top);
mPath.lineTo(2 * l, h / 2 + top);
mPath.lineTo((float) (l * 1.5), top);
mPath.lineTo(l / 2, top);
mPath.close();
}
@Override
public void draw(Canvas canvas) {
canvas.drawPath(mPath, mPaint);
}
@Override
public void setAlpha(int alpha) {
if (mPaint != null) {
mPaint.setAlpha(alpha);
}
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
if (mPaint != null) {
mPaint.setColorFilter(colorFilter);
}
}
@SuppressLint("WrongConstant")
@Override
public int getOpacity() {
return 0;
}
@Override
public void setBounds(int left, int top, int right, int bottom) {
super.setBounds(left, top, right, bottom);
mRect.set(left, top, right, bottom);
initPath();
}
@Override
public int getIntrinsicWidth() {
if (mBitmap != null) {
return mBitmap.getWidth();
} else {
return super.getIntrinsicWidth();
}
}
@Override
public int getIntrinsicHeight() {
if (mBitmap != null) {
return mBitmap.getHeight();
}
return super.getIntrinsicHeight();
}
}