001
/**
002
* Tab页面手势滑动切换以及动画效果
003
*
004
* <span>@author</span> D.Winter
005
*
006
*/
007
public class dd extends Activity {
008
// ViewPager是google SDk中自带的一个附加包的一个类,可以用来实现屏幕间的切换。
009
// android-support-v4.jar
010
private ViewPager mPager;//页卡内容
011
private List<View> listViews; // Tab页面列表
012
private ImageView cursor;// 动画图片
013
private TextView t1, t2, t3;// 页卡头标
014
private int offset = 0;// 动画图片偏移量
015
private int currIndex = 0;// 当前页卡编号
016
private int bmpW;// 动画图片宽度
017
018
@Override
019
public void onCreate(Bundle savedInstanceState) {
020
super.onCreate(savedInstanceState);
021
setContentView(R.layout.main);
022
InitImageView();
023
InitTextView();
024
InitViewPager();
025
}
026
027
/**
028
* 初始化头标
029
*/
030
private void InitTextView() {
031
t1 = (TextView) findViewById(R.id.text1);
032
t2 = (TextView) findViewById(R.id.text2);
033
t3 = (TextView) findViewById(R.id.text3);
034
035
t1.setOnClickListener(new MyOnClickListener(0));
036
t2.setOnClickListener(new MyOnClickListener(1));
037
t3.setOnClickListener(new MyOnClickListener(2));
038
}
039
040
/**
041
* 初始化ViewPager
042
*/
043
private void InitViewPager() {
044
mPager = (ViewPager) findViewById(R.id.vPager);
045
listViews = new ArrayList<View>();
046
LayoutInflater mInflater = getLayoutInflater();
047
listViews.add(mInflater.inflate(R.layout.lay1, null));
048
listViews.add(mInflater.inflate(R.layout.lay2, null));
049
listViews.add(mInflater.inflate(R.layout.lay3, null));
050
mPager.setAdapter(new MyPagerAdapter(listViews));
051
mPager.setCurrentItem(0);
052
mPager.setOnPageChangeListener(new MyOnPageChangeListener());
053
}
054
055
/**
056
* 初始化动画
057
*/
058
private void InitImageView() {
059
cursor = (ImageView) findViewById(R.id.cursor);
060
bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a)
061
.getWidth();// 获取图片宽度
062
DisplayMetrics dm = new DisplayMetrics();
063
getWindowManager().getDefaultDisplay().getMetrics(dm);
064
int screenW = dm.widthPixels;// 获取分辨率宽度
065
offset = (screenW / 3 - bmpW) / 2;// 计算偏移量
066
Matrix matrix = new Matrix();
067
matrix.postTranslate(offset, 0);
068
cursor.setImageMatrix(matrix);// 设置动画初始位置
069
}
070
071
/**
072
* ViewPager适配器
073
*/
074
public class MyPagerAdapter extends PagerAdapter {
075
public List<View> mListViews;
076
077
public MyPagerAdapter(List<View> mListViews) {
078
this.mListViews = mListViews;
079
}
080
081
@Override
082
public void destroyItem(View arg0, int arg1, Object arg2) {
083
((ViewPager) arg0).removeView(mListViews.get(arg1));
084
}
085
086
@Override
087
public void finishUpdate(View arg0) {
088
}
089
090
@Override
091
public int getCount() {
092
return mListViews.size();
093
}
094
095
@Override
096
public Object instantiateItem(View arg0, int arg1) {
097
((ViewPager) arg0).addView(mListViews.get(arg1), 0);
098
return mListViews.get(arg1);
099
}
100
101
@Override
102
public boolean isViewFromObject(View arg0, Object arg1) {
103
return arg0 == (arg1);
104
}
105
106
@Override
107
public void restoreState(Parcelable arg0, ClassLoader arg1) {
108
}
109
110
@Override
111
public Parcelable saveState() {
112
return null;
113
}
114
115
@Override
116
public void startUpdate(View arg0) {
117
}
118
}
119
120
/**
121
* 头标点击监听
122
*/
123
public class MyOnClickListener implements View.OnClickListener {
124
private int index = 0;
125
126
public MyOnClickListener(int i) {
127
index = i;
128
}
129
130
@Override
131
public void onClick(View v) {
132
mPager.setCurrentItem(index);
133
}
134
};
135
136
/**
137
* 页卡切换监听
138
*/
139
public class MyOnPageChangeListener implements OnPageChangeListener {
140
141
int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
142
int two = one * 2;// 页卡1 -> 页卡3 偏移量
143
144
@Override
145
public void onPageSelected(int arg0) {
146
Animation animation = null;
147
switch (arg0) {
148
case 0:
149
if (currIndex == 1) {
150
animation = new TranslateAnimation(one, 0, 0, 0);
151
} else if (currIndex == 2) {
152
animation = new TranslateAnimation(two, 0, 0, 0);
153
}
154
break;
155
case 1:
156
if (currIndex == 0) {
157
animation = new TranslateAnimation(offset, one, 0, 0);
158
} else if (currIndex == 2) {
159
animation = new TranslateAnimation(two, one, 0, 0);
160
}
161
break;
162
case 2:
163
if (currIndex == 0) {
164
animation = new TranslateAnimation(offset, two, 0, 0);
165
} else if (currIndex == 1) {
166
animation = new TranslateAnimation(one, two, 0, 0);
167
}
168
break;
169
}
170
currIndex = arg0;
171
animation.setFillAfter(true);// True:图片停在动画结束位置
172
animation.setDuration(300);
173
cursor.startAnimation(animation);
174
}
175
176
@Override
177
public void onPageScrolled(int arg0, float arg1, int arg2) {
178
}
179
180
@Override
181
public void onPageScrollStateChanged(int arg0) {
182
}
183
}
184
}