我已經實現了一個可移動的圖像視圖,我可以用手指移動並縮放。我使用拖放框架來移動它(因爲我也需要拖放),並且我有一個處理縮放的ScaleGestureDetector.SimpleOnScaleGestureListener
。 MovableImageView
擴展了Android的正常ImageView
。當用戶觸摸圖像視圖時,方法onTouchEvent(MotionEvent event)被調用。這種方法看起來是這樣的:組合拖放和縮放
public boolean onTouchEvent(MotionEvent event)
{
scaleDetector.onTouchEvent(event);
startDragNDrop();
return true;
}
startDragNDrop()
看起來是這樣的:
private void startDragNDrop() {
// Create a new ClipData.
// This is done in two steps to provide clarity. The convenience method
// ClipData.newPlainText() can create a plain text ClipData in one step.
// Create a new ClipData.Item from the ImageView object's tag
ClipData.Item item = new ClipData.Item(mediaItem.getMediaIdentifier());
// Create a new ClipData using the tag as a label, the plain text MIME type, and
// the already-created item. This will create a new ClipDescription object within the
// ClipData, and set its MIME type entry to "text/plain"
String[] mimeType = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData((CharSequence) this.getTag(),mimeType,item);
// Instantiates the drag shadow builder.
DragShadowBuilder myShadow = new ZPACDragShadowBuilder(this);
actualyDraggedView = this;
// Starts the drag
this.startDrag(dragData, // the data to be dragged
myShadow, // the drag shadow builder
null, // no need to use local data
0); // flags (not currently used, set to 0)
}
它基本上創建dragshadow並啓動拖動操作。
的onScale()實施,即scaleDetector.onTouchEvent(event)
後稱爲如下:
public boolean onScale(ScaleGestureDetector detector) {
float scaleFactor = MovableImageView.this.SCALE_FACTOR;
scaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 2.0f));
int width = (int) (MovableImageView.this.getWidth()*scaleFactor);
int height = (int) (MovableImageView.this.getHeight()*scaleFactor);
Log.e("MovableImageView", "Scale Gesture Captured. Scaling Factor " + scaleFactor + " old width " + MovableImageView.this.getWidth() + ", new width " + width);
AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams(width, height, MovableImageView.this.getLeft(), MovableImageView.this.getTop());
parentLayout.updateViewLayout(MovableImageView.this, layoutParams);
invalidate();
return true;
}
字段SCALE_FACTOR
是一個浮體和所述值是1.f
。而parentLayout
是一個擴展的AbsoluteLayout,用於管理屏幕上ImageView的位置和大小。
我的問題是,縮放不起作用,只有拖放。不執行縮放,只能在視圖中移動。如果我註釋掉linke startDragNDrop()
,那麼縮放工作,但顯然不是在視圖周圍移動。有沒有人有更好的想法將這些與imageview中的東西結合起來?
你有沒有找到一個解決方案S'我有類似的問題 – Amanni 2014-03-17 10:20:04
不幸的是,沒有。我不得不最終放棄縮放功能。 – Dude 2014-03-17 13:44:31