欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android常見手動(dòng)和自動(dòng)輪播圖效果

 更新時(shí)間:2018年11月26日 11:45:16   作者:碼海  
這篇文章主要為大家詳細(xì)介紹了android常見手動(dòng)和自動(dòng)輪播圖效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了android手動(dòng)和自動(dòng)輪播圖效果的具體代碼,供大家參考,具體內(nèi)容如下

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)于加入一個(gè)控件,看你想放哪里).

 <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);
    }
 
    /*顯示的圓點(diǎn) */
    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 一個(gè)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、自定義一個(gè)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,定時(shí)輪換

 /**
   * 圖片輪播任務(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;
    }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論