Android開發(fā)實(shí)現(xiàn)的自動換圖片、輪播圖效果示例
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)的自動換圖片、輪播圖效果。分享給大家供大家參考,具體如下:
50行代碼急速實(shí)現(xiàn)輪播圖:
先看效果:


兩個例子沒太大區(qū)別,
不過i是布局文件中控件的Height不一樣罷了這里一第二個為例:
public class MainActivity extends Activity {
int[] imageIds = new int[]{
R.drawable.a00,R.drawable.a01,R.drawable.a02,R.drawable.a3,R.drawable.a4,R.drawable.a5,
R.drawable.a6,R.drawable.a7,R.drawable.a8,R.drawable.a9
};
private AdapterViewFlipper flipper ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//創(chuàng)建一個BaseAdapter對象,該對象負(fù)責(zé)提供Gallery所顯示的列表項(xiàng)
flipper = (AdapterViewFlipper) findViewById(R.id.flipper);
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return imageIds.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//創(chuàng)建一個ImageView
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(imageIds[position]);
//設(shè)置ImageView的縮放類型
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//為ImageView設(shè)置布局參數(shù)
imageView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
return imageView;
}
};
flipper.setAdapter(adapter);
}
public void prev(View source){
//顯示上一個組件
flipper.showPrevious();
//停止自動播放
flipper.stopFlipping();
}
public void next(View source){
//顯示下一個組件
flipper.showNext();
//停止自動播放
flipper.stopFlipping();
}
public void auto(View source){
//開始自動播放
flipper.startFlipping();
}
}
嗯好像是五十多行。。無所謂了
順便給下**布局文件:**無腦定義罷了
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<AdapterViewFlipper
android:id="@+id/flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:flipInterval="5000"
android:layout_alignParentTop="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="prev"
android:text="下一個" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="next"
android:text="上一個" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="auto"
android:text="自動播放" />
</RelativeLayout>
搞定 啥都不要 就這么簡單~
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android實(shí)現(xiàn)輪播圖片展示效果
- Android實(shí)現(xiàn)炫酷輪播圖效果
- Android使用viewpager實(shí)現(xiàn)自動無限輪播圖
- Android實(shí)現(xiàn)ViewPage輪播圖效果
- Android ViewPager實(shí)現(xiàn)輪播圖效果
- Android如何使用RecyclerView打造首頁輪播圖
- android實(shí)現(xiàn)banner輪播圖無限輪播效果
- 簡單實(shí)現(xiàn)android輪播圖
- Android自定義控件實(shí)現(xiàn)優(yōu)雅的廣告輪播圖
- Android自定義輪播圖效果
相關(guān)文章
android 多點(diǎn)觸摸圖片縮放的具體實(shí)現(xiàn)方法
2013-06-06
Flutter 實(shí)現(xiàn)虎牙/斗魚 彈幕功能
這篇文章主要介紹了Flutter 實(shí)現(xiàn)虎牙/斗魚 彈幕功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04
Android studio 3.0上進(jìn)行多渠道打包遇到的問題小結(jié)(超簡潔版)
這篇文章主要介紹了Android studio 3.0上進(jìn)行多渠道打包遇到的問題小結(jié)(超簡潔版),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11
Kotlin文件讀寫與SharedPreferences存儲功能實(shí)現(xiàn)方法
SharedPreferences是安卓平臺上一個輕量級的存儲類,用來保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時,將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時,再從SharedPreferences中將值取出2022-12-12
Android實(shí)現(xiàn)標(biāo)題上顯示隱藏進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)標(biāo)題上顯示隱藏進(jìn)度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android 自定義TextView去除paddingTop和paddingBottom
這篇文章主要介紹了Android 自定義TextView去除paddingTop和paddingBottom的相關(guān)資料,這里提供實(shí)例來幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
Android實(shí)現(xiàn)后臺服務(wù)拍照功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)后臺服務(wù)拍照功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05

