欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android控件ImageSwitcher實現(xiàn)左右圖片切換功能

 更新時間:2022年05月17日 09:51:06   作者:summerpxy  
這篇文章主要為大家詳細介紹了Android控件ImageSwitcher實現(xiàn)左右圖片切換功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

ImageSwitcher類是ViewSwitcher類的子類,它實現(xiàn)的效果是在完成ImageView的切換并且?guī)в袆赢嬓Ч?。要使用這個類需要以下兩個步驟:

1)為ImageSwitcher類提供一個ViewFactory,該ViewFactory生成的View組件必須是ImageView。

2)需要切換的時候,只需要嗲用ImageSwitcher的setImageDrawable()、setImageResource()、setImageURL()方法即可實現(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;
    }

  }

}

實現(xiàn)的效果如下:

以上就是本文的全部內(nèi)容,希望對大家學(xué)習Android軟件編程有所幫助。

相關(guān)文章

  • Android的HTTP擴展包OkHttp中的緩存功能使用方法解析

    Android的HTTP擴展包OkHttp中的緩存功能使用方法解析

    OkHttp(GitHub主頁https://github.com/square/okhttp)是一款高人氣的第三方Android網(wǎng)絡(luò)編程包,這里我們來看一下Android的HTTP擴展包OkHttp中的緩存功能使用方法解析:
    2016-07-07
  • activity控制對話框風格、顯示大小與位置

    activity控制對話框風格、顯示大小與位置

    對于對話框風格大家普遍使用PopupWindow,也有許多朋友開發(fā)設(shè)計時使用的是activity對話框方式,因此,本文對如何通過activity實現(xiàn)與PopupWindow相同的效果進行詳細介紹,具有很好的參考價值,需要的朋友一起來看下吧
    2016-12-12
  • 解決Android平臺中應(yīng)用程序OOM異常的方法

    解決Android平臺中應(yīng)用程序OOM異常的方法

    這篇文章主要介紹了解決Android平臺中應(yīng)用程序OOM異常的方法,通常這一塊也是程序中的重點之一,感興趣的小伙伴們可以參考一下
    2015-12-12
  • Android實現(xiàn)花瓣飄落效果的步驟

    Android實現(xiàn)花瓣飄落效果的步驟

    這篇文章主要介紹了Android實現(xiàn)花瓣飄落效果的步驟,幫助大家更好的理解和學(xué)習使用Android開發(fā),感興趣的朋友可以了解下
    2021-04-04
  • Android RIL使用詳解

    Android RIL使用詳解

    這篇文章主要介紹了Android RIL使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2020-07-07
  • android popuwindow點擊外部窗口不消失的實例

    android popuwindow點擊外部窗口不消失的實例

    下面小編就為大家?guī)硪黄猘ndroid popuwindow點擊外部窗口不消失的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android畫圖之抗鋸齒paint和Canvas兩種方式實例

    Android畫圖之抗鋸齒paint和Canvas兩種方式實例

    本篇文章主要介紹了Android畫圖之抗鋸齒paint和Canvas兩種方式實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Android BannerView通用封裝詳解

    Android BannerView通用封裝詳解

    這篇文章主要介紹了Android BannerView通用封裝詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Android VideoCache視頻緩存的方法詳解

    Android VideoCache視頻緩存的方法詳解

    這篇文章主要介紹了Android VideoCache視頻緩存的方法詳解的相關(guān)資料,AndroidVideoCache是一個視頻/音頻緩存庫,利用本地代理實現(xiàn)了邊下邊播,需要的朋友可以參考下
    2017-07-07
  • Android實現(xiàn)過渡動畫、引導(dǎo)頁 Android判斷是否第一次啟動App

    Android實現(xiàn)過渡動畫、引導(dǎo)頁 Android判斷是否第一次啟動App

    這篇文章主要為大家詳細介紹了Android實現(xiàn)過渡動畫、引導(dǎo)頁,以及Android判斷是否第一次啟動App,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12

最新評論