当前位置: 移动技术网 > IT编程>移动开发>Android > Android中使用imageviewswitcher 实现图片切换轮播导航的方法

Android中使用imageviewswitcher 实现图片切换轮播导航的方法

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

前面写过了使用viewflipper和viewpager实现屏幕中视图切换的效果(viewpager未实现轮播)附链接:

android中使用viewflipper类实现屏幕切换(关于坐标轴的问题已补充更改)

android 中使用 viewpager实现屏幕页面切换和页面轮播效果

今天我们在换一种实现方式imageviewswitcher。

imageswitcher是android中控制图片展示效果的一个控件,如:幻灯片效果

imageswitcher粗略的理解就是imageview的选择器。

imageswitcher的原理:imageswitcher有两个子view:imageview,当左右滑动的时候,就在这两个imageview之间来回切换来显示图片。

既然有两个子imageview,那么我们要创建两个imageview给imageswitcher。创建imageviewswitcher中的imageview是通过viewfactory工厂来实现的。

下面我们展示下本次实现效果(可以轮播哦):

好了,废话不多说,开始撸代码:

第一步:layout中建立主布局(framelayout)文件activity_main.xml(包含导航原点的linearlayout布局)

<?xml version="1.0" encoding="utf-8"?>
<framelayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.switcher.mainactivity">
<imageswitcher
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/is">
</imageswitcher>
<linearlayout
android:id="@+id/point_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
<imageview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/default_holo"/>
<imageview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/default_holo"/>
<imageview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/default_holo"/>
<imageview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@mipmap/default_holo"/>
</linearlayout>
</framelayout>

这里大家也可以通过配置文件来布局下面的导航圆点,不必写死在布局文件中。

第二步:java中功能实现代码mainactivity.java

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.motionevent;
import android.view.view;
import android.widget.imageswitcher;
import android.widget.imageview;
import android.widget.linearlayout;
import android.widget.viewswitcher;
import java.util.arraylist;
/**
* created by panchengjia on 2016/12/04.
*/
public class mainactivity extends appcompatactivity implements viewswitcher.viewfactory,view.ontouchlistener{
private imageswitcher is;//声明imageswitcher布局
private linearlayout point_layout;//声明导航圆点的布局
//图片id数组
int[] images={r.mipmap.a1,r.mipmap.a2,r.mipmap.a3,r.mipmap.a4};
//实例化存储导航圆点的集合
arraylist<imageview> points = new arraylist<>();
int index;//声明index,记录图片id数组下标
float startx;//手指接触屏幕时x的坐标(演示左右滑动)
float endx;//手指离开屏幕时的坐标(演示左右滑动)
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
is = (imageswitcher) findviewbyid(r.id.is);
is.setfactory(this);//通过工厂实现imageswitcher
initpoint();
is.setontouchlistener(this);//设置触摸事件
}
//初始化导航圆点的方法
private void initpoint() {
point_layout= (linearlayout) findviewbyid(r.id.point_layout);
int count = point_layout.getchildcount();//获取布局中圆点数量
for(int i =0;i<count;i++){
//将布局中的圆点加入到圆点集合中
points.add((imageview) point_layout.getchildat(i));
}
//设置第一张图片(也就是图片数组的0下标)的圆点状态为触摸实心状态
points.get(0).setimageresource(r.mipmap.touched_holo);
}
//设选中图片对应的导航原点的状态
public void setimagebackground(int selectimage) {
for(int i=0;i<points.size();i++){
//如果选中图片的下标等于圆点集合中下标的id,则改变圆点状态
if(i==selectimage){
points.get(i).setimageresource(r.mipmap.touched_holo);
}else{
points.get(i).setimageresource(r.mipmap.default_holo);
}
}
}
//实现viewfactory的方法实例化imageview(这里未设置imageview的属性)
@override
public view makeview() {
//实例化一个用于切换的imageview视图
imageview iv = new imageview(this);
//默认展示的第一个视图为images[0]
iv.setimageresource(images[0]);
return iv;
}
@override
public boolean ontouch(view v, motionevent event) {
//按下屏幕
if(event.getaction()==motionevent.action_down){
startx=event.getx();//获取按下屏幕时x轴的坐标
//手指抬起
}else if (event.getaction()==motionevent.action_up){
endx=event.getx();
//判断结束坐标大于起始坐标则为下一张(为避免误操作,设置30的判断区间)
if(startx-endx>30){
//三目运算判断当前图片已经为最后一张,则从头开始
index = index+1<images.length?++index:0;
//使用系统自带的切换出入动画效果(也可以向viewflipper中一样自定义动画效果)
is.setinanimation(this,android.r.anim.fade_in);
is.setoutanimation(this,android.r.anim.fade_out);
//判断结束坐标小于于起始坐标则为上一张(为避免误操作,设置30的判断区间)
}else if(endx-startx>30){
//三目运算判断当前图片已经为第一张,则上一张为数组内最后一张图片
index = index-1>=0?--index:images.length-1;
is.setinanimation(this,android.r.anim.fade_in);
is.setoutanimation(this,android.r.anim.fade_out);
}
//设置imageswitcher的图片资源
is.setimageresource(images[index]);
//调用方法设置圆点对应状态
setimagebackground(index);
}
return true;
}
}

个人感觉,就图片切换轮播来讲,imageviewswitcher相对于viewflipper和viewpager实现起来,还是简单了很多。

以上所述是小编给大家介绍的android中使用imageviewswitcher 实现图片切换轮播导航的方法,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网