android常見手動(dòng)和自動(dòng)輪播圖效果
本文實(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;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用RollViewPager實(shí)現(xiàn)輪播圖
- Android實(shí)現(xiàn)輪播圖片效果
- Android實(shí)現(xiàn)背景圖片輪播
- Android輪播圖點(diǎn)擊圖片放大效果的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)圖片輪播列表
- Android仿京東快報(bào)無限輪播效果
- 詳解android 視頻圖片混合輪播實(shí)現(xiàn)
- Android開發(fā)實(shí)現(xiàn)的自動(dòng)換圖片、輪播圖效果示例
- Android實(shí)現(xiàn)輪播圖片展示效果
- Android基于AdapterViewFlipper實(shí)現(xiàn)的圖片/文字輪播動(dòng)畫控件
相關(guān)文章
Android自定義帶進(jìn)度條WebView仿微信加載過程
這篇文章主要為大家詳細(xì)介紹了Android自定義帶進(jìn)度條WebView仿微信加載過程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Android實(shí)現(xiàn)圖片一邊的三角形邊框效果
這篇文章主要介紹了Android實(shí)現(xiàn)圖片一邊的三角形邊框效果,本文圖文并茂通過實(shí)例代碼講解的非常詳細(xì),需要的朋友可以參考下2019-12-12
Android?8.0實(shí)現(xiàn)藍(lán)牙遙控器自動(dòng)配對(duì)
這篇文章主要為大家詳細(xì)介紹了Android?8.0實(shí)現(xiàn)藍(lán)牙遙控器自動(dòng)配對(duì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android倒計(jì)時(shí)神器(CountDownTimer)
這篇文章主要為大家詳細(xì)介紹了Android倒計(jì)時(shí)神器CountDownTimer,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
eclipse搭建android開發(fā)環(huán)境詳細(xì)步驟
本文主要介紹了eclipse搭建android開發(fā)環(huán)境詳細(xì)步驟,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
Android列表動(dòng)圖展示的實(shí)現(xiàn)策略
這篇文章主要給大家介紹了關(guān)于Android列表動(dòng)圖展示的實(shí)現(xiàn)策略的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
詳解Android Material設(shè)計(jì)中陰影效果的實(shí)現(xiàn)方法
這篇文章主要介紹了Android Material設(shè)計(jì)中陰影效果的實(shí)現(xiàn)方法,包括自定義陰影的輪廓和裁剪等,需要的朋友可以參考下2016-04-04

