当前位置: 移动技术网 > IT编程>移动开发>Android > Android控件ImageSwitcher实现左右图片切换功能

Android控件ImageSwitcher实现左右图片切换功能

2019年07月24日  | 移动技术网IT编程  | 我要评论

朗诵配乐网,ks6899企业邮局,影空论坛

imageswitcher类是viewswitcher类的子类,它实现的效果是在完成imageview的切换并且带有动画效果。要使用这个类需要以下两个步骤:

1)为imageswitcher类提供一个viewfactory,该viewfactory生成的view组件必须是imageview。

2)需要切换的时候,只需要嗲用imageswitcher的setimagedrawable()、setimageresource()、setimageurl()方法即可实现切换。

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

  }

}

实现的效果如下:

以上就是本文的全部内容,希望对大家学习android软件编程有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网