我有一个相对布局,我在动态添加多个ImageView(用户从图库中选择图像),我想缩放移动和缩放用户正在执行缩放移动和缩放的特定ImageView。其他ImageView应该像布局一样。 我创建了ZoomableImageView类,但是当我触摸ImageView时,未检测到事件。
ZoomableImageView.java
public class ZoomableImageView extends ImageView {
private static final String TAG = "zoomableimageview";
private float mScaleFactor = 1.f;
private ScaleGestureDetector mScaleDetector;
public ZoomableImageView(Context context) {
this(context, null);
}
public ZoomableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
Toast.makeText(context, "View Created", 1000).show();
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Toast.makeText(getContext(), "ondraw", 1000).show();
canvas.save();
canvas.scale(mScaleFactor, mScaleFactor);
canvas.restore();
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
Toast.makeText(getContext(), "Detector", 1000).show();
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
ZoomableImageView.this.invalidate();
return true;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
mScaleDetector.onTouchEvent(event);
return true;
}
}