欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

android中怎么實現(xiàn)一個自動輪播圖效果

這篇文章將為大家詳細講解有關(guān)android中怎么實現(xiàn)一個自動輪播圖效果,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

成都創(chuàng)新互聯(lián)專注于寧德企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計,成都商城網(wǎng)站開發(fā)。寧德網(wǎng)站建設(shè)公司,為寧德等地區(qū)提供建站服務(wù)。全流程按需策劃,專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

1、準(zhǔn)備好需要的輪播圖片,圖片標(biāo)題(初始化,聲明)。

/**輪播圖片*/
  private int[] imageIds=new int[]{
      R.drawable.ic_launcher,
      R.drawable.simple_player_control_focused_holo,
      R.drawable.dot_player1_1,
      R.drawable.jt5,
  };
  /**輪播圖片的標(biāo)題*/
  private String[] titles=new String[]{
      "我是一",
      "我是二",
      "我是三",
      "我是四",
  };

2、在你要輪播的布局里面加入下面的布局(相當(dāng)于加入一個控件,看你想放哪里).

 <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="200dip" >
 
    <android.support.v4.view.ViewPager
      android:id="@+id/viewPager"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />
 
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="35dip"
      android:layout_gravity="bottom"
      android:background="#33000000"
      android:gravity="center"
      android:orientation="vertical" >
 
      <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圖片標(biāo)題"
        android:textColor="@android:color/white" />
 
      <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dip"
        android:orientation="horizontal" >
 
        <View
          android:id="@+id/dot_0"
          android:layout_width="5dip"
          android:layout_height="5dip"
          android:layout_marginLeft="2dip"
          android:layout_marginRight="2dip"
          android:background="@drawable/dot_focused"/>
 
        <View
          android:id="@+id/dot_1"
          android:layout_width="5dip"
          android:layout_height="5dip"
          android:layout_marginLeft="2dip"
          android:layout_marginRight="2dip"
          android:background="@drawable/dot_normal"/>
        <View
          android:id="@+id/dot_2"
          android:layout_width="5dip"
          android:layout_height="5dip"
          android:layout_marginLeft="2dip"
          android:layout_marginRight="2dip"
          android:background="@drawable/dot_normal"/>
        <View
          android:id="@+id/dot_3"
          android:layout_width="5dip"
          android:layout_height="5dip"
          android:layout_marginLeft="2dip"
          android:layout_marginRight="2dip"
          android:background="@drawable/dot_normal"/>
 
 
      </LinearLayout>
    </LinearLayout>
</FrameLayout>

3、把圖片和標(biāo)題都設(shè)置控件里面。

 /**顯示的圖片*/
    images = new ArrayList<ImageView>();
    for(int i=0;i<imageIds.length;i++){
      ImageView imageView = new ImageView(getActivity());
      imageView.setBackgroundResource(imageIds[i]);
      images.add(imageView);
    }
 
    /*顯示的圓點 */
    dots = new ArrayList<View>();
    dots.add(view.findViewById(R.id.dot_0));
    dots.add(view.findViewById(R.id.dot_1));
    dots.add(view.findViewById(R.id.dot_2));
    dots.add(view.findViewById(R.id.dot_3));
    /**輪播的標(biāo)題*/
    title = (TextView) view.findViewById(R.id.title);
    title.setText(titles[0]);

4、findViewById到控件布局里面的ViewPager,new 一個ViewpagerAdapter(),通過setOnPageChangeListener的方法來監(jiān)聽改變

viewPager = (ViewPager) view.findViewById(R.id.viewPager);
    adapter = new ViewPagerAdapter();
    viewPager.setAdapter(adapter);
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        title.setText(titles[position]);
 
        dots.get(position).setBackgroundResource(R.drawable.dot_focused);
        dots.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);
 
        oldPosition = position;
        currentItem = position;
      }
 
      @Override
      public void onPageSelected(int position) {
 
      }
 
      @Override
      public void onPageScrollStateChanged(int state) {
 
      }
    });

5、自定義一個ViewPagerAdapter

 /**
   * 自定義Adapter
   * 內(nèi)部類
   */
  private class ViewPagerAdapter extends PagerAdapter {
 
    @Override
    public int getCount() {
      return images.size();//傳入的數(shù)據(jù)
    }
 
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
      return arg0 == arg1;
    }
 
    @Override
    public void destroyItem(ViewGroup view, int position, Object object) {
      // TODO Auto-generated method stub
//     super.destroyItem(container, position, object);
//     view.removeView(view.getChildAt(position));
//     view.removeViewAt(position);
      view.removeView(images.get(position));
    }
 
    @Override
    public Object instantiateItem(ViewGroup view, int position) {
      // TODO Auto-generated method stub
      view.addView(images.get(position));
      return images.get(position);
    }
 
}

6、這些自己看著改主要是線程池,handler,定時輪換

 /**
   * 圖片輪播任務(wù)
   *
   */
  private class ViewPageTask implements Runnable{
 
    @Override
    public void run() {
      currentItem = (currentItem + 1) % imageIds.length;
      mHandler.sendEmptyMessage(0);
    }
  }
 
  /**
   * 接收子線程傳遞過來的數(shù)據(jù)
   */
  private Handler mHandler = new Handler(){
    public void handleMessage(android.os.Message msg) {
      viewPager.setCurrentItem(currentItem);
    };
  };
  @Override
  public void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    if(scheduledExecutorService != null){
      scheduledExecutorService.shutdown();
      scheduledExecutorService = null;
    }
}

關(guān)于android中怎么實現(xiàn)一個自動輪播圖效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

新聞名稱:android中怎么實現(xiàn)一個自動輪播圖效果
當(dāng)前路徑:http://chinadenli.net/article34/jdpose.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站維護、響應(yīng)式網(wǎng)站、App開發(fā)、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)