为什么在Canvas类的drawBitmap方法扭曲图像 [英] Why does the drawBitmap method in Canvas class distorts the image

查看:209
本文介绍了为什么在Canvas类的drawBitmap方法扭曲图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在Canvas类的drawBitmap()方法扭曲的形象呢?
当我使用setImageBitmap()的ImageView的功能设置上的ImageView的子类的位图,图像完美呈现。

现在有在Android中4.x中的错误,这导致setImageBitmap问题额外的菜单项。额外的菜单项(一般超过6个菜单正在使用更多的显示...)失去联系的倾听者。

我有以下code,显示图像如何使用两种不同的方式呈现。

  / **
 *绘制位图图标
 * /
@覆盖
保护无效的onDraw(帆布油画){
    super.onDraw(画布);    如果(位图== NULL){
       位= AppMain.getItemIcon(thisItem);
    }
    // drawBitmap(帆布,位图); //扭曲图像
    drawBitmap(位图); //运行完美}
    //绘制使用setImageBitmap
私人无效drawBitmap(位图位图){
    如果(位图!= NULL){
        this.setImageBitmap(位图);        矩阵m = this.getImageMatrix();
        m.setRectToRect(新RectF(0,0,bitmap.getWidth(),bitmap.getHeight()),
                        新RectF(0,0,this.getWidth(),this.getHeight()),
                        Matrix.ScaleToFit.CENTER);        this.setImageMatrix(米);    }}
    //绘制使用canvas.drawBitmap()
私人无效drawBitmap(帆布油画,位图位图){
    如果(位图!= NULL){
        矩阵M =新的Matrix();
        m.setRectToRect(新RectF(0,0,bitmap.getWidth(),bitmap.getHeight()),
                        新RectF(0,0,this.getWidth(),this.getHeight()),
                        Matrix.ScaleToFit.CENTER);        canvas.drawBitmap(位图,男,NULL);    }}


解决方案

也许这只是扩大规模。默认情况下的ImageView 使用双线性过滤绘制缩放位图。你可以尝试调用 Canvas.drawBitmap 时启用它。您需要创建新的漆的对象: //developer.android.com/reference/android/graphics/Paint.html#FILTER_BITMAP_FLAG相对=nofollow>相应标志(但在的onDraw 法):

 油漆mPaint =新的油漆(Paint.FILTER_BITMAP_FLAG);

,然后提供这个油漆对象的 Canvas.drawBitmap 方法:

  canvas.drawBitmap(位图,男,mPaint);

Why does the drawBitmap() method in Canvas class distorts the image? When I use setImageBitmap() function of ImageView to set the bitmap on a subclass of ImageView, the image is rendered perfectly.

Now there is a bug in Android 4.x and this setImageBitmap causes problem for extra menu items. The extra menu items (generally beyond six menu items that are displayed using More...) looses touch listener.

I have following code that shows how the image is rendered using two different method.

/**
 * draws bitmap icons
 */
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if(bitmap == null){
       bitmap = AppMain.getItemIcon(thisItem);
    }
    //drawBitmap(canvas, bitmap); //distorts image
    drawBitmap(bitmap); //works perfect

}
    //draw using setImageBitmap
private void drawBitmap(Bitmap bitmap){
    if(bitmap != null){
        this.setImageBitmap(bitmap);

        Matrix m = this.getImageMatrix();
        m.setRectToRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), 
                        new RectF(0, 0, this.getWidth(), this.getHeight()),
                        Matrix.ScaleToFit.CENTER);

        this.setImageMatrix(m);

    }

}
    //draw using canvas.drawBitmap()
private void drawBitmap(Canvas canvas, Bitmap bitmap){
    if(bitmap != null){
        Matrix m = new Matrix();
        m.setRectToRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), 
                        new RectF(0, 0, this.getWidth(), this.getHeight()), 
                        Matrix.ScaleToFit.CENTER);

        canvas.drawBitmap(bitmap, m, null);

    }

}

解决方案

Probably it is just scaled up. By default ImageView draws scaled bitmaps using bilinear filtering. You can try enabling it when calling Canvas.drawBitmap. You need to create new Paint object with corresponding flag (but don't do this in onDraw method):

Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);

And then supply this Paint object to the Canvas.drawBitmap method:

canvas.drawBitmap(bitmap, m, mPaint);

这篇关于为什么在Canvas类的drawBitmap方法扭曲图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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