Android中使用ImageViewSwitcher实现图片切换轮播导航效果
来源:互联网 发布:观为监测大数据 编辑:程序博客网 时间:2024/05/16 10:29
达人科技 2016-12-05 08:02
前面写过了使用ViewFlipper和ViewPager实现屏幕中视图切换的效果(ViewPager未实现轮播)附链接:
今天我们在换一种实现方式ImageViewSwitcher。
ImageSwitcher是Android中控制图片展示效果的一个控件,如:幻灯片效果
ImageSwitcher粗略的理解就是ImageView的选择器。
ImageSwitcher的原理:ImageSwitcher有两个子View:ImageView,当左右滑动的时候,就在这两个ImageView之间来回切换来显示图片。
既然有两个子ImageView,那么我们要创建两个ImageView给ImageSwitcher。创建ImageViewSwitcher中的ImageView是通过ViewFactory工厂来实现的。
下面我们展示下本次实现效果(可以轮播哦):
好了,废话不多说,开始撸代码:
第一步:Layout中建立主布局(FrameLayout)文件activity_main.xml(包含导航原点的LinearLayout布局)
1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/activity_main" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.example.administrator.switcher.MainActivity"> 8 <ImageSwitcher 9 android:layout_width="match_parent"10 android:layout_height="match_parent"11 android:id="@+id/is">12 </ImageSwitcher>13 <LinearLayout14 android:id="@+id/point_layout"15 android:layout_width="match_parent"16 android:layout_height="wrap_content"17 android:layout_gravity="bottom"18 android:orientation="horizontal">19 <ImageView20 android:layout_width="wrap_content"21 android:layout_height="wrap_content"22 android:layout_weight="1"23 android:src="@mipmap/default_holo"/>24 <ImageView25 android:layout_width="wrap_content"26 android:layout_height="wrap_content"27 android:layout_weight="1"28 android:src="@mipmap/default_holo"/>29 <ImageView30 android:layout_width="wrap_content"31 android:layout_height="wrap_content"32 android:layout_weight="1"33 android:src="@mipmap/default_holo"/>34 <ImageView35 android:layout_width="wrap_content"36 android:layout_height="wrap_content"37 android:layout_weight="1"38 android:src="@mipmap/default_holo"/>39 </LinearLayout>40 </FrameLayout>
这里大家也可以通过配置文件来布局下面的导航圆点,不必写死在布局文件中。
第二步:Java中功能实现代码MainActivity.java
1 import android.support.v7.app.AppCompatActivity; 2 import android.os.Bundle; 3 import android.view.MotionEvent; 4 import android.view.View; 5 import android.widget.ImageSwitcher; 6 import android.widget.ImageView; 7 import android.widget.LinearLayout; 8 import android.widget.ViewSwitcher; 9 import java.util.ArrayList;10 /**11 * Created by panchengjia on 2016/12/04.12 */13 public class MainActivity extends AppCompatActivity implements ViewSwitcher.ViewFactory,View.OnTouchListener{14 private ImageSwitcher is;//声明ImageSwitcher布局15 private LinearLayout point_layout;//声明导航圆点的布局16 //图片id数组17 int images={R.mipmap.a1,R.mipmap.a2,R.mipmap.a3,R.mipmap.a4};18 //实例化存储导航圆点的集合19 ArrayList<ImageView> points = new ArrayList<>;20 int index;//声明index,记录图片id数组下标21 float startX;//手指接触屏幕时X的坐标(演示左右滑动)22 float endX;//手指离开屏幕时的坐标(演示左右滑动)23 24 @Override25 protected void onCreate(Bundle savedInstanceState) {26 super.onCreate(savedInstanceState);27 setContentView(R.layout.activity_main);28 is = (ImageSwitcher) findViewById(R.id.is);29 is.setFactory(this);//通过工厂实现ImageSwitcher30 initpoint;31 is.setOnTouchListener(this);//设置触摸事件32 }33 //初始化导航圆点的方法34 private void initpoint {35 point_layout= (LinearLayout) findViewById(R.id.point_layout);36 int count = point_layout.getChildCount;//获取布局中圆点数量37 for(int i =0;i<count;i++){38 //将布局中的圆点加入到圆点集合中39 points.add((ImageView) point_layout.getChildAt(i));40 }41 //设置第一张图片(也就是图片数组的0下标)的圆点状态为触摸实心状态42 points.get(0).setImageResource(R.mipmap.touched_holo);43 }44 //设选中图片对应的导航原点的状态45 public void setImageBackground(int selectImage) {46 for(int i=0;i<points.size;i++){47 //如果选中图片的下标等于圆点集合中下标的id,则改变圆点状态48 if(i==selectImage){49 points.get(i).setImageResource(R.mipmap.touched_holo);50 }else{51 points.get(i).setImageResource(R.mipmap.default_holo);52 }53 }54 }55 //实现ViewFactory的方法实例化imageView(这里未设置ImageView的属性)56 @Override57 public View makeView {58 //实例化一个用于切换的ImageView视图59 ImageView iv = new ImageView(this);60 //默认展示的第一个视图为images[0]61 iv.setImageResource(images[0]);62 return iv;63 }64 @Override65 public boolean onTouch(View v, MotionEvent event) {66 //按下屏幕67 if(event.getAction==MotionEvent.ACTION_DOWN){68 startX=event.getX;//获取按下屏幕时X轴的坐标69 //手指抬起70 }else if (event.getAction==MotionEvent.ACTION_UP){71 endX=event.getX;72 //判断结束坐标大于起始坐标则为下一张(为避免误操作,设置30的判断区间)73 if(startX-endX>30){74 //三目运算判断当前图片已经为最后一张,则从头开始75 index = index+1<images.length?++index:0;76 //使用系统自带的切换出入动画效果(也可以向ViewFlipper中一样自定义动画效果)77 is.setInAnimation(this,android.R.anim.fade_in);78 is.setOutAnimation(this,android.R.anim.fade_out);79 80 //判断结束坐标小于于起始坐标则为上一张(为避免误操作,设置30的判断区间)81 }else if(endX-startX>30){82 //三目运算判断当前图片已经为第一张,则上一张为数组内最后一张图片83 index = index-1>=0?--index:images.length-1;84 is.setInAnimation(this,android.R.anim.fade_in);85 is.setOutAnimation(this,android.R.anim.fade_out);86 }87 //设置ImageSwitcher的图片资源88 is.setImageResource(images[index]);89 //调用方法设置圆点对应状态90 setImageBackground(index);91 }92 return true;93 }94 }
个人感觉,就图片切换轮播来讲,ImageViewSwitcher相对于ViewFlipper和ViewPager实现起来,还是简单了很多。大家可以谈谈自己的看法,欢迎留言讨论。
本文为头条号作者发布,不代表今日头条立场。
0 0
- Android中使用ImageViewSwitcher实现图片切换轮播导航效果
- Android中使用ImageViewSwitcher实现图片切换轮播导航效果
- JS图片轮播切换效果实现
- Android 控件之ImageViewSwitcher图片切换控件
- Android中使用GridView和ImageViewSwitcher实现电子相册简单功能
- Android中使用GridView和ImageViewSwitcher实现电子相册简单功能
- Android实现图片轮播切换
- Android中ViewPager的使用(二):实现图片轮播效果
- RollViewPager实现android图片轮播效果
- 使用Jquery实现图片轮播效果
- JS原生实现图片轮播切换效果
- 纯原生javascript实现图片轮播切换效果代码。
- Android中ConvenientBanner的使用--获取本地图片 --(实现效果是自动轮播图片)
- Android中ConvenientBanner的使用--获取网络图片 --(实现效果是自动轮播图片)
- bootstrap图片轮播+导航栏效果
- JQuery实现图片轮播切换效果--水平切换/垂直切换
- Android中ViewPager实现图片的定时轮播效果(设置Integer.MAX_VALUE)
- Android fragment中广告图片轮播效果的实现(附图 )
- vs连接oracle数据库
- execute、executeUpdate、executeQuery三者的区别(及返回值)
- 微信公众号开发工具类
- JVM:GC日志
- 单台服务器上的并发TCP连接数可以有多少
- Android中使用ImageViewSwitcher实现图片切换轮播导航效果
- 函数式宏定义与普通函数的区别?C语言宏定义和宏定义函数 ?
- Service服务Android四大组件之一
- Tomcat学习总结(13)—— Tomcat常用参数配置说明
- 设置条SettingItem再也不用重复写这些垃圾代码了
- 使用zxing生成二维码
- 主界面
- JDK动态代理实现原理
- java中四种引用类型(对象的强、软、弱和虚引用)