溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ImageSwitcher圖像切換器的使用方法

發(fā)布時(shí)間:2020-10-26 14:38:28 來(lái)源:億速云 閱讀:127 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

ImageSwitcher圖像切換器的使用方法?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

描述

在該實(shí)例中,提供一個(gè)圖片切換器和兩個(gè)點(diǎn)擊按鈕,用于切換圖片,并用一個(gè)TextView顯示圖片信息。其中,當(dāng)前圖片若為最后一張,點(diǎn)擊下一張,則跳轉(zhuǎn)到第一張;同理,第一張圖片點(diǎn)擊上一張,則顯示最后一張圖片,循環(huán)查看當(dāng)前圖片。

目標(biāo)效果圖如下所示:

ImageSwitcher圖像切換器的使用方法

ImageSwitcher圖像切換器的使用方法

ImageSwitcher圖像切換器的使用方法

頁(yè)面布局

<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);

    //為獲取到的控件添加顯示效果:淡入動(dòng)畫(huà)和淡出動(dòng)畫(huà)
    image.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
    image.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

    //為圖像切換器設(shè)置一個(gè)ViewFactory,并重寫(xiě)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)點(diǎn)擊按鈕時(shí),圖像切換并顯示相應(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)點(diǎn)擊按鈕時(shí),圖像切換并顯示相應(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)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI