Android Canvas drawBitmap功能效率

时间:2012-09-27 05:30:45

标签: android image-processing canvas matrix bitmap

我正在尝试使用矩阵在画布上显示位图。

      canvas.drawBitmap(currentBitmap, m_matrix, tempPaint);

但结果显示,图像看起来很奇怪。之后我用边界显示了它

     canvas.drawBitmap(currentBitmap, 0, 0, tempPaint);

但是这里的图像看起来不错,在这两种情况下图像都没有缩放。

如何为初始显示设置矩阵属性?

矩阵显示是否使用其他东西来显示图像,因为该图像正在变化?

请提供任何教程以获取详细说明。

2 个答案:

答案 0 :(得分:0)

你必须使用Matrix。后缩放以缩放图像,如下例所示。

private void drawMatrix(){

Matrix matrix = new Matrix();      matrix.postScale(curScale,curScale);

 canvas.drawBitmap(currentBitmap, matrix, tempPaint);} 

Haven还没试过......但是应该工作......

答案 1 :(得分:0)

经过大量搜索后我终于找到了解决方案,一些与同一问题相关的线程帮助了我。

Quality Issue

Quality problems when resizing an image at runtime

我已经按照谷歌的教程加载位图并编写了我自己的功能来缩放位图。

public static Bitmap scaleDownBitmap(Bitmap original, boolean recycleOriginal, int newmaxWidth , int newmaxHeight){

        if(original == null)
             return null;

        Bitmap rtr= null;
        try{

                int origWidth  = original.getWidth();
                int origHeight = original.getHeight();


                if(origWidth <= newmaxWidth && origHeight <= newmaxWidth){
                        Bitmap b = Bitmap.createBitmap(original);
                        if (recycleOriginal && (original != b))
                            original.recycle();
                    return b;
                }

                int newWidth = 0;
                int newHeight = 0;

                float ratio;

                if(origWidth > origHeight){
                        ratio = (float)origWidth/(float)origHeight;
                        newWidth = newmaxWidth;
                        newHeight = (int)((float)newWidth/ratio);

                } 
                else{
                        ratio = (float)origHeight/(float)origWidth;
                        newHeight = newmaxHeight;
                        newWidth = (int)((float)newHeight/ratio);
                }

                rtr = CreateScaledBitmap(original , newWidth , newHeight);

                if(recycleOriginal && original != rtr)
                        original.recycle();

        }
        catch (Exception e) {
            Log.d("Image Compress Error", e.getMessage());
        }

        return rtr;

 }

     public static Bitmap CreateScaledBitmap(Bitmap paramBitmap, int paramInt1, int paramInt2)
   {
        Bitmap localBitmap = Bitmap.createBitmap(paramInt1, paramInt2, paramBitmap.getConfig());
        Canvas localCanvas = new Canvas(localBitmap);
        localCanvas.setDrawFilter(new PaintFlagsDrawFilter(0, 2));
        localCanvas.drawBitmap(paramBitmap, new Rect(0, 0, paramBitmap.getWidth(), paramBitmap.getHeight()),
                                            new Rect(0, 0, paramInt1, paramInt2), null);
    return localBitmap;
}

在获取图像后,我已将以下参数传递给绘画。

tempPaint.setFilterBitmap(true); //Line 1
canvas.drawBitmap(currentBitmap, m_matrix, tempPaint);