如何画“酥脆”低分辨率ImageView上的线条? [英] How to draw "crisp and sharp" lines on an low resolution ImageView?

查看:289
本文介绍了如何画“酥脆”低分辨率ImageView上的线条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过执行以下操作在ImageView上绘制线条:

I am drawing lines on an ImageView by doing something like this:

Bitmap imageBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Bitmap duplicateBitmap = Bitmap.createBitmap(imageBitmap.getWidth(),imageBitmap.getHeight(),Bitmap.Config.RGB_565);

Canvas targetCanvas = new Canvas(duplicateBitmap);
targetCanvas.drawBitmap(imageBitmap,0,0,null);
Paint paint = new Paint();
targetCanvas.drawLine(0f,100f, imageBitmap.getWidth(),100f,paint);
imageView.setImageDrawable(new BitmapDrawable(getResources(),duplicateBitmap));

这很好用当图像具有不错的分辨率或良好的分辨率时

但是它看起来像这样图片分辨率较低时。 (这是不好的经历)

But it looks like this when the image has a low resolution. (Which is a bad experience)

现在,如何在低分辨率图像上绘制清晰锐利的线条?

Now, how do I draw sharp and crisp lines on a low res image?

我曾经以为我曾经应使用高分辨率画布遮盖图像。但是那时我对如何继续这个想法一无所知。另外,这可能是内存效率低下的实现。

For once I thought I should mask the image with a high res canvas. But then I was clueless about how to proceed with this idea. Also, this might be a memory inefficient implementation.

推荐答案

我终于弄清楚了@D。 Karchnak的指导...

I finally figured out with @D. Karchnak's guidance...


假设您的手机屏幕为1080x1920,您使用
全屏ImageView创建了一个布局,因此该ImageView也是1080x1920 。现在
将.png图像加载为BitmapDrawable。此PNG图像的
分辨率为400x400,因此BitmapDrawable内的位图也是
也是400x400。如果您画出这个小字样,位图到全屏
ImageView时,由于分辨率低,它将变得模糊不清。如果
对此400x400位图绘制一条线,则该线也会模糊
。这就是为什么它可以以适当的分辨率或良好的分辨率工作的原因。

Let's say your phone's screen is 1080x1920, you create a layout with a fullscreen ImageView, therefore this ImageView is also 1080x1920. Now you load a .png image as a BitmapDrawable. This PNG image has a resolution of 400x400, therefore the bitmap inside BitmapDrawable is also 400x400. If you draw this "small" bitmap to a fullscreen ImageView, it will get blurred out, because of low resolution. If you draw a line to this 400x400 bitmap, this line will also get blurred out. That's also why it works with decent or good resolution.

我要做的就是创建一个Android ImageView的子类,然后将绘制逻辑转换为onDraw方法(当然是在重写它之后)。像这样:

All I need to do is, create a subclass of android's ImageView and then shift my drawing logic into the onDraw method (of course after overriding it). Like so:

public class FineLineImageView extends AppCompatImageView {

    public FineLineImageView(Context context) {
        this(context, null);
    }

    public FineLineImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FineLineImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);
        // logic for drawing goes here
    }
}

这篇关于如何画“酥脆”低分辨率ImageView上的线条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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