viewpager滑動加標記動畫滑動效果

private ViewPager mPager;// 頁卡內容
private List<View> listViews; // Tab頁面列表
private ImageView cursor;// 動畫圖片
private int offset = 0;// 動畫圖片偏移量
private int currIndex = 0;// 當前頁卡編號
private int bmpW;// 動畫圖片寬度

/**
* 初始化ViewPager
*/
private void InitViewPager() {
mPager = (ViewPager) findViewById(R.id.vPager);
listViews = new ArrayList<View>();
LayoutInflater mInflater = getLayoutInflater();
for(int i = 0; i < layout.length; i++){
listViews.add(mInflater.inflate(layout[i], null));
}
// listViews.add(mInflater.inflate(R.layout.lay1, null));
// listViews.add(mInflater.inflate(R.layout.lay1, null));
mPager.setAdapter(new MyPagerAdapter(listViews));
mPager.setCurrentItem(0);
mPager.setOnPageChangeListener(new MyOnPageChangeListener());
}

/**
* 初始化動畫
*/
private void InitImageView() {
cursor = (ImageView) findViewById(R.id.cursor);
bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.frame_bar)
.getWidth();// 獲取圖片寬度
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenW = dm.widthPixels;// 獲取分辨率寬度
offset = (screenW / 5 - bmpW) / 2;// 計算偏移量:屏幕寬度/5,平分爲5分,如果是5個view的話,再減去圖片寬度,因爲圖片居中,所以要得到兩變剩下的空隙需要再除以2


Log.v("screenW / 5", "screenW / 5 : "+(screenW / 5));
Log.v("bmpW", "bmpW : "+bmpW);
Log.v("偏移量", "offset : "+offset);
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);//初始化位置,在最左邊
cursor.setImageMatrix(matrix);// 設置動畫初始位置
}


/**
* ViewPager適配器
*/
public class MyPagerAdapter extends PagerAdapter {
public List<View> mListViews;


public MyPagerAdapter(List<View> mListViews) {
this.mListViews = mListViews;
}


@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView(mListViews.get(arg1));
}


@Override
public void finishUpdate(View arg0) {
}


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


@Override
public Object instantiateItem(View arg0, int arg1) {
((ViewPager) arg0).addView(mListViews.get(arg1), 0);


tvDetail = (TextView) arg0.findViewById(R.id.tt);
tvDetail.setText(Html.fromHtml(texts[arg1]));
return mListViews.get(arg1);
}


@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == (arg1);
}


@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}


@Override
public Parcelable saveState() {
return null;
}


@Override
public void startUpdate(View arg0) {
}
}


/**
* 頁卡切換監聽
*/
public class MyOnPageChangeListener implements OnPageChangeListener {


int one = offset * 2 + bmpW;// 頁卡1 -> 頁卡2 偏移量--移動1個頁卡位置
int two = one * 2;// 頁卡1 -> 頁卡3 偏移量--移動2個頁卡位置
int three = one * 3;// 頁卡1 -> 頁卡4 偏移量--移動3個頁卡位置
int four = one * 4;// 頁卡1 -> 頁卡5 偏移量--移動4個頁卡位置


@Override
public void onPageSelected(int arg0) {
Animation animation = null;
switch (arg0) {
case 0:// 切換到頁卡1 
if (currIndex == 1) {
animation = new TranslateAnimation(one, 0, 0, 0);
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, 0, 0, 0);
} else if (currIndex == 3) {
animation = new TranslateAnimation(three, 0, 0, 0);
} else if (currIndex == 4) {
animation = new TranslateAnimation(four, 0, 0, 0);
}
break;
case 1:// 切換到頁卡2
if (currIndex == 0) {
animation = new TranslateAnimation(0, one, 0, 0);
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, one, 0, 0);
} else if (currIndex == 3) {
animation = new TranslateAnimation(three, one, 0, 0);
} else if (currIndex == 4) {
animation = new TranslateAnimation(four, one, 0, 0);
}
break;
case 2:// 切換到頁卡3
if (currIndex == 0) {
animation = new TranslateAnimation(offset, two, 0, 0);
} else if (currIndex == 1) {
animation = new TranslateAnimation(one, two, 0, 0);
} else if (currIndex == 3) {
animation = new TranslateAnimation(three, two, 0, 0);
} else if (currIndex == 4) {
animation = new TranslateAnimation(four, two, 0, 0);
}
break;
case 3:// 切換到頁卡4
if (currIndex == 0) {
animation = new TranslateAnimation(offset, three, 0, 0);
} else if (currIndex == 1) {
animation = new TranslateAnimation(one, three, 0, 0);
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, three, 0, 0);
} else if (currIndex == 4) {
animation = new TranslateAnimation(four, three, 0, 0);
}
break;
case 4:// 切換到頁卡5
if (currIndex == 0) {
animation = new TranslateAnimation(offset, four, 0, 0);
} else if (currIndex == 1) {
animation = new TranslateAnimation(one, four, 0, 0);
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, four, 0, 0);
} else if (currIndex == 3) {
animation = new TranslateAnimation(three, four, 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) {
}
}


/**
* 頭標點擊監聽
*/
public class MyOnClickListener implements View.OnClickListener {
private int index = 0;


public MyOnClickListener(int i) {
index = i;
}


@Override
public void onClick(View v) {
mPager.setCurrentItem(index);
}
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章