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

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

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

前面写过了使用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"><ImageSwitcherandroID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:ID="@+ID/is"></ImageSwitcher><linearLayoutandroID:ID="@+ID/point_layout"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:layout_gravity="bottom"androID:orIEntation="horizontal"><ImageVIEwandroID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:layout_weight="1"androID:src="@mipmap/default_holo"/><ImageVIEwandroID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:layout_weight="1"androID:src="@mipmap/default_holo"/><ImageVIEwandroID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:layout_weight="1"androID:src="@mipmap/default_holo"/><ImageVIEwandroID: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;//手指离开屏幕时的坐标(演示左右滑动)@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.activity_main);is = (ImageSwitcher) findVIEwByID(R.ID.is);is.setFactory(this);//通过工厂实现ImageSwitcherinitpoint();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的属性)@OverrIDepublic VIEw makeVIEw() {//实例化一个用于切换的ImageVIEw视图ImageVIEw iv = new ImageVIEw(this);//默认展示的第一个视图为images[0]iv.setimageResource(images[0]);return iv;}@OverrIDepublic 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_out);}//设置ImageSwitcher的图片资源is.setimageResource(images[index]);//调用方法设置圆点对应状态setimagebackground(index);}return true;}}

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

以上所述是小编给大家介绍的AndroID中使用imagevIEwswitcher 实现图片切换轮播导航的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android中使用imageviewswitcher 实现图片切换轮播导航的方法全部内容,希望文章能够帮你解决Android中使用imageviewswitcher 实现图片切换轮播导航的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1147565.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存