Android六边形图像

本文介绍了一种在Android中将圆形图片转化为六边形的方法。通过使用自定义Drawable类和Canvas绘图API,实现了一个可以将任意图片缩放并裁剪为六边形形状的功能。该方法适用于游戏开发中的排行榜等场景。
摘要由CSDN通过智能技术生成

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值