我在 imageview 中设置了位图,并在触摸多次时执行旋转和拖动位图的操作。我希望在 imageview 中设置的位图不应该从 imageview 的特定区域之外。
有什么办法可以做到这一点。
请帮我这个..
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
scaleCount=scaleCount+scale;
angleCount = addAngle(angleCount, Math.toDegrees(angle));
if (!isInitialized) {
int w = getWidth();
int h = getHeight();
position.set(w / 2, h / 2);
isInitialized = true;
}
Paint paint = new Paint();
paint.setFilterBitmap(true);
//Log.v("Height and Width", "Height: "+ getHeight() + "Width: "+ getWidth());
transform.reset();
transform.postTranslate(-width / 2.0f, -height / 2.0f);
transform.postRotate((float) Math.toDegrees(angle));
transform.postScale(scale, scale);
transform.postTranslate(position.getX(), position.getY());
rectangleF = new RectF();
transform.mapRect(rectangleF);
//Log.v("VM", "Rect " + rectangleF.left + " " + rectangleF.top + " " + rectangleF.right + " " + rectangleF.bottom);
canvas.drawBitmap(bitmap, transform, paint);
canvas.restore();
BitmapWidth=BitmapWidth+bitmap.getScaledWidth(canvas);
BitmapHeight=BitmapHeight+bitmap.getScaledHeight(canvas);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
vca = null;
vcb = null;
vpa = null;
vpb = null;
x = event.getX();
y = event.getY();
try {
touchManager.update(event);
if (touchManager.getPressCount() == 1) {
vca = touchManager.getPoint(0);
vpa = touchManager.getPreviousPoint(0);
position.add(touchManager.moveDelta(0));
}
else {
if (touchManager.getPressCount() == 2) {
vca = touchManager.getPoint(0);
vpa = touchManager.getPreviousPoint(0);
vcb = touchManager.getPoint(1);
vpb = touchManager.getPreviousPoint(1);
VMVector2D current = touchManager.getVector(0, 1);
VMVector2D previous = touchManager.getPreviousVector(0, 1);
float currentDistance = current.getLength();
float previousDistance = previous.getLength();
if (currentDistance-previousDistance != 0) {
scale *= currentDistance / previousDistance;
}
angle -= VMVector2D.getSignedAngleBetween(current, previous);
/*angleCount=angleCount+angle;*/
}
}
invalidate();
}
catch(Exception exception) {
// Log.d("VM", exception.getMessage());
}
return true;
}