在布局文件中使用时,RoundedImageView不会舍入图像

时间:2015-02-02 22:09:56

标签: android

我有以下RoundedImageView

public class RoundedImageView extends ImageView {

    public RoundedImageView(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onDraw(Canvas canvas) {
        if (this.getDrawable() == null) {
            return;
        }

        if (this.getWidth() == 0 || this.getHeight() == 0) {
            return;
        }

        Bitmap bitmapDrawable = ((BitmapDrawable) this.getDrawable()).getBitmap();
        Bitmap bitmap = bitmapDrawable.copy(Bitmap.Config.ARGB_8888, true);

        int width = this.getWidth(); 

        Bitmap roundBitmap = getCroppedBitmap(bitmap, width);
        canvas.drawBitmap(roundBitmap, 0, 0, null);
    }

    public static Bitmap getCroppedBitmap(Bitmap bitmap, int radius) {
        Bitmap scaledBitmap = null;

        if (bitmap.getWidth() != radius || bitmap.getHeight() != radius) {
            float smallest = Math.min(bitmap.getWidth(), bitmap.getHeight());
            float factor = smallest / radius;

            scaledBitmap = Bitmap.createScaledBitmap(bitmap, (int)(bitmap.getWidth() / factor), (int)(bitmap.getHeight() / factor), false);
        } else {
            scaledBitmap = bitmap;
        }

        Bitmap output = Bitmap.createBitmap(radius, radius, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, radius, radius);

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.parseColor("#BAB399"));
        canvas.drawCircle(radius / 2 + 0.7f, radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(scaledBitmap, rect, rect, paint);

        return output;
    }
}

当我在以下代码中使用此RoundedImageView时,它可以完美运行:

    RelativeLayout.LayoutParams layoutParamsImageProfile = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParamsImageProfile.addRule(RelativeLayout.ABOVE, bottomDummyID);
    layoutParamsImageProfile.addRule(RelativeLayout.LEFT_OF, rightDummyID);

    RoundedImageView imageViewProfile = new RoundedImageView(this.getActivity());
    imageViewProfile.setLayoutParams(layoutParamsImageProfile);
    imageViewProfile.setImageDrawable(Helper.loadDrawableFromFile(this.getResources(), Helper.getProfileMenuImagePath()));

当我从布局文件中使用它时:

<de.myproject.views.RoundedImageView
    android:id="@+id/profile_common_imageview_profile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="20dp"
    android:contentDescription="@string/text_description" />

图像没有圆角。它们仍然是矩形。为什么机械师不是从布局工作,只是从代码?

1 个答案:

答案 0 :(得分:1)

我无法回答你原来的问题,但是我可以告诉你,你的onDraw()方法现在的方式太重了!你知道这种方法会被称为很多次,所以它必须尽可能快。

如果你想获得更圆的角落,我建议你阅读Romain Guy的a THIS RECIPE(是Android的核心开发者)。与您的代码相比,它具有许多性能优势。

另外,您可以查看a THIS LIBRARY声称它基于Romain Guy的方法。