简体   繁体   English

带有圆角的Android ImageView(再次)

[英]Android ImageView with rounded corners (again)

I searched carefully here and in other internets, but have not find solution for me. 我在这里和其他互联网上仔细搜索过,但没有为我找到解决方案。 I have ImageView of fixed size. 我有固定大小的ImageView。 I need to show some bitmaps, loaded in runtime. 我需要显示一些在运行时加载的位图。 All of them has different size and aspect ratio. 它们都有不同的尺寸和纵横比。 For example: while ImageView is 480x270px bitmaps could be 160x90, 1600x500, 50x100 and so on. 例如:虽然ImageView是480x270px,但位图可能是160x90,1600x500,50x100等等。 And i want them be centered in and fit to ImageView. 我希望它们居中并适合ImageView。 And rounded corners. 圆角。

Two most popular approaches is (1) process bitmap and (2) modify draw-stage in imageView subclass. 两种最流行的方法是(1)处理位图和(2)修改imageView子类中的绘制阶段。

Romain Guy extends Drawable and use drawRoundRect method in Canvas. Romain Guy扩展了Drawable并在Canvas中使用了drawRoundRect方法。 Unfortunately his solution does not applied FIT_CENTER, though rounded line is pretty sharp. 不幸的是他的解决方案并没有应用FIT_CENTER,尽管圆线非常清晰。

There is also variant to process bitmap, render it to another bitmap and get rounded. 处理位图还有变体,将其渲染到另一个位图并进行舍入。 Set it as source - get centered and fitted ImageView. 将其设置为源 - 获得居中并安装ImageView。 But in this case rounded rect exists only in bitmap's pixels grid. 但在这种情况下,圆角矩形仅存在于位图的像素网格中。 It could be very blurry if bitmap is small. 如果位图很小,可能会非常模糊。

And last one solution, which suits best for me, but which need to be upgraded too. 最后一个解决方案,最适合我,但也需要升级。 We can tune canvas to contain clipPath along it's border. 我们可以调整画布以包含沿着它的边框的clipPath。 But centered bitmap with 16/5 aspect ratio wont be rounded - it will be drawn outside of cliPath. 但是具有16/5宽高比的居中位图不会被舍入 - 它将被绘制在cliPath之外。

So, i completed answer from here so it can solve my problem. 所以,我从这里完成了答案,所以它可以解决我的问题。

XML: XML:

    <RoundedThumb
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

JAVA: 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);
}
}

And usage: 用法:

thumb.setScaleType(ImageView.ScaleType.FIT_CENTER);
Bitmap thumbnail = BitmapFactory.decodeFile(path);
thumb.setImageBitmap(thumbnail);

So, now rect for Path transformed just like BitmapDrawable inside of ImageView and always circumscribed exactly around any bitmap in ImageView. 因此,现在,对于Path的矩形转换就像ImageView中的BitmapDrawable一样,并且总是在ImageView中的任何位图周围完全限制。 What is important for me - ImageView still has aspectRatio 16/9 and takes it's place, defined in resources. 对我来说重要的是什么 - ImageView仍然有aspectRatio 16/9并且在资源中定义了它的位置。 But bitmap has rounded borders, while it is not modified. 但是位图具有圆形边框,而未进行修改。

UPD1: I am a little bit confused: unfortunately on some devices clipPath method has no effect (SII) or even crashes (old asus transformer). UPD1:我有点困惑:遗憾的是在某些设备上,clipPath方法没有效果(SII)甚至崩溃(旧的asus变换器)。 Can be fixed completely by setting hardwareAccelerated to false . 可以通过将hardwareAccelerated设置为false来完全修复。 But, damn, that's not good =/ 但是,该死的,那不好= /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM