我像疯子一样努力工作了超过我不愿承认的时间,并且多次放弃了我的尝试。这是我所拥有的最原始的版本,但是有两个问题:
1。)它只会缩放到图像的左上角,而没有其他位置。我希望用户可以缩放到他们想要的位置。
2。)我希望能够为用户平移图像,但是屏幕边缘使我受阻。可以这么说,我不确定如何保持这种状态滚动。
我看了看android.graphics.camera,还玩了android矩阵类,但是没法玩。我认为这可能是因为绘图循环部分使我的逻辑意识有些偏离,因为我通常不使用这些逻辑。
在绘制循环内调用的方法:
public void drawFrame(Canvas canvas) {
//super.onDraw(canvas);
int newWidth=0;
int newHeight=0;
float scale = gestures.getScaleFactor();
if(this.mBackgroundBitmap!=null) {
canvas.save();
float screenScaleX = (float) (this.mBackgroundBitmap.getWidth() / (float) getWidth()) * scale;
float screenScaleY = (float) (this.mBackgroundBitmap.getHeight() / (float) getHeight()) * scale;
canvas.translate(gestures.getScaleFocusX()/2, gestures.getScaleFocusY()/2);
newWidth = Math.round(this.mBackgroundBitmap.getWidth() / screenScaleX);
newHeight = Math.round(this.mBackgroundBitmap.getHeight() / screenScaleY);
scaledBitmap = Bitmap.createScaledBitmap(this.mBackgroundBitmap, newWidth, newHeight, true);
canvas.drawBitmap(scaledBitmap,0,0, new Paint());
canvas.restore();
}
我在自定义Surfaceview中使用的手势类:
public class MaelstromGestures implements View.OnTouchListener, ScaleGestureDetector.OnScaleGestureListener, View.OnLongClickListener {
private View view;
private ScaleGestureDetector gestureScale;
public MapActivity mapActivity;
public float getScaleFactor() {
return scaleFactor;
}
public void setScaleFactor(float scaleFactor) {
this.scaleFactor = scaleFactor;
}
private float scaleFactor = 1;
public boolean isInScale() {
return inScale;
}
boolean inScale=false;
private float scaleFocusX = 0;
private float scaleFocusY = 0;
public float getScaleFocusX(){
return gestureScale.getFocusX();
}
public float getScaleFocusY(){
return gestureScale.getFocusY();
}
public MaelstromGestures (Context c){ gestureScale = new ScaleGestureDetector(c, this); }
@Override
public boolean onTouch(View view, MotionEvent event) {
this.view = view;
gestureScale.onTouchEvent(event);
mapActivity.doTouch(view,event);
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
scaleFactor *= detector.getScaleFactor();
//scaleFactor = (scaleFactor < 0.5f ? 0.5f : scaleFactor); // prevent our view from becoming too small //
scaleFactor = Math.max(1.0f, Math.min(scaleFactor, 3.0f));//((float)((int)(scaleFactor * 25))) / 25; // Change precision to help with jitter when user just rests their fingers //
//view.setScaleX(scaleFactor);
//view.setScaleY(scaleFactor);
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
inScale = true;
scaleFocusX = gestureScale.getFocusX();
scaleFocusY = gestureScale.getFocusY();
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) { inScale = false; }
}