android-canvas.scale(scale,scale,px,py)跳到新位置

android-canvas.scale(scale,scale,px,py)跳到新位置,第1张

概述我正在尝试在视图中同时使用scalegesture侦听器和手势侦听器.如果比例尺为1,则一切工作正常,但如果将比例尺设置为其他值,则图像将移至新位置,然后平滑缩放.我的部分代码如下所示:publicclassSimpleViewextendsView{Bitmapimage;ScaleGestureDetectorscaleGesture;Ges

我正在尝试在视图中同时使用scalegesture侦听器和手势侦听器.如果比例尺为1,则一切工作正常,但如果将比例尺设置为其他值,则图像将移至新位置,然后平滑缩放.

我的部分代码如下所示:

public class SimpleVIEw extends VIEw {Bitmap image;ScaleGestureDetector scaleGesture;GestureDetector gestures;float touchX, touchY;float horizontalOffset;float verticalOffset;float scale;public SimpleVIEw(Context context) {    super(context);    image = BitmapFactory.decodeResource(getResources(), R.drawable.some_image);    scaleGesture = new ScaleGestureDetector(getContext(),            new ScaleListener());    gestures = new GestureDetector(getContext(), new GestureListener(),            null, true);    scale = 0.6f; //if this is set to  1.0f everything works perfectly,            // else the imagetransports to a new position            // and scales perfectly thereafter}@OverrIDeprotected voID onDraw(Canvas canvas) {    canvas.save();    canvas.translate(horizontalOffset, verticalOffset);    canvas.scale(scale, scale, touchX, touchY);    canvas.drawBitmap(image, getMatrix(), new Paint);    canvas.restore();}public class ScaleListener implements OnScaleGestureListener {    @OverrIDe    public boolean onScale(ScaleGestureDetector detector) {        float oldScale = scale;        scale *= detector.getScaleFactor();        scale = Math.max(0.5f, Math.min(scale, 5.0f));        if (scale != oldScale) {            touchX = detector.getFocusX();            touchY = detector.getFocusY();            invalIDate(0, 0, screenWIDth, screenHeight);        }         return true;    }}public class GestureListener implements OnGestureListener {    @OverrIDe    public boolean onScroll(MotionEvent e1, MotionEvent e2,            float distanceX, float distanceY) {                    //Helps in drag        if (!scaleGesture.isInProgress()) {            horizontalOffset -= distanceX;            verticalOffset -= distanceY;            invalIDate();            return true;        }        return false;    }}@OverrIDepublic boolean ontouchEvent(MotionEvent event) {    boolean result = scaleGesture.ontouchEvent(event);    boolean isScaling = result = scaleGesture.isInProgress();    if (!isScaling) {        if (!(event.getPointerCount() > 1)) {            result = gestures.ontouchEvent(event);        } else            result = false;    }    return result;}}

如果我将scale的值设置为1.0f,则可以顺利进行.当我将scale的值更改为(例如)0.6f时,它将传输到新位置,然后完美工作.

解决方法:

请查看@ToreRudberg的答案here-您可能希望对代码进行修改以更正所观察到的行为.

总结

以上是内存溢出为你收集整理的android-canvas.scale(scale,scale,px,py)跳到新位置全部内容,希望文章能够帮你解决android-canvas.scale(scale,scale,px,py)跳到新位置所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1211270.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存