为什么圆形的ImageView的背景是黑色的? [英] Why background of Round imageView is dark?

查看:602
本文介绍了为什么圆形的ImageView的背景是黑色的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已根据这个答案创造了一个圆形的ImageView。
它使图像完美的圆。不过,我有两个问题。

I have created a round imageView based on this answer. It makes image round perfectly. However, I have two problems.


  • 图像旋转90度,我不知道为什么(用户点击一个按钮,用户的画廊显示,用户选择一个图像作为他的个人资料图)

  • 图像背景太暗,我不知道从何而来。

这是我使用的类:

public class RoundImageView extends ImageView {

    private Path path;
    private Paint paint;
    private PorterDuffXfermode porterDuffXfermode;

    public RoundImageView(Context context) {
        super(context);
        init();
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        setWillNotDraw(false);

        path = new Path();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawColor(Color.TRANSPARENT);

        // Create a circular path.
        final float halfWidth = canvas.getWidth()/2;
        final float halfHeight = canvas.getHeight()/2;
        final float radius = Math.max(halfWidth, halfHeight);

        path.addCircle(halfWidth, halfHeight, radius, Path.Direction.CCW);

        paint.setXfermode(porterDuffXfermode);
        canvas.drawPath(path, paint);
    }
}

的办法,我在布局添加它:

The way I'm adding it in layout:

<com.allstarxi.widget.RoundImageView
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:src="@drawable/ic_launcher"
                    android:id="@+id/imageView"
                    android:contentDescription="@string/general_content_description"
                    android:scaleType="center" />

这是截图:

推荐答案

一直以来,我找不到如何解决这个问题,我用的 RoundedImageView库。当你修复宽度和ImageView的高度这是件好事。
这是我的示例:

Since, I couldn't find how to fix this issue, I used RoundedImageView library. It is good when you fix width and height of imageView. This is my sample:

<com.makeramen.RoundedImageView
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:layout_width="36dp"
                    android:layout_height="36dp"
                    android:src="@drawable/ic_launcher"
                    android:id="@+id/ivUserPic"
                    android:contentDescription="@string/general_content_description"
                    android:scaleType="centerCrop"
                    app:mutate_background="true"
                    app:border_width="0dp"
                    app:corner_radius="18dp"
                    app:oval="true"/>

这篇关于为什么圆形的ImageView的背景是黑色的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆