我想在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)
ImagePagerAdapater.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>
请指导我。我做错了什么? 非常感谢提前..
答案 0 :(得分:1)
1.为什么你得到NullPointerException(NPE)? Inflater为空。请检查条件
@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有所了解。希望这对你有帮助