android常見(jiàn)手動(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(),通過(guò)setOnPageChangeListener的方法來(lái)監(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);
}
}
/**
* 接收子線程傳遞過(guò)來(lái)的數(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)無(wú)限輪播效果
- 詳解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使用ExpandableListView實(shí)現(xiàn)三層嵌套折疊菜單
這篇文章主要介紹了Android使用ExpandableListView實(shí)現(xiàn)三層嵌套折疊菜單,對(duì)布局感興趣的同學(xué)可以參考下2021-04-04
Android實(shí)現(xiàn)隱藏狀態(tài)欄和標(biāo)題欄
這篇文章主要介紹了Android實(shí)現(xiàn)隱藏狀態(tài)欄和標(biāo)題欄的相關(guān)資料,需要的朋友可以參考下2015-06-06
android編程之menu按鍵功能實(shí)現(xiàn)方法
這篇文章主要介紹了android編程之menu按鍵功能實(shí)現(xiàn)方法,實(shí)例分析了Android實(shí)現(xiàn)menu的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
android實(shí)現(xiàn)視頻的加密和解密(使用AES)
本篇文章主要介紹了android實(shí)現(xiàn)視頻的加密和解密(使用AES),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
Android打開系統(tǒng)相機(jī)并拍照的2種顯示方法
這篇文章主要介紹了Android打開系統(tǒng)相機(jī)并拍照的2種顯示方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Android實(shí)現(xiàn)清除單個(gè)域名的cookie
這篇文章主要介紹了Android實(shí)現(xiàn)清除單個(gè)域名的cookie,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android 進(jìn)度條按鈕ProgressButton的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 進(jìn)度條按鈕實(shí)現(xiàn)(ProgressButton)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-10-10
Android本地驗(yàn)證碼的簡(jiǎn)易實(shí)現(xiàn)方法(防止暴力登錄)
驗(yàn)證馬真是無(wú)處不在啊,主要作用是防止惡意暴力破解登錄,這篇文章主要介紹了Android本地驗(yàn)證碼的簡(jiǎn)易實(shí)現(xiàn)方法(防止暴力登錄),需要的朋友可以參考下2017-04-04

