ImageSwitcher影象切換器的使用例項
阿新 • • 發佈:2020-10-26
本文例項為大家分享了ImageSwitcher影象切換器的實現程式碼,供大家參考,具體內容如下
描述
在該例項中,提供一個圖片切換器和兩個點選按鈕,用於切換圖片,並用一個TextView顯示圖片資訊。其中,當前圖片若為最後一張,點選下一張,則跳轉到第一張;同理,第一張圖片點選上一張,則顯示最後一張圖片,迴圈檢視當前圖片。
目標效果圖如下所示:
頁面佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg67" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:text="我是當前圖片的資訊~" android:textSize="24dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageSwitcher android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image" android:layout_gravity="center" android:background="#666666"> </ImageSwitcher> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一張" android:layout_marginLeft="20dp" android:textSize="24dp" android:id="@+id/up" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一張" android:layout_marginLeft="20dp" android:textSize="24dp" android:id="@+id/down" /> </LinearLayout> </LinearLayout> </LinearLayout>
事件響應
package com.example.imageswitchdemo; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.TextView; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity { TextView show=null; Button up,dowm=null; ImageSwitcher image=null; private int[] images=new int[]{R.drawable.a001,R.drawable.a002,R.drawable.a003,R.drawable.a004,R.drawable.a005,R.drawable.a006,R.drawable.a007,R.drawable.a008,R.drawable.a009}; private int index=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //獲取控制元件 show=(TextView) findViewById(R.id.show); up=(Button) findViewById(R.id.up); dowm=(Button) findViewById(R.id.down); image=(ImageSwitcher) findViewById(R.id.image); //為獲取到的控制元件新增顯示效果:淡入動畫和淡出動畫 image.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in)); image.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out)); //為影象切換器設定一個ViewFactory,並重寫makeView方法 image.setFactory(new ViewFactory() { @Override public View makeView() { //指定檢視切換工程 return new ImageView(MainActivity.this); } }); image.setImageResource(images[index]); show.setText("一共有"+images.length+"張圖片,當前是第"+(index+1)+"張圖片"); //當點選按鈕時,影象切換並顯示相應的資訊 up.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if(index>0) index--; else index=images.length-1; image.setImageResource(images[index]); show.setText("一共有"+images.length+"張圖片,當前是第"+(index+1)+"張圖片"); } }); //同理,當點選按鈕時,影象切換並顯示相應的資訊 dowm.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { if(index<images.length-1) index++; else index=0; image.setImageResource(images[index]); show.setText("一共有"+images.length+"張圖片,當前是第"+(index+1)+"張圖片"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main,menu); return true; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。