在Android中使用MikeOrtiz TouchImageView无法有效执行ZoomIn和Zoomout?

时间:2014-02-03 05:39:32

标签: java android zooming touchimageview

我是android的新手。这是SO Scrolling and zoomin and Zoomout Issue中已经问过的问题 现在这个问题已经解决。这是文件Latest Version with zoom and scrolling的最新版本,但每次我收到此错误

 02-03 21:21:08.030: E/AndroidRuntime(17110): java.lang.UnsupportedOperationException: Scale must be greater than minScale and less than maxScale

每当我试图用手指触摸图像时,它会抛出一个异常而我的应用程序崩溃。当试图连续点击zoomin和zoomout但是我希望这个图像的行为放大和缩小就像webview.i一样dnt想要在这个功能中设置任何限制。我可以解决touch和iamgeScale设置的这个错误。请帮助我。

MainActivity.java:

 public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                  setContentView(R.layout.newxml);
        ImageView zoonIn = (ImageView)findViewById(R.id.btn_zoomIn1);
        ImageView zoonOut = (ImageView)findViewById(R.id.btn_zoomout1);

        final TouchImageView1 touch = (TouchImageView1)findViewById(R.id.imagezoom);

        Bitmap bImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.app_icon);

        touch.setImageBitmap(bImage);
        touch.setMinZoom(0.5f);
        touch.setMaxZoom(5f);

        zoonIn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 float scale = touch.getCurrentZoom();
                 PointF centerPoint = touch.getCenterOfZoomedImage();
                Number zoomedValue =scale*1.25;
                        float value = zoomedValue.floatValue();
                touch.setZoom(value,centerPoint.x,centerPoint.y);
            }
        });


        zoonOut.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 float scale = touch.getCurrentZoom();
                 PointF centerPoint = touch.getCenterOfZoomedImage();
                    Number zoomedValue =scale/1.25;
                       float value = zoomedValue.floatValue();
                    touch.setZoom(value,centerPoint.x,centerPoint.y);
                }

        });

    }
}

Logcat错误:

  

02-03 21:21:08.030:E / AndroidRuntime(17110):致命异常:主要   02-03 21:21:08.030:E / AndroidRuntime(17110):java.lang.UnsupportedOperationException:Scale必须大于minScale且小于maxScale   02-03 21:21:08.030:E / AndroidRuntime(17110):at com.example.imagetouchview.TouchImageView1.setZoom(TouchImageView1.java:369)   02-03 21:21:08.030:E / AndroidRuntime(17110):at com.example.imagetouchview.TouchImageView1.setZoom(TouchImageView1.java:354)   02-03 21:21:08.030:E / AndroidRuntime(17110):at com.example.imagetouchview.MainActivity $ 2.onClick(MainActivity.java:71)   02-03 21:21:08.030:E / AndroidRuntime(17110):在android.view.View.performClick(View.java:4211)   02-03 21:21:08.030:E / AndroidRuntime(17110):在android.view.View $ PerformClick.run(View.java:17267)   02-03 21:21:08.030:E / AndroidRuntime(17110):在android.os.Handler.handleCallback(Handler.java:615)   02-03 21:21:08.030:E / AndroidRuntime(17110):在android.os.Handler.dispatchMessage(Handler.java:92)   02-03 21:21:08.030:E / AndroidRuntime(17110):在android.os.Looper.loop(Looper.java:137)   02-03 21:21:08.030:E / AndroidRuntime(17110):在android.app.ActivityThread.main(ActivityThread.java:4898)   02-03 21:21:08.030:E / AndroidRuntime(17110):at java.lang.reflect.Method.invokeNative(Native Method)   02-03 21:21:08.030:E / AndroidRuntime(17110):at java.lang.reflect.Method.invoke(Method.java:511)   02-03 21:21:08.030:E / AndroidRuntime(17110):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1006)   02-03 21:21:08.030:E / AndroidRuntime(17110):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)   02-03 21:21:08.030:E / AndroidRuntime(17110):at dalvik.system.NativeStart.main(Native Method)   02-03 21:21:19.400:I / Process(17110):发送信号。 PID:17110 SIG:9

1 个答案:

答案 0 :(得分:0)

现在很明显:您的缩放值大于您设置的maxZoom 5.9604645。
尝试将maxZoom设置为6f。在设置最小/最大缩放值之前,您需要正确考虑您的用例 还需要考虑不同的行为及其相应的值:

float scale = touch.getCurrentZoom();
PointF centerPoint = touch.getCenterOfZoomedImage();

我建议您仔细阅读适合您要求的内容并查看: https://github.com/chrisbanes/PhotoView
我已经使用过它,它的库类也很简单透明。