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

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

效果图:

34b25b4c0a8ad05d5610c14327b16e27.png

布局代码:

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@+id/imageswitcher"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

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">

页面代码:

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;

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值