[Android學習筆記3]ImageSwitcher

來源:互聯網
上載者:User

今天實現一個比較簡單的圖片轉場效果的小程式。

利用ImageSwitcher類,通過設定一個ViewFactory工廠,實現其makeView()方法來建立顯示圖片的View。

方法setImageResource用來顯示指定的圖片資源。

 

 

1. 將8張圖片放入/res/drawable目錄下,同時命名為sample_0,sample_1,....sample_7等;

2. 在代碼中定義資源id數組;

static final Integer[] imagelist = {R.drawable.sample_0,R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_4,R.drawable.sample_5,R.drawable.sample_6,R.drawable.sample_7,};

 

 

Activity.java

package com.luoye.allview;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 SecondActivity extends Activity implements OnClickListener, ViewFactory{ImageSwitcher imageswitcher;private Button button_pre;private Button button_next;static final Integer[] imagelist = {R.drawable.sample_0,R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_4,R.drawable.sample_5,R.drawable.sample_6,R.drawable.sample_7,};private static int index = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.secondactivity);imageswitcher = (ImageSwitcher)findViewById(R.id.imageswitch);imageswitcher.setFactory(this);imageswitcher.setImageResource(imagelist[index]);button_pre = (Button)findViewById(R.id.button_pre);button_next = (Button)findViewById(R.id.button_next);button_pre.setOnClickListener(this);button_next.setOnClickListener(this);}@Overridepublic View makeView() {// TODO Auto-generated method stubreturn new ImageView(this);}@Overridepublic void onClick(View v) {                      //實現button的onClick方法// TODO Auto-generated method stubswitch(v.getId()){case R.id.button_next:index++;if(index == imagelist.length){index = 0;}imageswitcher.setImageResource(imagelist[index]);break;case R.id.button_pre:index--;if(index < 0){index =  imagelist.length - 1;}imageswitcher.setImageResource(imagelist[index]);break;default:break;}}}

布局檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <ImageSwitcher         android:id="@+id/imageswitch"  android:layout_width="wrap_content"  android:layout_height="100dp"        ></ImageSwitcher>        <Button         android:id="@+id/button_next"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="上一張"        />        <Button         android:id="@+id/button_pre"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="下一張"        /></LinearLayout>

效果:

點擊下一張後,實現了切換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.