Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么使用矩阵查看图像与使用位图的结果不同?_Android_Matrix_Imageview_Android Bitmap - Fatal编程技术网

Android 为什么使用矩阵查看图像与使用位图的结果不同?

Android 为什么使用矩阵查看图像与使用位图的结果不同?,android,matrix,imageview,android-bitmap,Android,Matrix,Imageview,Android Bitmap,为什么将矩阵与ImageView setImageMatrix(矩阵)一起使用,以及 createBitmap(bmp,0,0,bmp.width(),bmp.height(),matrix,true); 给出不同的结果 备选案文1。 使用setImageMatrix(矩阵);只有我能正常工作 setLayoutParams(设备宽度、设备高度); 这意味着drawable的大小可能小于ImageView的大小,因为您可以看到setBackgroundColor(Color.RED)绘制整个屏幕

为什么将矩阵与ImageView setImageMatrix(矩阵)一起使用,以及 createBitmap(bmp,0,0,bmp.width(),bmp.height(),matrix,true); 给出不同的结果

备选案文1。 使用setImageMatrix(矩阵);只有我能正常工作 setLayoutParams(设备宽度、设备高度); 这意味着drawable的大小可能小于ImageView的大小,因为您可以看到setBackgroundColor(Color.RED)绘制整个屏幕,而我需要具有精确可绘制大小的ImageView,但是当我使用setLayoutParams()和适当的位图大小时,位图会被裁剪 您可以看到该图像,其中显示带有deviceSize layoutParams和bitmapSize layoutParams的ImageView 这是我的矩阵 矩阵{[1.8266667,0.0,110.0][0.0,1.3733333117.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);
}
我接收到显示在图像第二部分的结果,而需要像第一部分显示的那样变换ImageView,而不占用整个屏幕,而我得到这样的结果 评论这句话 setRectRect(imageRect、viewRect、matrix.ScaleToFit.START);

使用第2个选项时,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;

试着去理解。wrap_内容意味着ImageView将只与图像一样大,但您正在谈论缩放图像。请编辑您的问题,以详细说明您正试图实现的目标。我最近在ImageView中做了很多关于矩阵缩放的工作,所以对它的工作原理非常熟悉。我可以给您的一个提示是确保ImageView已运行onLayout(),否则ImageView将没有设置矩阵所需的宽度和高度。假设ImageView的大小与match_parent匹配,并且位图的大小设置为覆盖整个ImageView的大小,您可以放大,但是当你缩小位图时,它永远不会比图像视图小,这对你有用吗?亲爱的@kris larson我已经编辑了我的问题并更改了屏幕截图,我使用了你发布的代码,但我无法得到正确的结果。它不会做任何更改:(
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);
}