布局文件:主要看ImageView的位置:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/product_title_layout" android:layout_width="match_parent" android:layout_height="40.0dip" android:background="#FFFFFF" > <TextView android:id="@+id/product_title_detail_txtv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="图文详情" style="@style/textStyle.Small.black" /> <TextView android:id="@+id/product_title_comment_txtv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="评论" style="@style/textStyle.Small.black" /> </LinearLayout> <ImageView android:id="@+id/cursor" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="matrix" android:src="@drawable/icon_move_line" /> <android.support.v4.view.ViewPager android:id="@+id/product_comment_vpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1.0" android:background="#000000" android:flipInterval="30" android:persistentDrawingCache="animation" /> </LinearLayout>代码:private ImageView cursor; private int bmpw = 0; // 游标宽度 private int offset = 0;// // 动画图片偏移量 private int currIndex = 0;// 当前页卡编号ArrayList<Fragment> listFragment = new ArrayList<Fragment>();cursor = (ImageView) view.findViewById(R.id.cursor);mViewPager.setOffscreenPageLimit(1); mViewPager.setAdapter(new MyFragmentPagerAdapter(fragmentManager, listFragment));//适配器 mViewPager.setOnPageChangeListener(new MyPageChangeListener());
/** * 初始化指示器位置 */ public void initCursorPos() { // 初始化动画 bmpw = BitmapFactory.decodeResource(getResources(), R.drawable.img_line_red).getWidth();// 获取图片宽度 DisplayMetrics dm = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); int screenW = dm.widthPixels;// 获取分辨率宽度 offset = (screenW / 2 - bmpw) / 2;// 计算偏移量 Matrix matrix = new Matrix(); matrix.postTranslate(offset, 0); cursor.setImageMatrix(matrix);// 设置动画初始位置 }
//页面改变监听器 public class MyPageChangeListener implements ViewPager.OnPageChangeListener { int one = offset * 2 + bmpw;// 页卡1 -> 页卡2 偏移量 int two = one * 2;// 页卡1 -> 页卡3 偏移量 @Override public void onPageSelected(int arg0) { Animation animation = null; switch (arg0) { case 0: fragmentFlag=0; textTypeOne.setTextColor(getResources().getColor(R.color.color_FA4148));//红 textTypeTwo.setTextColor(getResources().getColor(R.color.color_7e7e7e));//灰 if (currIndex == 1) { animation = new TranslateAnimation(one, 0, 0, 0); } else if (currIndex == 2) { animation = new TranslateAnimation(two, 0, 0, 0); } break; case 1: fragmentFlag=1; textTypeOne.setTextColor(getResources().getColor(R.color.color_7e7e7e)); textTypeTwo.setTextColor(getResources().getColor(R.color.color_FA4148)); if (currIndex == 0) { animation = new TranslateAnimation(offset, one, 0, 0); } else if (currIndex == 2) { animation = new TranslateAnimation(two, one, 0, 0); } break; case 2: if (currIndex == 0) { animation = new TranslateAnimation(offset, two, 0, 0); } else if (currIndex == 1) { animation = new TranslateAnimation(one, two, 0, 0); } break; } currIndex = arg0; animation.setFillAfter(true);// True:图片停在动画结束位置 animation.setDuration(300); cursor.startAnimation(animation); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }
/** * ViewPager适配器 */ public class MyFragmentPagerAdapter extends FragmentPagerAdapter { ArrayList<Fragment> list; public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) { super(fm); this.list = list; } @Override public int getCount() { return list.size(); } @Override public Fragment getItem(int arg0) { return list.get(arg0); } }
2870

被折叠的 条评论
为什么被折叠?



