我使用的是PhotoView库,但在将其与viewpager一起使用时,我在缩放时出错。
08-22 17:14:50.096: E/AndroidRuntime(8740): FATAL EXCEPTION: main
08-22 17:14:50.096: E/AndroidRuntime(8740): java.lang.IllegalArgumentException: pointerIndex out of range
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.MotionEvent.nativeGetAxisValue(Native Method)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.MotionEvent.getX(MotionEvent.java:2148)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:91)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:219)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1834)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1909)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2270)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2010)
08-22 17:14:50.096: E/AndroidRuntime(8740): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2182)
08-22 17:14:50.096: E/AndroidRuntime(8740): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1512)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.app.Activity.dispatchTouchEvent(Activity.java:2466)
08-22 17:14:50.096: E/AndroidRuntime(8740): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2130)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.View.dispatchPointerEvent(View.java:7641)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3682)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3613)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4804)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4783)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4875)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:174)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4854)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4894)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.Choreographer.doFrame(Choreographer.java:530)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.os.Handler.handleCallback(Handler.java:725)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.os.Looper.loop(Looper.java:137)
08-22 17:14:50.096: E/AndroidRuntime(8740): at android.app.ActivityThread.main(ActivityThread.java:5328)
08-22 17:14:50.096: E/AndroidRuntime(8740): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 17:14:50.096: E/AndroidRuntime(8740): at java.lang.reflect.Method.invoke(Method.java:511)
08-22 17:14:50.096: E/AndroidRuntime(8740): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-22 17:14:50.096: E/AndroidRuntime(8740): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-22 17:14:50.096: E/AndroidRuntime(8740): at dalvik.system.NativeStart.main(Native Method)
08-22 17:15:18.416: D/dalvikvm(8872): GC_FOR_ALLOC freed 94K, 14% free 17782K/20656K, paused 13ms, total 13ms
虽然缩放效果很好,但当图像变远时,就会出现错误,我的活动就会崩溃。。以下是我在viewapager中的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
pv = new PhotoView(getActivity());
pv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
imageLoader.DisplayImage(mContent, pv);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
layout.setGravity(Gravity.CENTER);
layout.addView(pv);
return layout;
}
我在谷歌上搜索了这个问题,但找不到任何合适的解决方案!有什么建议吗??谢谢各位
我的全部代码:
public class viewpager extends SherlockFragmentActivity {
AllPhonesDatabase apd;
Bundle gotBasket;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.simple_circles);
apd = new AllPhonesDatabase(this.getApplicationContext()).open();
gotBasket = viewpager.this.getIntent().getExtras();
final String imageurl1 = gotBasket.getString("Imageurl1");
final String imageurl2 = gotBasket.getString("Imageurl2");
final String imageurl3 = gotBasket.getString("Imageurl3");
TestFragmentAdapter mAdapter = new TestFragmentAdapter(
this.getSupportFragmentManager(), imageurl1, imageurl2,
imageurl3);
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
CirclePageIndicator mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
}
class TestFragmentAdapter extends FragmentPagerAdapter {
protected static String[] CONTENT;
private int mCount;
public TestFragmentAdapter(FragmentManager fm, String imageurl1,
String imageurl2, String imageurl3) {
super(fm);
CONTENT = new String[] { imageurl1, imageurl2, imageurl3 };
mCount = CONTENT.length;
}
@Override
public Fragment getItem(int position) {
return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
return TestFragmentAdapter.CONTENT[position % CONTENT.length];
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
public final class TestFragment extends Fragment {
private static final String KEY_CONTENT = "TestFragment:Content";
public ImageLoader2 imageLoader;
PhotoView pv;
public static TestFragment newInstance(String content) {
TestFragment fragment = new TestFragment();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 1; i++) {
builder.append(content).append(" ");
}
builder.deleteCharAt(builder.length() - 1);
fragment.mContent = builder.toString();
return fragment;
}
private String mContent = "???";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imageLoader = new com.androidarabia.lazylist.ImageLoader2(
this.getActivity());
if ((savedInstanceState != null)
&& savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
pv = new PhotoView(getActivity());
pv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
imageLoader.DisplayImage(mContent, pv);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
layout.setGravity(Gravity.CENTER);
layout.addView(pv);
return layout;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
这似乎是一个bug。我不确定它是在PhotoViewer端还是在机器人端。我通过在自己的自定义视图寻呼机中使用以下代码解决了这个问题:
public class HackyViewPager extends ViewPager {
public HackyViewPager(Context context)
{
super(context);
}
public HackyViewPager(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
try
{
return super.onInterceptTouchEvent(ev);
}
catch (IllegalArgumentException e)
{
}
catch (ArrayIndexOutOfBoundsException e)
{
}
return false;
}
}
我在使用这段代码时没有遇到任何问题,放大和缩小似乎都很好。在源代码中创建这个类,然后可以在xml文件中引用它,如下所示:
<my.default.package.HackyViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
您可能应该检查这是否与前面的问题类似。java.lang.IollegalArgumentException:pointerIndex超出范围异常-dispatchTouchEvent
显然我同意hacky view pager的事情,但我想为了完美的实现,必须使用photoViewAttacher.update(),在每次图像资源更改后,就像在成功、失败中一样,相应图像加载库的侦听器使用
-
创建一个新类
HackyViewPager
并粘贴此代码public class HackyViewPager extends ViewPager { public HackyViewPager(Context context) { super(context); } public HackyViewPager(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { try { return super.onInterceptTouchEvent(ev); } catch (IllegalArgumentException e) { } catch (ArrayIndexOutOfBoundsException e) { } return false; } }
-
您的xml布局更改了viewPager组件的类
<android.support.v4.view.ViewPager android:layout_width="match_parent" android:elevation="0dp" android:id="@+id/viewPager" android:layout_height="match_parent"> </android.support.v4.view.ViewPager>
至
<com.yourpackage.HackyViewPager android:layout_width="match_parent" android:elevation="0dp" android:id="@+id/viewPager" android:layout_height="match_parent"> </com.yourpackage.HackyViewPager>
-
用类HackyViewPager声明您的viewPager变量
HackyViewPager viewPager;
内部onCreate方法
viewPager = (HackyViewPager) findViewById(R.id.viewPager);