【问题标题】:Why background of Round imageView is dark?为什么 Round imageView 的背景是暗的?
【发布时间】:2014-06-05 07:36:27
【问题描述】:

我创建了一个基于this answer 的圆形imageView。 它使图像完美圆润。但是,我有两个问题。

  • 图片旋转了 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);
    }
}

我在布局中添加它的方式:

<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" />

这是截图:

【问题讨论】:

  • Hesam ,我得到你的代码并把 mTopCard.setLayerType(LAYER_TYPE_HARDWARE, null);在 init() 方法中并为我工作,我在清单上也有硬件加速标志“true”

标签: android imageview android-imageview


【解决方案1】:

试着把它放在 RoundImageView 类中:

    public Bitmap getCroppedBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    //return _bmp;
    return output;
}

然后把这个放到onDraw中:

getCroppedBitmap(BitmapFactory.decodeResource(getResources(), getDrawable()));

【讨论】:

  • 谢谢,只有一件事。 BitmapFactory.decodeResource() 将 getResources 视为正确的第一个参数,将“int”视为可绘制的第二个参数。我应该传递其他东西而不是 getDrawable() 吗?
  • 如果图片总是一样的,那么试试R.drawable.image_id。否则,请尝试以另一种方式将图像制作为位图并将其传递给 getCroppedImage
  • 我用这种方式stackoverflow.com/a/8306683/513413(获取图像的位图)。图像显示,但它是方形而不是圆形。我试图找出问题所在。您的代码似乎没问题!
【解决方案2】:

由于我找不到解决此问题的方法,我使用了RoundedImageView library。固定imageView的宽度和高度时很好。 这是我的示例:

<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"/>

【讨论】:

    【解决方案3】:

    我以前也有过同样的经历,因为我在 UI 线程中从数据库中加载了一些东西,你是否在 UI 线程中从数据库/磁盘中加载了一些东西?如果是,您需要使用 AsynTask 加载它,如果不是,它将影响您的 UI 渲染过程

    【讨论】:

      【解决方案4】:

      就是这样:android:scaleType="center"

      从这里尝试其他 scaleTypes 并查看结果:

      中心, CENTER_CROP, CENTER_INSIDE , FIT_CENTER , FIT_END , FIT_START, FIT_XY, 矩阵

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-16
        • 1970-01-01
        • 2020-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多