在Android上为imageView渲染圆角

在Android上为imageView渲染圆角,第1张

概述我有以下代码用于渲染带圆角的imageView.publicclassRoundedCornerImageViewextendsImageView{privateintrounded;publicRoundedCornerImageView(Contextcontext){super(context);}publicRoundedCornerImageView(Contextcontext,AttributeSetattrs){

我有以下代码用于渲染带圆角的imageVIEw.

public class RoundedCornerImageVIEw extends ImageVIEw {private int rounded;public RoundedCornerImageVIEw(Context context) {    super(context);}public RoundedCornerImageVIEw(Context context, AttributeSet attrs) {    super(context, attrs);}public RoundedCornerImageVIEw(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);}public int getRounded() {    return rounded;}public voID setRounded(int rounded) {    this.rounded = rounded;}@OverrIDepublic voID onDraw(Canvas canvas){    Drawable drawable = getDrawable();    int w = drawable.getIntrinsicHeight(),        h = drawable.getIntrinsicWIDth();    Bitmap rounder = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);    Canvas tmpCanvas = new Canvas(rounder);    // We're going to apply this paint eventually using a porter-duff xfer mode.    // This will allow us to only overwrite certain pixels. RED is arbitrary. This    // Could be any color that was fully opaque (Alpha = 255)    Paint xferPaint = new Paint(Paint.ANTI_AliAS_FLAG);    xferPaint.setcolor(color.WHITE);    // We're just reusing xferPaint to paint a normal looking rounded Box, the 20.f    // is the amount we're rounding by.    tmpCanvas.drawRoundRect(new RectF(0,0,w,h), 10.0f, 10.0f, xferPaint);    // Now we apply the 'magic sauce' to the paint    xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));    drawable.draw(canvas);    canvas.drawBitmap(rounder, 0, 0, xferPaint);}}<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"          androID:orIEntation="vertical"          androID:layout_wIDth="wrap_content"          androID:layout_height="wrap_content"          androID:background='#a3deef'    ><com.example.scheduling_androID.vIEw.RoundedCornerImageVIEw        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/eventimageVIEw"        androID:adjustVIEwBounds="false"/></linearLayout>

它的工作原理是它确实裁剪掉了图像的角落.但是,当我尝试在具有背景颜色#a3deef的linearLayout中渲染它时会出现问题.生成的显示为#a3deef的背景颜色,每个图像显示为圆角,其中4个裁剪角全部为黑色.

我该怎么做才能使裁剪角透明而不是黑色?此外,如果有人可以向我解释为什么它会变黑,而不是任何其他颜色也会很棒!

提前致谢.

解决方法:

如果源代码不是Bitmap,那么你所采用的方法并不能很好地工作,主要是因为最好使用绘图回调之外的传输模式将内容绘制到Canvas中(所以它只发生一次而不是每次绘制刷新)并在其他地方调用Drawable.draw()将不会产生正确的结果,因为边界将不会按预期设置.

一种显着更有效的方法是不修改源数据,只是将圆形剪辑应用于绘图画布.对于非常大的半径,这可能会产生一点锯齿,但在10px时它不会引人注意.这种方法唯一的另一个缺点是硬件加速目前不支持clipPath(),因此您必须将此视图的图层类型设置为软件,以便渲染在AndroID 3.0上运行

public class RoundedCornerImageVIEw extends ImageVIEw {    private Path roundedpath;    private int rounded;    public RoundedCornerImageVIEw(Context context) {        super(context);        init();    }    public RoundedCornerImageVIEw(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    public RoundedCornerImageVIEw(Context context, AttributeSet attrs,            int defStyle) {        super(context, attrs, defStyle);        init();    }    private voID init() {        // If the application is harDWare accelerated,        // must disable it for this vIEw.        setLayerType(VIEw.LAYER_TYPE_SOFTWARE, null);        // Set a default radius        setRounded(10);    }    @OverrIDe    protected voID onSizeChanged(int w, int h, int olDW, int oldh) {        if (w != olDW || h != oldh) {            roundedpath = new Path();            roundedpath.addRoundRect(new RectF(0, 0, w, h),                    rounded, rounded, Path.Direction.CW);        }    }    public int getRounded() {        return rounded;    }    public voID setRounded(int rounded) {        this.rounded = rounded;        roundedpath = new Path();        roundedpath.addRoundRect(new RectF(0, 0, getWIDth(), getHeight()),                rounded, rounded, Path.Direction.CW);    }    @OverrIDe    protected voID onDraw(Canvas canvas) {        //Apply the clip        canvas.clipPath(roundedpath);        //Let the vIEw draw as normal        super.onDraw(canvas);    }}

在修改版本中,您只需在每次视图或半径大小更改时更新剪切路径,并在绘制之前将该路径作为剪辑应用于“画布”.

HTH

总结

以上是内存溢出为你收集整理的在Android上为imageView渲染圆角全部内容,希望文章能够帮你解决在Android上为imageView渲染圆角所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1116376.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存