ImageSwitcher圖像切換器的使用實例
本文實例為大家分享了ImageSwitcher圖像切換器的實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
描述
在該實例中,提供一個圖片切換器和兩個點擊按鈕,用于切換圖片,并用一個TextView顯示圖片信息。其中,當(dāng)前圖片若為最后一張,點擊下一張,則跳轉(zhuǎn)到第一張;同理,第一張圖片點擊上一張,則顯示最后一張圖片,循環(huán)查看當(dāng)前圖片。
目標效果圖如下所示:



頁面布局
<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="我是當(dāng)前圖片的信息~"
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>
事件響應(yīng)
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));
//為圖像切換器設(shè)置一個ViewFactory,并重寫makeView方法
image.setFactory(new ViewFactory()
{
@Override
public View makeView()
{
//指定視圖切換工程
return new ImageView(MainActivity.this);
}
});
image.setImageResource(images[index]);
show.setText("一共有"+images.length+"張圖片,當(dāng)前是第"+(index+1)+"張圖片");
//當(dāng)點擊按鈕時,圖像切換并顯示相應(yīng)的信息
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+"張圖片,當(dāng)前是第"+(index+1)+"張圖片");
}
});
//同理,當(dāng)點擊按鈕時,圖像切換并顯示相應(yīng)的信息
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+"張圖片,當(dāng)前是第"+(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;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)實現(xiàn)的文本折疊點擊展開功能示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)的文本折疊點擊展開功能,涉及Android界面布局與屬性控制相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
Android自定義View之漸變色折線圖的實現(xiàn)
折線圖的實現(xiàn)方法在github上有很多開源的程序,但是對于初學(xué)者來講,簡單一點的教程可能更容易入門,下面這篇文章主要給大家介紹了關(guān)于Android自定義View之漸變色折線圖的相關(guān)資料,需要的朋友可以參考下2022-04-04
Android應(yīng)用中使用DOM方式解析XML格式數(shù)據(jù)的基本方法
這篇文章主要介紹了Android應(yīng)用中使用DOM方式解析XML格式數(shù)據(jù)的基本方法,值得注意的是DOM方式解析的效率并不高,在數(shù)據(jù)量大的時候并不推薦使用,需要的朋友可以參考下2016-04-04
Android開發(fā)之開關(guān)按鈕控件ToggleButton簡單用法示例
這篇文章主要介紹了Android開發(fā)之開關(guān)按鈕控件ToggleButton簡單用法,結(jié)合實例形式分析了Android開關(guān)按鈕控件ToggleButton的相關(guān)xml布局與調(diào)用操作技巧,需要的朋友可以參考下2017-12-12
Android把svg圖片轉(zhuǎn)為jpg保存到相冊圖庫
這篇文章主要為大家詳細介紹了Android把svg圖片轉(zhuǎn)為jpg保存到相冊圖庫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
老生常談Android HapticFeedback(震動反饋)
下面小編就為大家?guī)硪黄仙U凙ndroid HapticFeedback(震動反饋)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Android編程開發(fā)之Spinner控件用法實例分析
這篇文章主要介紹了Android編程開發(fā)之Spinner控件用法,結(jié)合實例形式較為詳細的分析了下拉列表Spinner的具體使用技巧,需要的朋友可以參考下2015-12-12

