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

 更新时间:2016年12月05日 11:51:30   作者:潘侯爷  
ImageSwitcher是Android中控制图片展示效果的一个控件。本文给大家介绍Android中使用imageviewswitcher 实现图片切换轮播导航的方法,需要的朋友参考下吧

前面写过了使用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 实现图片切换轮播导航的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • Android中使用AsyncTask实现文件下载以及进度更新提示

    Android中使用AsyncTask实现文件下载以及进度更新提示

    AsyncTask,它使创建需要与用户界面交互的长时间运行的任务变得更简单,本篇文章主要介绍了Android中使用AsyncTask实现文件下载以及进度更新提示,有兴趣的可以了解一下。
    2016-12-12
  • android读取assets中Excel表格并显示

    android读取assets中Excel表格并显示

    这篇文章主要为大家详细介绍了android读取assets中Excel表格并显示的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • 基于Android studio3.6的JNI教程之ncnn之语义分割ENet

    基于Android studio3.6的JNI教程之ncnn之语义分割ENet

    这篇文章主要介绍了基于Android studio3.6的JNI教程之ncnn之语义分割ENet的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值 ,需要的朋友可以参考下
    2020-03-03
  • 一篇文章弄懂kotlin的扩展方法

    一篇文章弄懂kotlin的扩展方法

    这篇文章主要给大家介绍了如何通过一篇文章弄懂kotlin的扩展方法,文中通过示例代码介绍的非常详细,对大家学习或者使用kotlin具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-09-09
  • Android的ListView多选删除操作实现代码

    Android的ListView多选删除操作实现代码

    这篇文章主要为大家详细介绍了Android的ListView多选删除操作实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Android XML数据解析简单示例

    Android XML数据解析简单示例

    这篇文章主要介绍了Android XML数据解析简单示例,本文直接给出了实现代码,需要的朋友可以参考下
    2014-10-10
  • 详解Glide4.0集成及使用注意事项

    详解Glide4.0集成及使用注意事项

    这篇文章主要介绍了详解Glide4.0集成及使用注意事项,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • android嵌套滚动入门实践

    android嵌套滚动入门实践

    嵌套滚动是 Android OS 5.0之后,google 为我们提供的新特性,本篇文章主要介绍了android嵌套滚动入门实践,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Android通过AlarmManager类实现简单闹钟功能

    Android通过AlarmManager类实现简单闹钟功能

    这篇文章主要为大家详细介绍了Android通过AlarmManager类实现简单闹钟功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • Android判断当前App是在前台还是在后台

    Android判断当前App是在前台还是在后台

    这篇文章主要为大家详细介绍了Android判断当前App是在前台还是在后台的方法,感兴趣的小伙伴们可以参考一下
    2016-08-08

最新评论