我有一个应用程序,我有一个表面视图。在此表面视图中,我一次最多显示两个位图,这些位图是来自短电影的剪辑。我有这个工作正常,但我允许用户缩放和平移图像/剪辑。我想将屏幕平分,所以在垂直方向上我会有一个夹子占据屏幕的上半部分而另一个夹子占据下半部分。由于这是一个表面视图,因此这些视图没有实际分离。
目前我绘制的第二个位图是否穿过屏幕不动产的虚边界,然后将其覆盖在另一个位图上。我想切掉越过边界的图像部分,有人知道我在画布上画画时如何强制执行此操作吗?
答案 0 :(得分:1)
参数
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;
答案 1 :(得分:0)
我想出来了,我会在这里发布答案,以防其他人遇到类似的事情。我正在做的是类似于以下内容。我创建了可以使用的新画布对象和这些画布对象的可写位图。然后,我可以对各个画布对象执行任何绘制和转换,然后将它们绘制到我的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);