Android之動畫ViewPager小結(QQ續三)

來源:互聯網
上載者:User

        點我下載該例源碼-->

        其實寫完這個題目,我也覺得自己光榮的加入標題黨了,我這裡所謂的動畫,其實只是上面的索引小表徵圖的動畫而已,先一下吧,由於只是測試效果,我只是把背景顏色改變了而已,並沒有添加實際的View。這裡我就不介紹什麼是ViewPager了,不知道的童鞋,百度一下吧,一大堆的。

 

 

 

好的,下面我們開始實現,先來看一下布局檔案部分:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/root"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <RelativeLayout        android:id="@+id/RelativeLayout1"        android:layout_width="fill_parent"        android:layout_height="60dp"        android:background="@drawable/login_bg" >        <ImageView            android:id="@+id/friend_list_myImg"            android:layout_width="50dp"            android:layout_height="50dp"            android:layout_alignParentLeft="true"            android:layout_centerVertical="true"            android:layout_marginLeft="15dp"            android:layout_weight="1.0"            android:background="@drawable/img_select"            android:src="@drawable/icon" />        <TextView            android:id="@+id/friend_list_myName"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerVertical="true"            android:layout_marginLeft="20dp"            android:layout_toRightOf="@+id/friend_list_myImg"            android:layout_weight="1.0"            android:gravity="center"            android:text="way"            android:textColor="#ffffff"            android:textSize="20sp" />        <ImageView            android:id="@+id/imageView1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerVertical="true"            android:layout_marginLeft="20dp"            android:layout_toRightOf="@+id/friend_list_myName"            android:src="@drawable/account_current" />    </RelativeLayout>    <RelativeLayout        android:id="@+id/RelativeLayout1"        android:layout_width="fill_parent"        android:layout_height="45dp"        android:background="@drawable/login_moremenu_back"        android:orientation="horizontal" >        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_alignParentLeft="true"            android:layout_alignParentTop="true"            android:gravity="center" >            <ImageView                android:id="@+id/tab1"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:src="@drawable/tab1" />            <ImageView                android:id="@+id/tab2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:src="@drawable/tab2" />            <ImageView                android:id="@+id/tab3"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:src="@drawable/tab3" />        </LinearLayout>        <ImageView            android:id="@+id/tab2_bg"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_centerInParent="true"            android:scaleType="matrix"            android:src="@drawable/topbar_select" />    </RelativeLayout>    <LinearLayout        android:id="@+id/bodylayout"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:background="#ffffff"        android:orientation="vertical" >        <android.support.v4.view.ViewPager            android:id="@+id/viewPager"            android:layout_width="fill_parent"            android:layout_height="fill_parent" />    </LinearLayout></LinearLayout>

下面是布局檔案的:

下面是關於ViewPager的適配器,說實話,我也沒有仔細去研究,只是知道它需要傳入一個view的數組:

/** * 自訂的ViewPager適配器 *  * @author way *  */public class MyPagerAdapter extends PagerAdapter {private List<View> mListViews;public MyPagerAdapter(List<View> mListViews) {// 構造方法// TODO Auto-generated constructor stubthis.mListViews = mListViews;}@Overridepublic void destroyItem(View arg0, int arg1, Object arg2) {((ViewPager) arg0).removeView(mListViews.get(arg1));}@Overridepublic void finishUpdate(View arg0) {}@Overridepublic int getCount() {return mListViews.size();}@Overridepublic Object instantiateItem(View arg0, int arg1) {((ViewPager) arg0).addView(mListViews.get(arg1), 0);return mListViews.get(arg1);}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == (arg1);}@Overridepublic void restoreState(Parcelable arg0, ClassLoader arg1) {}@Overridepublic Parcelable saveState() {return null;}@Overridepublic void startUpdate(View arg0) {}}

最重要的部分到了,裡面注釋說得蠻清楚的,關於那個動畫的索引圖片,如果不是很理解,可以看看最後的那張圖片,從別人部落格複製過來的,感覺很經典:

 

/** * 主Activity *  * @author way *  */public class MainActivity extends Activity implements OnClickListener {private static final int PAGE1 = 0;// 頁面1private static final int PAGE2 = 1;// 頁面2private static final int PAGE3 = 2;// 頁面3private ViewPager mPager;private List<View> mListViews;// Tab頁面private ImageView cursor;// 動畫圖片private ImageView tabBtn1, tabBtn2, tabBtn3;// 頁卡表徵圖private int currIndex = 1;// 當前頁卡編號private int offset = 0;// 動畫圖片位移量private int bmpW;// 動畫圖片寬度@Overridepublic void onCreate(Bundle savedInstanceState) {requestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);setContentView(R.layout.friend_list);InitImageView();// 初始化那個可以動的背景動畫圖片initView();}/** * 初始化動畫 */private void InitImageView() {cursor = (ImageView) findViewById(R.id.tab2_bg);bmpW = BitmapFactory.decodeResource(getResources(),R.drawable.topbar_select).getWidth();// 擷取圖片寬度DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);int screenW = dm.widthPixels;// 擷取螢幕解析度寬度offset = (screenW / 3 - bmpW) / 2;// 計算位移量:螢幕寬度/3,平分為3分,如果是3個view的話,再減去圖片寬度,因為圖片置中,所以要得到兩變剩下的空隙需要再除以2Matrix matrix = new Matrix();int start = offset * 3 + bmpW;matrix.postTranslate(start, 0);// 初始化位置,在中間cursor.setImageMatrix(matrix);// 設定動畫初始位置}private void initView() {tabBtn1 = (ImageView) findViewById(R.id.tab1);tabBtn2 = (ImageView) findViewById(R.id.tab2);tabBtn3 = (ImageView) findViewById(R.id.tab3);tabBtn1.setOnClickListener(this);tabBtn2.setOnClickListener(this);tabBtn3.setOnClickListener(this);mPager = (ViewPager) findViewById(R.id.viewPager);// 擷取我們的viewPagermListViews = new ArrayList<View>();LayoutInflater inflater = LayoutInflater.from(this);View lay1 = inflater.inflate(R.layout.tab1, null);View lay2 = inflater.inflate(R.layout.tab2, null);View lay3 = inflater.inflate(R.layout.tab3, null);mListViews.add(lay1);mListViews.add(lay2);mListViews.add(lay3);mPager.setAdapter(new MyPagerAdapter(mListViews));mPager.setCurrentItem(PAGE2);mPager.setOnPageChangeListener(new OnPageChangeListener() {int one = offset * 2 + bmpW;// 頁卡1 -> 頁卡2 位移量public void onPageSelected(int arg0) {// 頁面選擇事件// TODO Auto-generated method stubAnimation animation = null;switch (arg0) {case PAGE1:// 切換到頁卡1if (currIndex == PAGE2) {// 如果之前顯示的是頁卡2animation = new TranslateAnimation(0, -one, 0, 0);} else if (currIndex == PAGE3) {// 如果之前顯示的是頁卡3animation = new TranslateAnimation(one, -one, 0, 0);}break;case PAGE2:// 切換到頁卡2if (currIndex == PAGE1) {// 如果之前顯示的是頁卡1animation = new TranslateAnimation(-one, 0, 0, 0);} else if (currIndex == PAGE3) {// 如果之前顯示的是頁卡3animation = new TranslateAnimation(one, 0, 0, 0);}break;case PAGE3:// 切換到頁卡3if (currIndex == PAGE1) {// 如果之前顯示的是頁卡1animation = new TranslateAnimation(-one, one, 0, 0);} else if (currIndex == PAGE2) {// 如果之前顯示的是頁卡2animation = new TranslateAnimation(0, one, 0, 0);}break;default:break;}currIndex = arg0;// 動畫結束後,改變當前圖片位置animation.setFillAfter(true);// True:圖片停在動畫結束位置animation.setDuration(300);cursor.startAnimation(animation);}public void onPageScrolled(int arg0, float arg1, int arg2) {// TODO Auto-generated method stub}public void onPageScrollStateChanged(int arg0) {// TODO Auto-generated method stub}});}public void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.tab1:mPager.setCurrentItem(PAGE1);// 點擊頁面1break;case R.id.tab2:mPager.setCurrentItem(PAGE2);// 點擊頁面2break;case R.id.tab3:mPager.setCurrentItem(PAGE3);// 點擊頁面3break;default:break;}}}

根據螢幕的解析度和圖片的寬度計算動畫移動的位移量:



本文參考:http://www.cnblogs.com/dwinter/archive/2012/02/27/2369590.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.