我想在我的应用程序的同一视图中使用双指缩放和拖放。
我可以单独实现它们但是当我尝试使用它们时它不起作用。
以下是拖动代码
class MyClickListener implements OnLongClickListener {
@Override
public boolean onLongClick(View view) {
ClipData.Item item = new ClipData.Item((CharSequence) view.getTag());
String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData data = new ClipData(view.getTag().toString(), mimeTypes,
item);
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, // data to be dragged
shadowBuilder, // drag shadow
view, // local data about the drag and drop operation
0 // no needed flags
);
return true;
}
}
当我在非自定义视图上使用而无需缩放来缩放时,此方法正常。
当我使用具有scalegesture侦听器的自定义视图,然后将MyClickListener设置为LongClickListener时,我只获得缩放功能,并且不会启动拖动。
我在检测到长按视图
后,尝试将拖动代码直接添加到自定义视图中public class TouchImageView extends View {
long time = 0;
private Drawable image;
private float scaleFactor = 1.0f;
private ScaleGestureDetector scaleGestureDetector;
public TouchImageView(Context context) {
super(context);
}
public TouchImageView(Context context, Drawable mImage) {
super(context);
image = mImage;
setFocusable(true);
image.setBounds(0, 0, image.getIntrinsicWidth(),
image.getIntrinsicHeight());
scaleGestureDetector = new ScaleGestureDetector(context,
new ScaleListener());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Set the image bounderies
canvas.save();
canvas.scale(scaleFactor, scaleFactor);
image.draw(canvas);
canvas.restore();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount() > 1) {
scaleGestureDetector.onTouchEvent(event);
invalidate();
} else {
int action = MotionEventCompat.getActionMasked(event);
switch(action){
case MotionEvent.ACTION_DOWN:
time = SystemClock.elapsedRealtime();
Log.i(EJ.TAG, time+"");
break;
case MotionEvent.ACTION_UP:
long time1 = SystemClock.elapsedRealtime() - time;
if(time1 >= 1000){
ClipData.Item item = new ClipData.Item((CharSequence) this.getTag());
String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData data = new ClipData(this.getTag().toString(), mimeTypes,
item);
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(this);
this.startDrag(data, // data to be dragged
shadowBuilder, // drag shadow
this, // local data about the drag and drop operation
0 // no needed flags
);
//Toast.makeText(context, "Long press", Toast.LENGTH_SHORT).show();
return true;
}
break;
}
}
return true;
}
private class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
scaleFactor *= detector.getScaleFactor();
// don't let the object get too small or too large.
scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 5.0f));
invalidate();
return true;
}
}
}
我不确定这是否是正确的做法。对此有任何帮助表示赞赏