有人使用下面這樣的方法來仿微博的效果,java
,android
這個標題就固定了,並且能夠左右滑動,也有下面的橫線,來指示頁卡。方法和上面的差很少,區別在於這個橫線須要咱們本身來作,其實就是一個圖片。這個例子是網上的一篇文章,看代碼:app
主Activity:ide
package com.example.viewpagerdemo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class WeiBoActivity extends Activity { private ViewPager viewPager;//頁卡內容 private ImageView imageView;// 動畫圖片 private TextView textView1,textView2,textView3; private List<View> views;// Tab頁面列表 private int offset = 0;// 動畫圖片偏移量 private int currIndex = 0;// 當前頁卡編號 private int bmpW;// 動畫圖片寬度 private View view1,view2,view3;//各個頁卡 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.weibo); InitImageView(); InitTextView(); InitViewPager(); } private void InitViewPager() { viewPager=(ViewPager) findViewById(R.id.vPager); views=new ArrayList<View>(); LayoutInflater inflater=getLayoutInflater(); view1=inflater.inflate(R.layout.lay1, null); view2=inflater.inflate(R.layout.lay2, null); view3=inflater.inflate(R.layout.lay3, null); views.add(view1); views.add(view2); views.add(view3); viewPager.setAdapter(new MyViewPagerAdapter(views)); viewPager.setCurrentItem(0); viewPager.setOnPageChangeListener(new MyOnPageChangeListener()); } /** * 初始化頭標 */ private void InitTextView() { textView1 = (TextView) findViewById(R.id.text1); textView2 = (TextView) findViewById(R.id.text2); textView3 = (TextView) findViewById(R.id.text3); textView1.setOnClickListener(new MyOnClickListener(0)); textView2.setOnClickListener(new MyOnClickListener(1)); textView3.setOnClickListener(new MyOnClickListener(2)); } /** 2 * 初始化動畫,這個就是頁卡滑動時,下面的橫線也滑動的效果,在這裏須要計算一些數據 3 */ private void InitImageView() { imageView= (ImageView) findViewById(R.id.cursor); bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a).getWidth();// 獲取圖片寬度 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int screenW = dm.widthPixels;// 獲取分辨率寬度 offset = (screenW / 3 - bmpW) / 2;// 計算偏移量 Matrix matrix = new Matrix(); matrix.postTranslate(offset, 0); imageView.setImageMatrix(matrix);// 設置動畫初始位置 } <img src="http://img.my.csdn.net/uploads/201211/10/1352554452_1685.jpg" alt=""> /** * * 頭標點擊監聽 3 */ private class MyOnClickListener implements OnClickListener{ private int index=0; public MyOnClickListener(int i){ index=i; } public void onClick(View v) { viewPager.setCurrentItem(index); } } public class MyViewPagerAdapter extends PagerAdapter{ private List<View> mListViews; public MyViewPagerAdapter(List<View> mListViews) { this.mListViews = mListViews; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(mListViews.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(mListViews.get(position), 0); return mListViews.get(position); } @Override public int getCount() { return mListViews.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; } } public class MyOnPageChangeListener implements OnPageChangeListener{ int one = offset * 2 + bmpW;// 頁卡1 -> 頁卡2 偏移量 int two = one * 2;// 頁卡1 -> 頁卡3 偏移量 public void onPageScrollStateChanged(int arg0) { } public void onPageScrolled(int arg0, float arg1, int arg2) { } public void onPageSelected(int arg0) { /*兩種方法,這個是一種,下面還有一種,顯然這個比較麻煩 Animation animation = null; switch (arg0) { case 0: if (currIndex == 1) { animation = new TranslateAnimation(one, 0, 0, 0); } else if (currIndex == 2) { animation = new TranslateAnimation(two, 0, 0, 0); } break; case 1: 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; } */ Animation animation = new TranslateAnimation(one*currIndex, one*arg0, 0, 0);//顯然這個比較簡潔,只有一行代碼。 currIndex = arg0; animation.setFillAfter(true);// True:圖片停在動畫結束位置 animation.setDuration(300); imageView.startAnimation(animation); Toast.makeText(WeiBoActivity.this, "您選擇了"+ viewPager.getCurrentItem()+"頁卡", Toast.LENGTH_SHORT).show(); } } }
佈局文件:佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="40.0dip" android:background="#FFFFFF" > <TextView android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text=" @我" android:textColor="#000000" android:textSize="20.0dip" /> <TextView android:id="@+id/text2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="評論" android:textColor="#000000" android:textSize="20.0dip" /> <TextView android:id="@+id/text3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:gravity="center" android:text="私信" android:textColor="#000000" android:textSize="20.0dip" /> </LinearLayout> <ImageView android:id="@+id/cursor" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scaleType="matrix" android:src="@drawable/a" /> <android.support.v4.view.ViewPager android:id="@+id/vPager" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_gravity="center" android:layout_weight="1.0" android:background="#000000" android:flipInterval="30" android:persistentDrawingCache="animation" /> </LinearLayout>
效果以下:post
下面使用ViewPager來實現一個程序引導的demo:動畫
通常來講,引導界面是出現第一次運行時出現的,以後不會再出現。因此須要記錄是不是第一次使用程序,辦法有不少,最容易想到的就是使用SharedPreferences來保存。步驟以下:ui
一、程序進入歡迎界面,SplashActivity,在這裏讀取SharedPreferences裏面的變量,先設置爲true。進入引導界面,而後設置爲false。this
二、以後每次進入歡迎界面讀取SharedPreferences裏面的變量,由於是false,因此不會運行引導界面了。spa
代碼以下:
SplashActivity.java,歡迎界面。
import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Message; /** */ public class SplashActivity extends Activity { boolean isFirstIn = false; private static final int GO_HOME = 1000; private static final int GO_GUIDE = 1001; // 延遲3秒 private static final long SPLASH_DELAY_MILLIS = 3000; private static final String SHAREDPREFERENCES_NAME = "first_pref"; /** * Handler:跳轉到不一樣界面 */ private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case GO_HOME: goHome(); break; case GO_GUIDE: goGuide(); break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); init(); } private void init() { // 讀取SharedPreferences中須要的數據 // 使用SharedPreferences來記錄程序的使用次數 SharedPreferences preferences = getSharedPreferences( SHAREDPREFERENCES_NAME, MODE_PRIVATE); // 取得相應的值,若是沒有該值,說明還未寫入,用true做爲默認值 isFirstIn = preferences.getBoolean("isFirstIn", true); // 判斷程序與第幾回運行,若是是第一次運行則跳轉到引導界面,不然跳轉到主界面 if (!isFirstIn) { // 使用Handler的postDelayed方法,3秒後執行跳轉到MainActivity mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS); } else { mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS); } } private void goHome() { Intent intent = new Intent(SplashActivity.this, MainActivity.class); SplashActivity.this.startActivity(intent); SplashActivity.this.finish(); } private void goGuide() { Intent intent = new Intent(SplashActivity.this, GuideActivity.class); SplashActivity.this.startActivity(intent); SplashActivity.this.finish(); } }
GuideActivity.java引導界面: