非直接方式实现Glide4.x版本图片部分圆角功能

最近UI小姐姐一开心又把项目的UI重绘了一遍。
在这里插入图片描述
本来说开开心心把Python学完的,看来又要耽误一点时间了。

直接上部分UI图。
在这里插入图片描述
可以清晰的看到只有左上角和右上角实现了5dp的圆角。

实现

为什么是间接呢,因为是直接修改ImageView生成。不使用自定义的BitmapTransformation方式(或多或少有点问题,直接放弃了。)

直接贴代码

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

public class TopRoundImageView extends AppCompatImageView {


    private Path mPath;
    private RectF mRectF;

    /*圆角的半径,依次为左上角xy半径,右上角,右下角,左下角*/
    private float[] rids = {dp2px(5), dp2px(5), dp2px(5),dp2px(5), 0.0f, 0.0f, 0.0f, 0.0f,};


    public TopRoundImageView(Context context) {
        this(context,null);
    }

    public TopRoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TopRoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mPath= new Path();
        mRectF= new RectF();
    }

    private   int dp2px(final float dpValue) {
        final float scale = this.getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 画图
     * @param canvas
     */
    protected void onDraw(Canvas canvas) {

        int w = this.getWidth();
        int h = this.getHeight();

        mRectF.set(0, 0, w, h);
        mPath.addRoundRect(mRectF, rids, Path.Direction.CW);
        canvas.clipPath(mPath);
        super.onDraw(canvas);
    }

}

直接在布局中引用即可。

题外话

说下自定义View的一个坑,下面是自定义的基本实现方式。

	public TopRoundImageView(Context context) {
        this(context,null);
    }

    public TopRoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TopRoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

在实现第二个方法时,一定要注意继承的View有没有引入特殊的“defStyleAttr”,即父View中第二个构造方法不是传入的0,如AppCompatTextView控件。

	public AppCompatTextView(Context context) {
        this(context, (AttributeSet)null);
    }

    public AppCompatTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 16842884);
    }

    public AppCompatTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(TintContextWrapper.wrap(context), attrs, defStyleAttr);
        this.mBackgroundTintHelper = new AppCompatBackgroundHelper(this);
        this.mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr);
        this.mTextHelper = new AppCompatTextHelper(this);
        this.mTextHelper.loadFromAttributes(attrs, defStyleAttr);
        this.mTextHelper.applyCompoundDrawablesTints();
    }

还有AppCompatEditText控件

import android.support.v7.appcompat.R.attr;

	public AppCompatEditText(Context context) {
        this(context, (AttributeSet)null);
    }

    public AppCompatEditText(Context context, AttributeSet attrs) {
        this(context, attrs, attr.editTextStyle);
    }

    public AppCompatEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(TintContextWrapper.wrap(context), attrs, defStyleAttr);
        this.mBackgroundTintHelper = new AppCompatBackgroundHelper(this);
        this.mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr);
        this.mTextHelper = new AppCompatTextHelper(this);
        this.mTextHelper.loadFromAttributes(attrs, defStyleAttr);
        this.mTextHelper.applyCompoundDrawablesTints();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值