最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android viewpaper實(shí)例探討

 更新時(shí)間:2012年12月03日 09:39:18   作者:  
本文將提供一個(gè)android viewpaper實(shí)例實(shí)現(xiàn)過程,需要了解更多的朋友可以參考下

一、首先,我們來(lái)看一下效果圖,這是新浪微博的Tab滑動(dòng)效果。我們可以手勢(shì)滑動(dòng),也可以點(diǎn)擊上面的頭標(biāo)進(jìn)行切換。與此同方式,白色橫條會(huì)移動(dòng)到相應(yīng)的頁(yè)卡頭標(biāo)下。這是一個(gè)動(dòng)畫效果,白條是緩慢滑動(dòng)過去的。好了,接下來(lái)我們就來(lái)實(shí)現(xiàn)它。

二、在開始前,我們先要認(rèn)識(shí)一個(gè)控件,ViewPager。它是google SDk中自帶的一個(gè)附加包的一個(gè)類,可以用來(lái)實(shí)現(xiàn)屏幕間的切換。這個(gè)附加包是android-support-v4。jar,在最后的源碼中會(huì)提供給大 家,在libs文件夾中。當(dāng)然你也可以自己從網(wǎng)上搜索最新的版本。找到它后,我們需要在項(xiàng)目中添加

三、我們先做界面, 界面設(shè)計(jì)很簡(jiǎn)單,第一行三個(gè)頭標(biāo),第二行動(dòng)畫圖片,第三行頁(yè)卡內(nèi)容展示。

復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:umadsdk="http://schemas.android.com/apk/res/com.LoveBus"
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="100.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="頁(yè)卡1"
android:textColor="#000000"
android:textSize="22.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="頁(yè)卡2"
android:textColor="#000000"
android:textSize="22.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="頁(yè)卡3"
android:textColor="#000000"
android:textSize="22.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="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0"
android:background="#000000"
android:flipInterval="30"
android:persistentDrawingCache="animation" />
</LinearLayout>

我們要展示三個(gè)頁(yè)卡,所以還需要三個(gè)頁(yè)卡內(nèi)容的界面設(shè)計(jì),這里我們只設(shè)置了背景顏色,能起到區(qū)別作用即可。
復(fù)制代碼 代碼如下:

<?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"
android:background="#158684" >
</LinearLayout>

四、代碼部分要進(jìn)行初始化的工作 (1) 先來(lái)變量的定義
復(fù)制代碼 代碼如下:

private ViewPager mPager;//頁(yè)卡內(nèi)容
private List<View> listViews; // Tab頁(yè)面列表
private ImageView cursor;// 動(dòng)畫圖片
private TextView t1, t2, t3;// 頁(yè)卡頭標(biāo)
private int offset = 0;// 動(dòng)畫圖片偏移量
private int currIndex = 0;// 當(dāng)前頁(yè)卡編號(hào)
private int bmpW;// 動(dòng)畫圖片寬度

(2) 初始化頭標(biāo)
復(fù)制代碼 代碼如下:

/**
* 初始化頭標(biāo)
*/
private void InitTextView() {
t1 = (TextView) findViewById(R.id.text1);
t2 = (TextView) findViewById(R.id.text2);
t3 = (TextView) findViewById(R.id.text3);
t1.setOnClickListener(new MyOnClickListener(0));
t2.setOnClickListener(new MyOnClickListener(1));
t3.setOnClickListener(new MyOnClickListener(2));
}
/**
* 頭標(biāo)點(diǎn)擊監(jiān)聽
*/
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);
}
};

(3) 初始化頁(yè)卡內(nèi)容區(qū)
復(fù)制代碼 代碼如下:

<font color="#008000"><font color="black">  /**
  * 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) {
 ?。ǎ╒iewPager) 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) {
 ?。ǎ╒iewPager) arg0).addView(mListViews.get(arg1), 0);
  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) {
  }
  }
</font></font>
這里我們實(shí)現(xiàn)了各頁(yè)卡的裝入和卸載 (4) 初始化動(dòng)畫
/**
* 初始化動(dòng)畫
*/
private void InitImageView() {
cursor = (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;// 計(jì)算偏移量
Matrix matrix = new Matrix();
matrix.postTranslate(offset, 0);
cursor.setImageMatrix(matrix);// 設(shè)置動(dòng)畫初始位置
}

根據(jù)屏幕的分辨率和圖片的寬度計(jì)算動(dòng)畫移動(dòng)的偏移量   
復(fù)制代碼 代碼如下:

/**   * 頁(yè)卡切換監(jiān)聽   */   
public class MyOnPageChangeListener implements OnPageChangeListener {   
int one = offset * 2 + bmpW;// 頁(yè)卡1 -> 頁(yè)卡2 偏移量   
int two = one * 2;// 頁(yè)卡1 -> 頁(yè)卡3 偏移量   
@Override   
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;   }   
currIndex = arg0;   
animation.setFillAfter(true);// True:圖片停在動(dòng)畫結(jié)束位置   
animation.setDuration(300);   
cursor.startAnimation(animation);   }   
@Override   
public void onPageScrolled(int arg0, float arg1, int arg2) {   }   
@Override   
public void onPageScrollStateChanged(int arg0) {   }   }

五、打完收工,快來(lái)看看自己的勞動(dòng)成果吧

相關(guān)文章

  • Android 調(diào)試工具用法詳細(xì)介紹

    Android 調(diào)試工具用法詳細(xì)介紹

    本文主要介紹Android 調(diào)試工具,在Android應(yīng)用開發(fā)的過程中肯定會(huì)用到Android 調(diào)試工具,這里整理了調(diào)試工具的使用方法,有需要的小伙伴可以參考下
    2016-08-08
  • Android Studio快捷鍵生成TAG、Log.x日志輸出介紹

    Android Studio快捷鍵生成TAG、Log.x日志輸出介紹

    這篇文章主要介紹了Android Studio快捷鍵生成TAG、Log.x日志輸出介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-04-04
  • android實(shí)現(xiàn)文字水印效果 支持多行水印

    android實(shí)現(xiàn)文字水印效果 支持多行水印

    這篇文章主要為大家詳細(xì)介紹了android添加文字水印,并支持多行水印,自定義角度和文字大小,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Android 7.0應(yīng)用之間如何共享文件

    Android 7.0應(yīng)用之間如何共享文件

    這篇文章主要介紹了Android 7.0應(yīng)用之間如何共享文件,幫助大家更好的理解和使用Android進(jìn)行開發(fā),感興趣的朋友可以了解下
    2020-12-12
  • Android條目拖拽刪除功能實(shí)例代碼

    Android條目拖拽刪除功能實(shí)例代碼

    最近做項(xiàng)目遇到這樣的需求,要做條目條目拖拽刪除效果,實(shí)際效果和QQ消息刪除一樣,側(cè)滑有制定和刪除,下面通過本文給大家分享Android條目拖拽刪除功能,需要的朋友參考下吧
    2017-08-08
  • Android 中 WebView 的基本用法詳解

    Android 中 WebView 的基本用法詳解

    這篇文章主要介紹了Android 中 WebView 的基本用法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • android選項(xiàng)卡TabHost功能用法詳解

    android選項(xiàng)卡TabHost功能用法詳解

    這篇文章主要為大家詳細(xì)介紹了android選項(xiàng)卡TabHost的功能用法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • 詳解Android studio如何導(dǎo)入jar包方法

    詳解Android studio如何導(dǎo)入jar包方法

    這篇內(nèi)容主要給大家詳細(xì)說明了如何導(dǎo)入jar包,以及Android studio遇到的各種問題和解決辦法。
    2017-12-12
  • Android開發(fā)必備知識(shí) 為什么說Kotlin值得一試

    Android開發(fā)必備知識(shí) 為什么說Kotlin值得一試

    為什么說值得一試,這篇文章主要為大家詳細(xì)介紹了Android開發(fā)必備知識(shí),Kotlin的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android使用CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)鬧鐘

    Android使用CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)鬧鐘

    這篇文章主要為大家詳細(xì)介紹了Android使用CountDownTimer類實(shí)現(xiàn)倒計(jì)時(shí)鬧鐘,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評(píng)論

班玛县| 长宁县| 西乌珠穆沁旗| 措勤县| 邢台市| 木里| 朝阳区| 绍兴县| 肇庆市| 连云港市| 苏州市| 五原县| 榆林市| 大港区| 松溪县| 娱乐| 万源市| 蚌埠市| 宜阳县| 雷州市| 呼和浩特市| 兴业县| 青神县| 合阳县| 临沭县| 临颍县| 丹凤县| 三门峡市| 乐陵市| 涟源市| 盘山县| 尉氏县| 临泉县| 安阳县| 沙田区| 加查县| 鹤岗市| 焦作市| 安宁市| 阜宁县| 鱼台县|