非直接方式实现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();
}