從零開始學android<ImageSwitcher圖片切換組件.二十六.>

來源:互聯網
上載者:User

標籤:風飛雪未揚   從零開始學android   imageswitcher圖片切換組件   

ImageSwitcher組件的主要功能是完成圖片的切換顯示,例如使用者在進行圖片瀏覽的時候,可以通過按鈕點擊一張張的切換顯示的圖片,而且使用ImageSwitcher組件在每次切換的時候也可以為其增加一些動畫的效果,此類定義如下:java.lang.Object   ? android.view.View     ? android.view.ViewGroup       ? android.widget.FrameLayout         ? android.widget.ViewAnimator           ? android.widget.ViewSwitcher             ? android.widget.ImageSwitcher
用到的方法
1 public ImageSwitcher(Context context) 構造 建立ImageSwitcher對象
2 public void setFactory(ViewSwitcher.ViewFactory factory) 普通 設定ViewFactory對象,用於完成兩個圖片切換時ViewSwitcher的轉換操作
3 public void setImageResource(int resid) 普通 設定顯示的圖片資源ID
4 public void setInAnimation(Animation inAnimation) 普通 圖片讀取進ImageSwitcher時的動畫效果
5 public void setOutAnimation(Animation outAnimation) 普通 圖片從ImageSwitcher要消失時的動畫效果


如果要想實現圖片的切換功能,則定義的Activity類還必須實現ViewSwitcher.ViewFactory介面,以指定切換視圖的操作工廠,此介面定義如下:public static interface ViewSwitcher.ViewFactory {/** * 建立一個新的View顯示,並將其加入到ViewSwitcher之中 * @return新的View對象 */

public abstract View makeView() ;}
如private class ViewFactoryImpl implements ViewFactory {@Overridepublic View makeView() {ImageView img = new ImageView(MyImageSwitcherDemo.this);// 實例化圖片顯示img.setBackgroundColor(0xFFFFFFFF); // 設定背景顏色img.setScaleType(ImageView.ScaleType.CENTER);//置中顯示img.setLayoutParams(new ImageSwitcher.LayoutParams(// 自適應圖片大小LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));// 定義組件return img;}}
XMl檔案
<RelativeLayout 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: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" >    <ImageSwitcher        android:id="@+id/imageSwitcher1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="114dp" >    </ImageSwitcher>    <Button        android:id="@+id/button1"        style="?android:attr/buttonStyleSmall"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/button2"        android:layout_alignBottom="@+id/button2"        android:layout_marginRight="20dp"        android:layout_toLeftOf="@+id/imageSwitcher1"        android:text="上一張" />    <Button        android:id="@+id/button2"        style="?android:attr/buttonStyleSmall"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_toRightOf="@+id/imageSwitcher1"        android:text="下一張" /></RelativeLayout>


JAVA檔案 
package com.example.imageswitcher;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.view.ViewGroup.LayoutParams;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 Button ButNext, ButPrevious;//初始化按鈕private ImageSwitcher imageSwitcher;//初始化組件private int Images[] = { R.drawable.a1, R.drawable.a2, R.drawable.a3,R.drawable.a4, R.drawable.a5, R.drawable.a6 };//設定圖片資料private int foot = 0;//設定角標@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher1);//獲得組件ButNext = (Button) this.findViewById(R.id.button1);ButPrevious = (Button) this.findViewById(R.id.button2);imageSwitcher.setFactory(new Myfactory());//為組件設定組件工廠imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in));//設定圖片進入動畫imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_out));//設定圖片離開動畫imageSwitcher.setImageResource(Images[foot++]);//設定圖片//按鈕事件監聽ButNext.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {// TODO Auto-generated method stubimageSwitcher.setImageResource(Images[foot++]);MainActivity.this.CheckEnable();//設定按鈕是否可用防止數組越界}});//按鈕事件監聽ButPrevious.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubimageSwitcher.setImageResource(Images[foot--]);MainActivity.this.CheckEnable();}});}class Myfactory implements ViewFactory {@Overridepublic View makeView() {// TODO Auto-generated method stubImageView image = new ImageView(MainActivity.this);//設定圖片組件image.setBackgroundColor(Color.GRAY);//設定對齊效果image.setScaleType(ImageView.ScaleType.CENTER);//設定劇中image.setLayoutParams(new ImageSwitcher.LayoutParams( // 自適應圖片大小LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // 定義組件return image;//返回圖片}}public void CheckEnable() {if (this.foot < this.Images.length - 1) {this.ButNext.setEnabled(true); // 按鈕可用} else {this.ButNext.setEnabled(false); // 按鈕不可用}if (this.foot == 0) {this.ButPrevious.setEnabled(false); // 按鈕不可用} else {this.ButPrevious.setEnabled(true); // 按鈕可用}}}



+
Textswitcher與該組件的操作基本相同,不再做具體介紹,讀者可自行練習
下節預報:gallery拖拉組件

聯繫我們

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