2014-03-25 38 views
0

我需要一点帮助。我有一个带有触摸监听器的ImageView,并且我能够将所有触摸输入捕获到矩阵中,并将该矩阵应用于ImageView和VOILA!图像平移并适当放大。Android位图平移/缩放/作物

这是麻烦:我现在想要以这样一种方式来裁剪图像,使它始终以相同的尺寸结束;例如300x300的图像。

换句话说,假设我的屏幕中间有一个300x300的方块,用户可以平移和放大图像,直到感兴趣的项目适合该方块,然后点击“下一步”。我想有一个由此产生的图像,裁剪照片只是包含在框中的300x300部分。

有道理?请帮忙!谢谢同志们!到目前为止我已经尝试了一些代码。

float[] matrixVals = new float[9]; 
    ImageTouchListener.persistedMatrix.getValues(matrixVals); 
    model.setCurrentBitmap(Bitmap.createBitmap(model.getOriginalBitmap(), 0, 0, model.getTargetWidth(), model.getTargetHeight(), ImageTouchListener.persistedMatrix, true)); 
    model.setCurrentBitmap(Bitmap.createBitmap(model.getCurrentBitmap(), Math.round(matrixVals[Matrix.MTRANS_X]), Math.round(matrixVals[Matrix.MTRANS_Y]), model.getTargetWidth(), model.getTargetHeight(), null, false)); 

最后,我也想能够将图像缩入盒,其中边缘实际上可能需要使用黑色或白色或某种边界填写...到目前为止,一切当我点击下一页时,我除了没有平移或缩小所有崩溃。

再次感谢!

+0

https://github.com/rombdn/img-touch-canvas –

回答

0

这对我来说真不明白你的问题是什么,计算或绘图问题...如果它是一个绘图问题,我可能有解决方案......

创建一个新的位图,让画布并绘制大图像的正确RECT到新的较小....

Bitmap bigPicture; //your big picture 
    Bitmap bitmap = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(bitmap); 
    Rect source = ... 
    Rect dest = ... 
    c.drawBitmap(bigPicture, source, dest, paint); 

现在,如果你的问题是一个计算问题,我可能有一个解决方案太...

1

看到这个自定义的ImageView ,最重要的部分是onTouchEvent,其中裁剪的Bitmap是crea ted并保存到/ sdcard进行验证:

class IV extends ImageView { 
    Paint paint = new Paint(); 
    Rect crop = new Rect(); 

    public IV(Context context) { 
     super(context); 
     paint.setColor(0x660000ff); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     crop.set(w/2, h/2, w/2, h/2); 
     crop.inset(-75, -75); 
    } 

    @Override 
    public void setImageResource(int resId) { 
     super.setImageResource(resId); 
     setScaleType(ScaleType.MATRIX); 
     Matrix m = getImageMatrix(); 
     m.postScale(2, 2); 
     m.postTranslate(40, 30); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     Bitmap croppedBitmap = Bitmap.createBitmap(crop.width(), crop.height(), Config.ARGB_8888); 
     Canvas c = new Canvas(croppedBitmap); 
     c.translate(-crop.left, -crop.top); 
     c.concat(getImageMatrix()); 
     getDrawable().draw(c); 

     // just save it for test verification 
     try { 
      OutputStream stream = new FileOutputStream("/sdcard/test.png"); 
      croppedBitmap.compress(CompressFormat.PNG, 100, stream); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     return false; 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     canvas.drawRect(crop, paint); 
    } 
}