所以,我完成了here的回答,所以它可以解决我的问题。
XML:
<RoundedThumb
android:layout_width="match_parent"
android:layout_height="match_parent"/>
JAVA:
public class RoundedThumb extends ImageView {
private final float radius = getContext().getResources().getDimension(R.dimen.corner_radius);
private RectF mSrcRect = new RectF();
private RectF mDstRect = new RectF();
private Path mClipPath = new Path();
public RoundedThumb(Context context) {
super(context);
}
public RoundedThumb(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RoundedThumb(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
protected void onDraw(Canvas canvas) {
if (getDrawable() != null && getImageMatrix() != null) {
mSrcRect.set(0, 0, getDrawable().getIntrinsicWidth(), getDrawable().getIntrinsicHeight());
getImageMatrix().mapRect(mDstRect, mSrcRect);
mClipPath.reset();
mClipPath.addRoundRect(mDstRect, radius, radius, Path.Direction.CW);
canvas.clipPath(mClipPath);
}
super.onDraw(canvas);
}
}
与用法:
thumb.setScaleType(ImageView.ScaleType.FIT_CENTER);
Bitmap thumbnail = BitmapFactory.decodeFile(path);
thumb.setImageBitmap(thumbnail);
所以,现在矩形的路径转化就像BitmapDrawable内的ImageView,始终准确地周围包围ImageView中的任何位图。对我来说重要的是 - ImageView仍然具有aspectRatio 16/9,并在资源中定义它的位置。但是位图有四舍五入的边框,但没有修改。
UPD1:我有点困惑:不幸的是在某些设备上clipPath方法没有效果(SII)甚至崩溃(旧的华硕变压器)。可以通过将hardwareAccelerated
设置为false
来完全修复。但是,该死的,这是不好的=/
与hardwareAccelerated真相同:( – Marabita
最后我从资源中删除hardwareAccelerated,只是放在try/catch onDraw。 – mjollneer