android - 重新缩放 ImageView 的位图时图像质量下降

标签 android matrix imageview scale

所以我有一个 ImageView,我已经为其设置了一个 OnTouchListener。 OnTouch 方法如下所示。

//Touch event related variables
int touchState;
final int IDLE = 0;
final int TOUCH = 1;
final int PINCH = 2;
float dist0, distCurrent;

public boolean onTouch(View view, MotionEvent event) {
    boolean handledHere = false;

    float distx, disty;

    final int action = event.getAction();

    switch(action & MotionEvent.ACTION_MASK){           
    case MotionEvent.ACTION_DOWN:
        //A pressed gesture has started, the motion contains the initial starting location.
        touchState = TOUCH;
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        //A non-primary pointer has gone down.
        touchState = PINCH;

        //Get the distance when the second pointer touch
        distx = event.getX(0) - event.getX(1);
        disty = event.getY(0) - event.getY(1);
        dist0 = FloatMath.sqrt(distx * distx + disty * disty);

        break;
    case MotionEvent.ACTION_MOVE:
        if(touchState == PINCH){                        
            //Get the current distance
            distx = event.getX(0) - event.getX(1);
            disty = event.getY(0) - event.getY(1);
            distCurrent = FloatMath.sqrt(distx * distx + disty * disty);

            drawMatrix((ImageView) view);

        } else {
            handledHere = startDrag (view);
        }

        break;
    case MotionEvent.ACTION_UP:
        //A pressed gesture has finished.
        touchState = IDLE;
        break;
    case MotionEvent.ACTION_POINTER_UP:
        //A non-primary pointer has gone up.
        touchState = TOUCH;
        break;

    }

    return handledHere;
}

private void drawMatrix(ImageView view){
    float curScale = distCurrent/dist0;

    if (curScale < 0.5){
        curScale = 0.5f;    
    } else if (curScale > 1.5){
        curScale = 1.2f;
    }

    Bitmap originalBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); //view.getDrawingCache();
    Canvas canvas = new Canvas(originalBitmap);
    view.draw(canvas);
    int[] originalSize = (int[]) view.getTag();
    int newHeight = (int) (originalSize[1] * curScale); 
    int newWidth = (int) (originalSize[0] * curScale); 

    view.setImageBitmap(getResizedBitmap(originalBitmap, newHeight, newWidth));
    view.getLayoutParams().height=newHeight;
    view.getLayoutParams().width=newWidth;

}

private Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}

问题是,当我使用手指手势放大或缩小时,图像质量会下降。例如,如果图像是 PNG 图像,具有半透明渐变,渐变将变成纯色模糊,图像的其余部分变成像素化......任何人都有解决方案,或者遇到相同的问题?

最佳答案

在 getResizedBitmap() 中添加过滤:

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);

关于android - 重新缩放 ImageView 的位图时图像质量下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13283658/

相关文章:

android - 在 Android 中按日期获取日历事件?

java - RxJava 过滤器运算符

android - 如何添加具有某些指定背景颜色的动态圆形 ImageView

android - 删除未反射(reflect)在图库中的图像

android - 更新通用图像加载器缓存

android - 任务 mockableAndroidJar, mockable-android- 执行失败。 jar 已经存在

android - 同步Firebase动态链接?

python - 调试数值稳定性问题的策略?

java - 实现 SparseMatrix 的有效方法

java - 如何格式化数组,使其在打印时看起来像矩阵?