图片缩放时java.lang.IllegalArgumentException: pointerIndex out of range解决方案

Posted 追梦的Leo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片缩放时java.lang.IllegalArgumentException: pointerIndex out of range解决方案相关的知识,希望对你有一定的参考价值。

06-03 20:45:24.143: E/androidRuntime(1230): FATAL EXCEPTION: main
06-03 20:45:24.143: E/AndroidRuntime(1230): java.lang.IllegalArgumentException: pointerIndex out of range
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.MotionEvent.nativeGetAxisValue(Native Method)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.MotionEvent.getX(MotionEvent.java:1981)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:91)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:219)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1768)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1935)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2289)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2032)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2289)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2032)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2289)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2032)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2289)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2032)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2289)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2032)
06-03 20:45:24.143: E/AndroidRuntime(1230): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2020)
06-03 20:45:24.143: E/AndroidRuntime(1230): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1470)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.app.Activity.dispatchTouchEvent(Activity.java:2471)
06-03 20:45:24.143: E/AndroidRuntime(1230): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1968)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.View.dispatchPointerEvent(View.java:7396)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3251)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3196)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4238)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4217)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4309)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4288)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4328)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.Choreographer.doFrame(Choreographer.java:523)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.os.Handler.handleCallback(Handler.java:615)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.os.Handler.dispatchMessage(Handler.java:92)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.os.Looper.loop(Looper.java:137)
06-03 20:45:24.143: E/AndroidRuntime(1230): at android.app.ActivityThread.main(ActivityThread.java:4803)
06-03 20:45:24.143: E/AndroidRuntime(1230): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 20:45:24.143: E/AndroidRuntime(1230): at java.lang.reflect.Method.invoke(Method.java:511)
06-03 20:45:24.143: E/AndroidRuntime(1230): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-03 20:45:24.143: E/AndroidRuntime(1230): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-03 20:45:24.143: E/AndroidRuntime(1230): at dalvik.system.NativeStart.main(Native Method)
06-03 20:45:26.773: E/Trace(1281): error opening trace file: No such file or directory (2)

一个问题,两天时间,搜破了百度没有啥成效。只有这点东西,到处转的,没有说明怎么用。帮助不大!百度上仅有的点内容如下:

=========================================================================================================

在做多点触控放大缩小,操作自己所绘制的图形时发生这个异常,如果是操作图片的放大缩小多点触控不会出现这个错误
 
这个bug是Android系统原因   所以第一种方式是:
 
修改frameworks\base\core\jni\android_view_MotionEvent.cpp的android_view_MotionEvent_nativeGetAxisValue方法
 
注释掉
[java]
if (!validatePointerIndex(env, pointerIndex, pointerCount)) {return 0;} 
改完后需重新编译整个系统,然后替换lib库,重新编译整个系统一般需要半个多小时,这个方法就比较麻烦了
 
第二种方法是:捕获IllegalArgumentException(非法参数异常)异常 即如
[java] 
private float spacing(MotionEvent event) { 
        try { 
        x = event.getX(0) - event.getX(1); 
        y = event.getY(0) - event.getY(1); 
    } catch (IllegalArgumentException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
    } 
    第二种方法简单有效

========================================================================================================

这种方法我不会用,不知道该用在哪儿。

 

接着用谷歌搜索,顺着终于找到了解决方案。经历了以下网站:

http://stackoverflow.com/questions/6919292/pointerindex-out-of-range-android-multitouch

https://code.google.com/p/android/issues/detail?id=18990

https://github.com/chrisbanes/PhotoView/issues/31

 

上面是搜索的过程,现在把精华内容拣出来:思路是自定义ViewPager,重写onTouchEvent 和onInterceptTouchEvent。具体见下:

 

It‘s the android ViewPager‘s bug
stackoverflow‘s report: http://stackoverflow.com/questions/6919292/pointerindex-out-of-range-android-multitouch
android‘s report: http://code.google.com/p/android/issues/detail?id=18990

My simple‘s method to fix this bug:
You can extends the ViewPager class, your own ViewPager should override the onTouchEvent and the onInterceptTouchEvent methods, and try-catch the IllegalArgumentException exception. Then use your own ViewPager class in layout or others you want.

Examples:

 

[html] view plain copy
 
 技术分享技术分享
  1. /** Custom your own ViewPager to extends support ViewPager. java source: */  
  2. /** Created by azi on 2013-6-21.  */  
  3.   
  4. package com.chaokuadi.android.support.view;  
  5.   
  6. import android.content.Context;  
  7. import android.util.AttributeSet;  
  8. import android.view.MotionEvent;  
  9.   
  10. public class ViewPagerFixed extends android.support.v4.view.ViewPager {  
  11.   
  12.     public ViewPagerFixed(Context context) {  
  13.         super(context);  
  14.     }  
  15.   
  16.     public ViewPagerFixed(Context context, AttributeSet attrs) {  
  17.         super(context, attrs);  
  18.     }  
  19.   
  20.     @Override  
  21.     public boolean onTouchEvent(MotionEvent ev) {  
  22.         try {  
  23.             return super.onTouchEvent(ev);  
  24.         } catch (IllegalArgumentException ex) {  
  25.             ex.printStackTrace();  
  26.         }  
  27.         return false;  
  28.     }  
  29.   
  30.     @Override  
  31.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  32.         try {  
  33.             return super.onInterceptTouchEvent(ev);  
  34.         } catch (IllegalArgumentException ex) {  
  35.             ex.printStackTrace();  
  36.         }  
  37.         return false;  
  38.     }  
  39. }  

[html] view plain copy
 
 技术分享技术分享
  1. /** ViewPager layout */  
  2. <?xml version="1.0" encoding="utf-8"?>  
  3. <com.chaokuaidi.android.support.view.ViewPagerFixed xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@+id/pager"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent" />  

以上是关于图片缩放时java.lang.IllegalArgumentException: pointerIndex out of range解决方案的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Spring Boot 的 YML 文件中创建条件属性?

VB中如何实现图片自动缩放

如何用MATLAB对图片进行缩放?

上传图片时等比缩放的一个小小算法

4.页面查看图片时控制图片的旋转放大和缩放(修订版)

ueditor 图片选区错位问题,图片无法正常缩放