Android實戰簡易教程-第八槍(ImageSwitcher用法執行個體)

來源:互聯網
上載者:User

標籤:android   imageswitcher   

ImageSwitcher 組件的主要功能是完成圖片的切換顯示,例如使用者在進行圖片瀏覽時,可以通過單擊按鈕逐張切換顯示的圖片,在進行切換時還可以加入一些動畫效果。

如果想進行實現圖片的切換功能,則定義的Activity類還必須實現ViewSwitcher.ViewFactory介面,以指定切換視圖的操作工廠,此介面定義如下:

android.widget
介面 ViewSwitcher.ViewFactory
包容類:
ViewSwitcher

 

public static interface ViewSwitcher.ViewFactory

Creates views in a ViewSwitcher.

 

方法摘要
 View makeView() 
          Creates a new View to be added in a ViewSwitcher.
 

方法詳細資料

makeView
View makeView()
Creates a new View to be added in aViewSwitcher.

返回:
a View

本介面中只存在一個makeView()方法,此方法的主要功能是返回一個View對象的多幹設定參數。

下面看一下代碼:

1.main.xml代碼:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/MyLayout"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ImageSwitcher        android:id="@+id/imageSwitcher"        android:layout_gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/btnPrevious"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:enabled="false"            android:text="上一張" />        <Button            android:id="@+id/btnNext"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:enabled="true"            android:text="下一張" />    </LinearLayout></LinearLayout>


2.MainActivity.java代碼如下:

package org.yayun.demo;import android.R.anim;import android.R.integer;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;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.ViewSwitcher.ViewFactory;public class MainActivity extends Activity {private ImageSwitcher imageSwitcher;private Button btnPrevious;private Button btnNext;private int foot=0;private int[] imgRes=new int[]{R.drawable.ispic_a,R.drawable.ispic_b,R.drawable.ispic_c,R.drawable.ispic_d,R.drawable.ispic_e,};public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); // 生命週期方法super.setContentView(R.layout.main); // 設定要使用的布局管理器imageSwitcher=(ImageSwitcher)findViewById(R.id.imageSwitcher);btnPrevious=(Button)findViewById(R.id.btnPrevious);btnNext=(Button)findViewById(R.id.btnNext);imageSwitcher.setFactory(new ViewFactory() {//設定轉化工廠public View makeView() {//覆寫方法ImageView imageView=new ImageView(MainActivity.this);imageView.setBackgroundColor(0xFFFFFFFF);imageView.setScaleType(ImageView.ScaleType.CENTER);//置中顯示imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));//定義組件return imageView;}});imageSwitcher.setImageResource(imgRes[foot++]);//初始化時顯示,必須放在工廠後面,否則會報NullPointerExceptionimageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));//設定動畫imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));//設定動畫btnPrevious.setOnClickListener(new OnClickListener() {public void onClick(View v) {MainActivity.this.imageSwitcher.setImageResource(imgRes[foot--]);MainActivity.this.checkBtnEnable();}});btnNext.setOnClickListener(new OnClickListener() {public void onClick(View v) {MainActivity.this.imageSwitcher.setImageResource(imgRes[foot++]);MainActivity.this.checkBtnEnable();}});}protected void checkBtnEnable() {//判斷按鈕可用狀態if(this.foot<this.imgRes.length-1){this.btnNext.setEnabled(true);}else{this.btnNext.setEnabled(false);}if(this.foot==0){this.btnPrevious.setEnabled(false);}else {this.btnPrevious.setEnabled(true);}}}


3.運行執行個體如下:

總結

1.不設定setFactory()方法會出現NullPointerException錯誤。

2.設定動畫的方法:setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));

3.自訂判斷按鈕是否可用的方法checkBtnEnable()

Android實戰簡易教程-第八槍(ImageSwitcher用法執行個體)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.