我正在尝试创建一个自定义ImageView,用户可以在其中捏合以调整视图大小,但即使检测器因子始终具有不同的值,该方法也不会执行任何操作。
public class Art extends AppCompatImageView {
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;
public Art(Context context) {
super(context);
mScaleDetector = new ScaleGestureDetector(context , new scaleListner());
}
public Art(Context context, AttributeSet attrs) {
super(context, attrs);
mScaleDetector = new ScaleGestureDetector(context , new scaleListner());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
event.setLocation(event.getRawX(), event.getRawY());
mScaleDetector.onTouchEvent(event);
performClick();
return true;
}
@Override
public boolean performClick() {
return super.performClick();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.scale(mScaleFactor, mScaleFactor);
canvas.restore();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int imageWidth = MeasureSpec.getSize(widthMeasureSpec);
int imageHeight = MeasureSpec.getSize(heightMeasureSpec);
int scaledWidth = Math.round(imageWidth * mScaleFactor);
int scaledHeight = Math.round(imageHeight * mScaleFactor);
setMeasuredDimension(
Math.min(imageWidth , scaledWidth) ,
Math.min(imageHeight , scaledHeight)
);
}
private class scaleListner extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
return super.onScaleBegin(detector);
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
invalidate();
return super.onScale(detector);
}
}
}
并使用添加图像 MainActivity中的setImageResource
任何人都可以帮我解决问题吗?