前面一篇高仿launcher和墨跡左右拖動(dòng)效果獲得了很多朋友的好評(píng),上一篇文章 主要是通過自定義ViewGroup實(shí)現(xiàn)的,有點(diǎn)麻煩。今天用ViewPager 這個(gè)類實(shí)現(xiàn)了同樣的效果,這樣代碼更少,但是效果是一樣的。ViewPager是實(shí)現(xiàn)左右兩個(gè)屏幕平滑地切換的一個(gè)類,它是Google提供的。 使用ViewPager首先需要引入android-support-v4.jar這個(gè)jar包。具體ViewPager的用法,這里不做介紹,自己從網(wǎng)上搜索吧! 下面先看一下效果:
效果請(qǐng)自行體驗(yàn)和上一篇比較。下面上代碼: 首先是layout下面的main.xml
android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <>< p=""> <> android:id="@+id/viewPager" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <>< p=""> <> android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <>< p=""> <> android:id="@+id/viewGroup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="30dp" android:gravity="center_horizontal" android:orientation="horizontal" >
復(fù)制代碼 接下來為每一個(gè)切換界面設(shè)置布局item1.xml
android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <>< p=""> <> android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/guide01" >
復(fù)制代碼 其他的幾個(gè)界面布局和這個(gè)一樣 ,就是修改下背景圖片,所以不再?gòu)?fù)述,最后是核心代碼: import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.Window; import android.widget.ImageView; public class MainActivity extends Activity { ViewPager viewPager; ArrayList ViewGroup main, group; ImageView imageView; ImageView[] imageViews; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); LayoutInflater inflater = getLayoutInflater(); list = new ArrayList list.add(inflater.inflate(R.layout.item1, null)); list.add(inflater.inflate(R.layout.item2, null)); list.add(inflater.inflate(R.layout.item3, null)); list.add(inflater.inflate(R.layout.item4, null)); list.add(inflater.inflate(R.layout.item5, null)); imageViews = new ImageView[list.size()]; ViewGroup main = (ViewGroup) inflater.inflate(R.layout.main, null); // group是R.layou.main中的負(fù)責(zé)包裹小圓點(diǎn)的LinearLayout. ViewGroup group = (ViewGroup) main.findViewById(R.id.viewGroup); viewPager = (ViewPager) main.findViewById(R.id.viewPager); for (int i = 0; i < list.size(); i++) { imageView = new ImageView(MainActivity.this); imageView.setLayoutParams(new LayoutParams(10,10)); imageView.setPadding(10, 0, 10, 0); imageViews[i] = imageView; if (i == 0) { // 默認(rèn)進(jìn)入程序后第一張圖片被選中; imageViews[i].setBackgroundResource(R.drawable.guide_dot_white); } else { imageViews[i].setBackgroundResource(R.drawable.guide_dot_black); } group.addView(imageView); } setContentView(main); viewPager.setAdapter(new MyAdapter()); viewPager.setOnPageChangeListener(new MyListener()); } class MyAdapter extends PagerAdapter { @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public int getItemPosition(Object object) { // TODO Auto-generated method stub return super.getItemPosition(object); } @Override public void destroyItem(View arg0, int arg1, Object arg2) { // TODO Auto-generated method stub ((ViewPager) arg0).removeView(list.get(arg1)); } @Override public Object instantiateItem(View arg0, int arg1) { // TODO Auto-generated method stub ((ViewPager) arg0).addView(list.get(arg1)); return list.get(arg1); } @Override public void restoreState(Parcelable arg0, ClassLoader arg1) { // TODO Auto-generated method stub } @Override public Parcelable saveState() { // TODO Auto-generated method stub return null; } @Override public void startUpdate(View arg0) { // TODO Auto-generated method stub } @Override public void finishUpdate(View arg0) { // TODO Auto-generated method stub } } class MyListener implements OnPageChangeListener { @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int arg0) { for (int i = 0; i < imageViews.length; i++) { imageViews[arg0] .setBackgroundResource(R.drawable.guide_dot_white); if (arg0 != i) { imageViews[i] .setBackgroundResource(R.drawable.guide_dot_black); } } } } } 復(fù)制代碼 最后在提醒一句,不要忘記加入android-support-v4.jar這個(gè)jar包。 注意: 源碼在公司,如果大家想要源碼的話,先把這個(gè)教程分享到自己的QQ空間或者微博上,下周一源碼給大家發(fā)上來!! |
|