Android常用控件ImageSwitcher使用方法詳解
圖像切換器使用ImageSwitcher表示,用于實(shí)現(xiàn)類似于Windows操作系統(tǒng)下的“Windows照片查看器”中的上一張、下一張切換圖片的功能。在使用ImageSwitcher時,必須實(shí)現(xiàn)ViewSwitcher.ViewFactory接口,并通過makeView()方法創(chuàng)建用于顯示圖片的ImageView對象。makeView()方法將返回一個顯示圖片的ImageView。在使用ImageSwitcher組件時,還有一個非常重要的方法,那就是setImageResource()方法,改方法用于指定在ImageSwitcher中顯示的圖片資源。
第一步:XML布局文件的代碼如下:
<ImageSwitcher android:id="@+id/im" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" android:layout_marginTop="99dp" > </ImageSwitcher> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="77dp" android:text="上一張" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/im" android:layout_marginTop="51dp" android:text="下一張" />
第二步:在Java中編寫邏輯代碼,詳細(xì)代碼如下所示:
package com.example.imageswitcher; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory; import android.os.Build; public class MainActivity extends Activity { private int[] imageId=new int[]{ R.drawable.bj, R.drawable.bj1, R.drawable.bj11,R.drawable.bj12 }; private int index=0; private ImageSwitcher imageSwitcher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageSwitcher=(ImageSwitcher) findViewById(R.id.im); imageSwitcher.setInAnimation(this, android.R.anim.fade_in);//設(shè)置淡入的動畫 imageSwitcher.setOutAnimation(this ,android.R.anim.fade_out);//設(shè)置淡出的動畫 imageSwitcher.setFactory(new ViewFactory() { @Override public View makeView() { ImageView imageView=new ImageView(MainActivity.this);//創(chuàng)建一個Iv 的類 imageView.setAdjustViewBounds(true); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);//設(shè)置保持橫縱比居中縮放圖片 imageView.setLayoutParams(new ImageSwitcher.LayoutParams(240,180)); return imageView; } }); imageSwitcher.setImageResource(imageId[index]);//顯示默認(rèn)的圖片 //獲取兩個按鈕的ID Button up=(Button) findViewById(R.id.button1); Button down=(Button) findViewById(R.id.button2); up.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(index>0){ index--; }else{ index =imageId.length-1; } imageSwitcher.setImageResource(imageId[index]);//顯示當(dāng)前的圖片 } }); down.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(index < imageId.length-1){ index++; }else{ index=0; } imageSwitcher.setImageResource(imageId[index]);//顯示當(dāng)前的圖片 } }); } }
第三步:用手機(jī)運(yùn)行的結(jié)果如下所示:
感謝大家的閱讀,如有錯誤和不足請指出,謝謝大家。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android入門之Gallery+ImageSwitcher用法實(shí)例解析
- 很贊的引導(dǎo)界面效果Android控件ImageSwitcher實(shí)現(xiàn)
- Android控件ImageSwitcher實(shí)現(xiàn)左右圖片切換功能
- Android基于ImageSwitcher實(shí)現(xiàn)圖片切換功能
- Android UI控件之ImageSwitcher實(shí)現(xiàn)圖片切換效果
- Android高級組件ImageSwitcher圖像切換器使用方法詳解
- Android之ImageSwitcher的實(shí)例詳解
- 基于Android實(shí)現(xiàn)保存圖片到本地并可以在相冊中顯示出來
- android獲取相冊圖片和路徑的實(shí)現(xiàn)方法
- Android ViewPager相冊橫向移動的實(shí)現(xiàn)方法
- Android開發(fā)之ImageSwitcher相冊功能實(shí)例分析
相關(guān)文章
Android一個類實(shí)現(xiàn)錄音與播放實(shí)例
大家好,本篇文章主要講的是Android一個類實(shí)現(xiàn)錄音與播放實(shí)例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02android使用RxJava實(shí)現(xiàn)預(yù)加載
這篇文章主要為大家詳細(xì)介紹了android使用RxJava實(shí)現(xiàn)預(yù)加載的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01Android?Studio實(shí)現(xiàn)登錄界面功能
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)登錄界面功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04android Animation監(jiān)聽器AnimationListener的使用方法)
AnimaitonListener的使用方法主要是在Animation上設(shè)置一個監(jiān)聽器,下面通過一個實(shí)例說明它的使用方法2013-11-11Android StickyListHeaders實(shí)現(xiàn)電話本列表效果
這篇文章主要為大家詳細(xì)介紹了Android StickyListHeaders實(shí)現(xiàn)電話本列表效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Android-Zxing實(shí)現(xiàn)二維碼的掃描與生成
本文主要介紹了Android中Zxing實(shí)現(xiàn)二維碼的掃描與生成的方法,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02Android開發(fā)listview選中高亮簡單實(shí)現(xiàn)代碼分享
這篇文章主要介紹了Android開發(fā)listview選中高亮簡單實(shí)現(xiàn)代碼分享,具有一定借鑒價值,需要的朋友可以參考下2018-01-01