【发布时间】:2012-01-12 16:32:53
【问题描述】:
我已经实现了一个可移动的图像视图,我可以用手指四处移动并进行缩放。我使用拖放框架来移动它(因为我还需要它可拖放),并且我有一个处理缩放的ScaleGestureDetector.SimpleOnScaleGestureListener。 MovableImageView 扩展了普通的 ImageView 形式 Android。当用户触摸图像视图时,会调用方法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)
}
它基本上创建了拖动阴影并开始拖动操作。
在scaleDetector.onTouchEvent(event)之后调用的onScale()的实现如下:
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在屏幕上的位置和大小。
我的问题是缩放不起作用,只有拖放。不执行缩放,仅在视图中移动。如果我注释掉链接startDragNDrop(),则缩放有效,但在视图中移动显然不行。有没有人更好的想法将这些与图像视图中的东西结合起来?
【问题讨论】:
-
您找到解决方案了吗?我也有类似的问题
-
不幸的是,没有。我最终不得不放弃缩放功能。
标签: android android-layout android-widget