Android控件ImageSwitcher實(shí)現(xiàn)左右圖片切換功能
ImageSwitcher類(lèi)是ViewSwitcher類(lèi)的子類(lèi),它實(shí)現(xiàn)的效果是在完成ImageView的切換并且?guī)в袆?dòng)畫(huà)效果。要使用這個(gè)類(lèi)需要以下兩個(gè)步驟:
1)為ImageSwitcher類(lèi)提供一個(gè)ViewFactory,該ViewFactory生成的View組件必須是ImageView。
2)需要切換的時(shí)候,只需要嗲用ImageSwitcher的setImageDrawable()、setImageResource()、setImageURL()方法即可實(shí)現(xiàn)切換。
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageSwitcher
android:id="@+id/imageswitcher"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="back" />
<Button
android:id="@+id/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="forward" />
</RelativeLayout>
</LinearLayout>
Main_activity.java:
package com.example.android_imageswitcher1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity implements ViewFactory,
OnClickListener {
ImageSwitcher mImageSwitcher = null;
Button btn1, btn2;
int index = 0;
int[] resId = new int[9];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageswitcher);
btn1 = (Button) this.findViewById(R.id.back);
btn2 = (Button) this.findViewById(R.id.forward);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
mImageSwitcher.setFactory(this);
mImageSwitcher.setInAnimation(this, android.R.anim.slide_in_left);
mImageSwitcher.setOutAnimation(this, android.R.anim.slide_out_right);
initResources();
if (resId.length > 0) {
mImageSwitcher.setImageResource(resId[0]);
}
}
public void initResources() {
resId[0] = R.drawable.adobe;
resId[1] = R.drawable.android;
resId[2] = R.drawable.circle;
resId[3] = R.drawable.digg;
resId[4] = R.drawable.flower;
resId[5] = R.drawable.gmail;
resId[6] = R.drawable.imdb;
resId[7] = R.drawable.photo;
resId[8] = R.drawable.point;
}
@Override
public View makeView() {
return new ImageView(MainActivity.this);
}
@Override
public void onClick(View view) {
int action = view.getId();
switch (action) {
case R.id.back:
index--;
if (index < 0) {
index = resId.length - 1;
}
mImageSwitcher.setImageResource(resId[index]);
break;
case R.id.forward:
index++;
if (index > resId.length - 1) {
index = 0;
}
mImageSwitcher.setImageResource(resId[index]);
break;
default:
break;
}
}
}
實(shí)現(xiàn)的效果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
- Android開(kāi)發(fā)之使用ViewPager實(shí)現(xiàn)圖片左右滑動(dòng)切換效果
- Android自定義ImageView實(shí)現(xiàn)點(diǎn)擊兩張圖片切換效果
- Android點(diǎn)擊Button實(shí)現(xiàn)切換點(diǎn)擊圖片效果的示例
- Android編程單擊圖片實(shí)現(xiàn)切換效果的方法
- Android實(shí)現(xiàn)滑動(dòng)屏幕切換圖片
- Android中ViewPager組件的基本用法及實(shí)現(xiàn)圖片切換的示例
- Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼
- Android編程實(shí)現(xiàn)圖片背景漸變切換與圖層疊加效果
- Android 圖片切換器(dp、sp、px) 的單位轉(zhuǎn)換器
- Android實(shí)現(xiàn)左右滑動(dòng)切換圖片
相關(guān)文章
Android的HTTP擴(kuò)展包OkHttp中的緩存功能使用方法解析
OkHttp(GitHub主頁(yè)https://github.com/square/okhttp)是一款高人氣的第三方Android網(wǎng)絡(luò)編程包,這里我們來(lái)看一下Android的HTTP擴(kuò)展包OkHttp中的緩存功能使用方法解析:2016-07-07
activity控制對(duì)話框風(fēng)格、顯示大小與位置
對(duì)于對(duì)話框風(fēng)格大家普遍使用PopupWindow,也有許多朋友開(kāi)發(fā)設(shè)計(jì)時(shí)使用的是activity對(duì)話框方式,因此,本文對(duì)如何通過(guò)activity實(shí)現(xiàn)與PopupWindow相同的效果進(jìn)行詳細(xì)介紹,具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12
解決Android平臺(tái)中應(yīng)用程序OOM異常的方法
這篇文章主要介紹了解決Android平臺(tái)中應(yīng)用程序OOM異常的方法,通常這一塊也是程序中的重點(diǎn)之一,感興趣的小伙伴們可以參考一下2015-12-12
android popuwindow點(diǎn)擊外部窗口不消失的實(shí)例
下面小編就為大家?guī)?lái)一篇android popuwindow點(diǎn)擊外部窗口不消失的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
Android畫(huà)圖之抗鋸齒paint和Canvas兩種方式實(shí)例
本篇文章主要介紹了Android畫(huà)圖之抗鋸齒paint和Canvas兩種方式實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Android實(shí)現(xiàn)過(guò)渡動(dòng)畫(huà)、引導(dǎo)頁(yè) Android判斷是否第一次啟動(dòng)App
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)過(guò)渡動(dòng)畫(huà)、引導(dǎo)頁(yè),以及Android判斷是否第一次啟動(dòng)App,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12

