[英]zoom image within set bounds
我有一個可以查看表面的應用程序。 在此表面視圖上,一次最多顯示兩個位圖,它們是一部短片的剪輯。 我的工作正常,但是我允許用戶縮放和平移圖像/剪輯。 我想將屏幕平均分割,因此在垂直方向上,我將使一個剪輯占據屏幕的上半部分,而另一個剪輯占據屏幕的下半部分。 由於這是一個表面視圖,因此這些視圖沒有實際分離。
目前,如果繪制的第二個位圖越過屏幕空間的假想邊界,則它會覆蓋在另一個位圖上。 我只想剪掉跨越邊界的圖像部分,有人知道在畫布上繪圖時該如何執行?
參量
private float dx; // postTranslate X distance
private float dy; // postTranslate Y distance
private float[] matrixValues = new float[9];
float matrixX = 0; // X coordinate of matrix inside the ImageView
float matrixY = 0; // Y coordinate of matrix inside the ImageView
float width = 0; // width of drawable
float height = 0; // height of drawable
改變
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) { // movement of first finger
// matrix.set(savedMatrix);
//.
matrix.set(savedMatrix);
matrix.getValues(matrixValues);
matrixX = matrixValues[2];
matrixY = matrixValues[5];
width = matrixValues[0] * (((ImageView) view).getDrawable()
.getIntrinsicWidth());
height = matrixValues[4] * (((ImageView) view).getDrawable()
.getIntrinsicHeight());
dx = event.getX() - start.x;
dy = event.getY() - start.y;
//if image will go outside left bound
if (matrixX + dx < 0){
dx = -matrixX;
}
//if image will go outside right bound
if(matrixX + dx + width > view.getWidth()){
dx = view.getWidth() - matrixX - width;
}
//if image will go oustside top bound
if (matrixY + dy < 0){
dy = -matrixY;
}
//if image will go outside bottom bound
if(matrixY + dy + height > view.getHeight()){
dy = view.getHeight() - matrixY - height;
}
matrix.postTranslate(dx, dy);
//...
// if (view.getLeft() >= -392) {
// matrix.postTranslate(event.getX() - start.x, event.getY()
// - start.y);
//
// }
} else if (mode == ZOOM) { // pinch zooming
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 5f) {
matrix.set(savedMatrix);
scale = newDist / oldDist; // thinking i need to play around
// with this value to limit it**
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
我想通了,如果其他人遇到類似問題,我將在此處發布答案。 我正在做的事情類似於以下內容。 我創建了要使用的新畫布對象以及這些畫布對象的可寫位圖。 然后,我可以在單個畫布對象上執行任何繪制和轉換,然后將它們繪制到SurfaceView的主畫布上。
if(workingBitmap==null){
workingBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight()/2, Bitmap.Config.ARGB_4444);
c.setBitmap(workingBitmap);
workingBitmap2 = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight()/2, Bitmap.Config.ARGB_4444);
c2.setBitmap(workingBitmap2);
}
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);
c.drawBitmap(bitmap, 0,0,null);
c2.drawBitmap(bitmap2, 0,-100,null);
canvas.drawBitmap(workingBitmap, 0,0,null);
canvas.drawBitmap(workingBitmap2, 0,canvas.getHeight()/2,null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.