TouchImageView 不适用于 ViewPager
Posted
技术标签:
【中文标题】TouchImageView 不适用于 ViewPager【英文标题】:TouchImageView is not working for ViewPager 【发布时间】:2014-07-27 15:56:45 【问题描述】:我想在 ViewPager 中的 imageView 上实现捏缩放效果。我得到了如何缩放图像的代码。但是当我在 ViewPage 中实现该代码时它不起作用。我用过 TouchImageView 类它不起作用。我已经用谷歌搜索了我的错误,它没有让我满意。我看不到我的图像显示错误..
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_
android:layout_ >
<com.androidbegin.viewpagergridview.TouchImageView
android:id="@+id/imgDisplay"
android:layout_
android:layout_
android:scaleType="fitCenter" />
</RelativeLayout>
请指导我。我在做什么错误? 提前非常感谢..
【问题讨论】:
指出行号40
@MIS 检查您的PKG PATH
TouchImageView
所属的位置。
@Aniruddha 查看我对第 40 行的编辑
@SimplePlan 什么 PKG?我的包名在那个类中都正确
尝试将View viewLayout = inflater.inflate(R.layout.view_pager, container, false);
更改为View viewLayout = inflater.inflate(R.layout.view_pager, null);
【参考方案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.如何设置图片位置显示?正如您在 cmets 中讨论的那样。看看这里
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有所了解。希望对你有帮助
【讨论】:
以上是关于TouchImageView 不适用于 ViewPager的主要内容,如果未能解决你的问题,请参考以下文章
Tensorflow - 多 GPU 不适用于模型(输入),也不适用于计算梯度