0

ViewPager 内の imageView にピンチ ズーム効果を実装したいと考えています。画像をズームする方法のコードを取得します。しかし、そのコードを ViewPage 内に実装すると、機能しませんでした。TouchImageView Class を使用しましたが、動作しません。私は自分のエラーのためにそれをグーグルで検索しましたが、それは私を満足させませんでした。エラーが表示されて画像が表示されませんでした..

LogCat:

06-06 16:50:44.570: E/AndroidRuntime(30756): FATAL EXCEPTION: main
06-06 16:50:44.570: E/AndroidRuntime(30756): java.lang.NullPointerException
06-06 16:50:44.570: E/AndroidRuntime(30756):  at com.androidbegin.viewpagergridview.ImagePagerAdapter.instantiateItem(ImagePagerAdapter.java:40)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.View.measure(View.java:15504)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.View.measure(View.java:15504)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.View.measure(View.java:15504)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.View.measure(View.java:15504)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2385)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.View.measure(View.java:15504)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1979)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1222)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4526)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.Choreographer.doFrame(Choreographer.java:525)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.os.Handler.handleCallback(Handler.java:615)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.os.Looper.loop(Looper.java:137)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at android.app.ActivityThread.main(ActivityThread.java:4921)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at java.lang.reflect.Method.invokeNative(Native Method)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at java.lang.reflect.Method.invoke(Method.java:511)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
06-06 16:50:44.570: E/AndroidRuntime(30756):  at dalvik.system.NativeStart.main(Native Method)

ImagePagerAdapter.class

public class ImagePagerAdapter extends PagerAdapter {

        private Activity _activity;
         private LayoutInflater inflater;
    private List<ImageView> images;


    public ImagePagerAdapter(Activity activity,
              List<ImageView> images) {
          this._activity = activity;
          this.images = images;
         }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        TouchImageView imgDisplay;
         inflater = (LayoutInflater) _activity
                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View viewLayout = inflater.inflate(R.layout.view_pager, container,
                 false); //Line number 40

         imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);

         ((ViewPager) container).addView(viewLayout);

         return viewLayout;
    }



        @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
       // container.removeView(images.get(position));
        ((ViewPager) container).removeView((RelativeLayout) object);
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }
  }

ImageAdapter.class

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return mThumbIds[position];
    }

    public long getItemId(int position) {
        return 0;
    }

    // Create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // If it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
           // imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // References to our images in res > drawable
    public Integer[] mThumbIds = {
            R.drawable.small, R.drawable.small1,
            R.drawable.small2, R.drawable.small3,
            R.drawable.small4, R.drawable.small5,
            R.drawable.small6, R.drawable.small7,
            R.drawable.small8, R.drawable.small9};
}

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.androidbegin.viewpagergridview.TouchImageView
        android:id="@+id/imgDisplay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitCenter" />


</RelativeLayout>

私を案内してください。私は何を間違えていますか?事前にどうもありがとう..

4

1 に答える 1

1

1.NullPointerException(NPE) が発生するのはなぜですか? インフレータはヌルです。状態をご確認ください

@Override
public Object instantiateItem(ViewGroup container, int position) {

    TouchImageView imgDisplay;
     //Here is the problem i hope
     //  inflater = (LayoutInflater) _activity
     //        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     //try using getContext() instead _activity
       inflater  = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     //check whether inflater is null or not
      if(inflater!=null)
          {
             View viewLayout = inflater.inflate(R.layout.view_pager, container,
             false); //Line number 40

             imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);

            ((ViewPager) container).addView(viewLayout);

          return viewLayout;

           }
         }

2.表示する画像の位置を設定するには?あなたがコメントで議論したように。こちらをご覧ください

Pager には、setOnPageChangeListenerを使用したオーバーライド メソッドがいくつかあります。ページがスクロールされた後、画像の現在の位置を取得できます

   mViewPager.setOnPageChangeListener(new SimpleOnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, 
                                   int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {
                     //check for the state
                    if (state == SCROLL_STATE_SETTLING) {

                           // Here code for the image zooming
                         }

        }
    });

デバッガーを使用して、これらのオーバーライド メソッドが何を行っているかを確認してください。次に、viewpager についていくつかのアイデアを得ることができます。これがあなたに役立つことを願っています

于 2014-06-06T15:29:55.187 に答える