为什么在双指缩放手势导致闪烁调用setScaleX? [英] Why does calling setScaleX during pinch zoom gesture cause flicker?

查看:939
本文介绍了为什么在双指缩放手势导致闪烁调用setScaleX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可缩放的容器,我针对API 14 +

I am trying to create a zoomable container and I am targeting API 14+

在我onScale(我使用的ScaleGestureDetector检测捏缩放)我做这样的事情:

In my onScale (i am using the ScaleGestureDetector to detect pinch-zoom) I am doing something like this:

public boolean onScale (ScaleGestureDetector detector) {
   float scaleFactor = detector.getScaleFactor();
   setScaleX(getScaleX() * scaleFactor);
   setScaleY(getScaleY() * scaleFactor);

   return true;
};

它的工作原理,但变焦并不顺利。事实上,它明显地闪烁。

It works but the zoom is not smooth. In fact it noticeably flickers.

我也试图与硬件层认为该比例将发生在GPU上一次纹理上传,因此是超级快。但它并没有区别 - 变焦不光滑,闪烁和古怪,有时

I also tried it with hardware layer thinking that the scaling would happen on the GPU once the texture was uploaded and thus would be super fast. But it made no difference - the zoom is not smooth and flickers weirdly sometimes.

我是什么做错了吗?

推荐答案

我面临同样的问题。现在的任务是:

I faced the same problem. The task was:

  • 创建画布(在视图或SurfaceView的子类,无所谓)
  • 在画它的一些图片和图形基元(与方法drawBitmap()的drawLine()...)
  • 请帆布滚动和scallable

我想,没有必要显示所有的类code在这里。

I suppose, no need to show all the class code here.

的问题与scalling动作期间闪烁解决的办法很简单。 只要把缩放程序写入 onScaleEnd()方式。

The solution of the problem with flickering during scalling gesture was very simple. Just put the scaling procedure into the onScaleEnd() method.

private class MyScaleGestureListener implements OnScaleGestureListener
{
    public boolean onScale(ScaleGestureDetector detector)
    {   
        scaleFactor *= detector.getScaleFactor();  // class variable of type float
        if (scaleFactor > 5) scaleFactor = 5;      // some limitations
        if (scaleFactor < 1) scaleFactor = 1;
        return true;
    }
    public boolean onScaleBegin(ScaleGestureDetector detector)
    { return true;}

    public void onScaleEnd(ScaleGestureDetector detector) 
     {
       setScaleX(scaleFactor); setScaleY(scaleFactor); 
       invalidate();     // it seems to me - no effect
     }
} 

测试在真实的设备三星GALAXY S III迷你。

Tested on real device Samsung GALAXY S III mini.

这篇关于为什么在双指缩放手势导致闪烁调用setScaleX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆