欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

很赞的引导界面效果Android控件ImageSwitcher实现

程序员文章站 2024-03-01 20:46:28
本文实例为大家分享了android控件imageswitcher实现引导界面的代码,供大家参考,具体内容如下 效果图: 布局代码:

本文实例为大家分享了android控件imageswitcher实现引导界面的代码,供大家参考,具体内容如下

效果图:

很赞的引导界面效果Android控件ImageSwitcher实现

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<framelayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">

  <imageswitcher
    android:id="@+id/imageswitcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  </imageswitcher>

  <relativelayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <linearlayout
      android:id="@+id/ll_view"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignparentbottom="true"
      android:layout_marginbottom="30dp"
      android:gravity="center_horizontal"
      android:orientation="horizontal">
    </linearlayout>
  </relativelayout>
</framelayout>

页面代码:

public class imageswitcheractivity extends activity implements viewswitcher.viewfactory, view.ontouchlistener {
  private int[] imgids;//图片id数组
  private int currentposition;//当前选中的图片id序号
  private imageswitcher mimageswitcher;//imagaswitcher 的引用

  private float downx;//按下点的x坐标
  private imageview[] tips;//点点数组
  private linearlayout linearlayout;//装载点点的容器

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_switcher);

    imgids = new int[]{r.drawable.bg, r.drawable.c2, r.drawable.c3, r.drawable.c4, r.drawable.c5, r.drawable.c6, r.drawable.c7, r.drawable.c8, r.drawable.c9};

    mimageswitcher = (imageswitcher) findviewbyid(r.id.imageswitcher);//实例化imageswitcher
    mimageswitcher.setfactory(this); //设置factory
    mimageswitcher.setontouchlistener(this);//设置ontouchlistener,我们通过touch事件来切换图片

    linearlayout = (linearlayout) findviewbyid(r.id.ll_view);//指示器布局
    tips = new imageview[imgids.length];
    for (int i = 0; i < imgids.length; i++) {
      imageview mimageview = new imageview(this);
      tips[i] = mimageview;
      linearlayout.layoutparams layoutparams = new linearlayout.layoutparams(new viewgroup.layoutparams(layoutparams.wrap_content, layoutparams.wrap_content));
      layoutparams.rightmargin = 3;
      layoutparams.leftmargin = 3;

      mimageview.setbackgroundresource(r.drawable.page_indicator_unfocused);
      linearlayout.addview(mimageview, layoutparams);
    }

    //上一个界面传过来的位置
    currentposition = getintent().getintextra("position", 0);
    mimageswitcher.setimageresource(imgids[currentposition]);

    setimagebackground(currentposition);

  }

  //设置选中的tip的背景
  private void setimagebackground(int selectitems) {
    for (int i = 0; i < tips.length; i++) {
      if (i == selectitems) {
        tips[i].setbackgroundresource(r.drawable.page_indicator_focused);
      } else {
        tips[i].setbackgroundresource(r.drawable.page_indicator_unfocused);
      }
    }
  }

  @override
  public view makeview() {
    final imageview i = new imageview(this);
    i.setbackgroundcolor(0xff000000);
    i.setscaletype(imageview.scaletype.center_crop);
    i.setlayoutparams(new imageswitcher.layoutparams(layoutparams.match_parent, layoutparams.match_parent));
    return i;
  }

  @override
  public boolean ontouch(view v, motionevent event) {
    switch (event.getaction()) {
      case motionevent.action_down: {
        downx = event.getx();//手指按下的x坐标
        break;
      }
      case motionevent.action_up: {
        float lastx = event.getx();
        //抬起的时候的x坐标大于按下的时候就显示上一张图片
        if (lastx > downx) {
          if (currentposition > 0) {
            //设置动画
            mimageswitcher.setinanimation(animationutils.loadanimation(getapplication(), r.anim.left_in));
            mimageswitcher.setoutanimation(animationutils.loadanimation(getapplication(), r.anim.right_out));
            currentposition--;
            mimageswitcher.setimageresource(imgids[currentposition % imgids.length]);
            setimagebackground(currentposition);
          } else {
            toast.maketext(getapplication(), "已经是第一张", toast.length_short).show();
          }
        }

        if (lastx < downx) {
          if (currentposition < imgids.length - 1) {
            mimageswitcher.setinanimation(animationutils.loadanimation(getapplication(), r.anim.right_in));
            mimageswitcher.setoutanimation(animationutils.loadanimation(getapplication(), r.anim.lift_out));
            currentposition++;
            mimageswitcher.setimageresource(imgids[currentposition]);
            setimagebackground(currentposition);
          } else {
            toast.maketext(getapplication(), "到了最后一张", toast.length_short).show();
          }
        }
      }

      break;
    }

    return true;
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。