2015-05-28 46 views
0

为什么使用矩阵与ImageView setImageMatrix(矩阵)和 Bitmap.createBitmap(bmp,0,0,bmp.width(),bmp.height(),matrix,true); 给出不同的结果?为什么不同的结果使用矩阵图像视图与位图?

选项1. 使用setImageMatrix(matrix);它只适用于我 setLayoutParams(deviceWidth,deviceHeight); 这意味着drawable的大小可能小于ImageView的大小,因为您可以看到setBackgroundColor(Color.RED)绘制整个屏幕,而我需要具有精确可绘制大小的ImageView,但是当我使用setLayoutParams()大小适当的位图大小位图被裁剪 你可以看到图像,我显示ImageView与deviceSize-layoutParams和bitmapSize-layoutParams 这是我的矩阵 矩阵{[1.8266667,0.0,110.0] [0.0,1.3733333,117.000015] [ 0.0,0.0,1.0]}

private void drawBitmap() { 
    imageView = new ImageView(this); 
    imageView.setLayoutParams(new ViewGroup.LayoutParams(deviceWidth, deviceHeight)); 
    imageView.setBackgroundColor(Color.RED); 
    imageView.setScaleType(ImageView.ScaleType.MATRIX); 
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image4); 

    Matrix matrix = loadMatrix(bmp); 
    RectF viewRect = new RectF(0, 0, deviceWidth, deviceHeight); 
    RectF imageRect = new RectF(0, 0, bmp.getWidth(), bmp.getHeight()); 
    matrix.setRectToRect(imageRect, viewRect, Matrix.ScaleToFit.START); 
    imageView.setImageMatrix(matrix); 
    imageView.setImageBitmap(bmp); 
    setContentView(imageView); 
} 

我接收上的图像的一部分2_nd显示的结果,当需要已经改变的ImageView等上第一部分显示仅不占用整个屏幕,WH我得到这样的结果 评论此行 matrix.setRectToRect(imageRect,viewRect,Matrix.ScaleToFit.START); enter image description here

同时使用2_nd选项是没有问题的LayoutParams 而它在某种程度上裁剪ImageView的,而不是充分展示

矩阵包括应当扩大和翻译的ImageView

数据,我需要的一个正确的解决方案将ImageView转换为可绘制的大小。我的意思是ImageView应该占据(或ImageView的触摸区域)应该是可绘制的部分。

Bitmap targetBitmap = Bitmap.createBitmap((source.getWidth()), 
      (source.getHeight()), 
      Bitmap.Config.ARGB_8888); 
    Paint p = new Paint(); 
    p.setAntiAlias(true); 
    p.setDither(true); 
    p.setFilterBitmap(true); 
    RectF rectF = new RectF(0, 0, source.getWidth(), source.getHeight()); 
    matrix.mapRect(rectF); 

    targetBitmap = Bitmap.createBitmap((int) rectF.width(), (int) rectF.height(), Bitmap.Config.ARGB_8888); 

    Canvas tempCanvas = new Canvas(targetBitmap); 
    tempCanvas.drawBitmap(source, matrix, p); 
    if (targetBitmap != source) { 
     source.recycle(); 
    } 
return targetBitmap; 
+0

试图了解。 wrap_content意味着ImageView将只与图像一样大,但您正在讨论缩放图像。请编辑你的问题,以更多地讨论你想要达到的目标。最近我在ImageView中通过矩阵缩放工作了很多,所以我很熟悉它的工作原理。我可以给你的一个提示是确保ImageView已经运行onLayout(),否则ImageView将没有设置矩阵所需的宽度和高度。 –

+0

假设ImageView的大小设置为match_parent,并且您的位图被设置为覆盖整个ImageView的大小,并且您可以放大,但是当缩小位图时,该位图永远不会比ImageView小,是否适合您? –

回答

0
private void drawBitmap() { 

    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image4); 
    int imageWidth = bmp.getWidth(); 
    int imageHeight = bmp.getHeight(); 

    // first we set the size of the view to have the same aspect ratio 
    // as the image 
    int viewWidth = deviceWidth; 
    int viewHeight = deviceWidth * imageHeight/imageWidth; 
    imageView.setLayoutParams(new LayoutParams(viewWidth, viewHeight)); 

    // imageView.setBackgroundColor(Color.RED); 

    // get a rectangle that is the size of the ImageView 
    RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); 

    // get a rectangle that is the size of the bitmap 
    RectF imageRect = new RectF(0, 0, imageWidth, imageHeight); 

    // init the matrix so it will map the image to the full size of the view 
    // imageRect is the source rectangle, the current size of the image 
    // viewRect is the target rectangle, the size we want the image to be 
    // setRectToRect will calculate the matrix required to map source to target 
    // we use scale to fit so that the aspect ratio doesn't change 
    matrix.setRectToRect(imageRect, viewRect, ScaleToFit.START); 

    // tell the ImageView to use this matrix 
    // make sure android:scaleType="matrix" is in the ImageView attributes 
    imageView.setMatrix(matrix) 

    imageView.setImageBitmap(bmp); 
} 
+0

亲爱的@kris larson我编辑了我的问题,并更改了屏幕截图 ,我已经使用了您的发布代码,但无法获得正确的结果。 –

+0

它不会做任何改变:( –

相关问题