• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > Android中使用ImageViewSwitcher实现图片切换轮播导航效果,

Android中使用ImageViewSwitcher实现图片切换轮播导航效果,

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了android imageview,android圆形imageview,android中imageview,android圆角imageview,android设置imageview等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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布局)

 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     <LinearLayout
14         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         <ImageView
20             android:layout_width="wrap_content"
21             android:layout_height="wrap_content"
22             android:layout_weight="1"
23             android:src="@mipmap/default_holo"/>
24         <ImageView
25             android:layout_width="wrap_content"
26             android:layout_height="wrap_content"
27             android:layout_weight="1"
28             android:src="@mipmap/default_holo"/>
29         <ImageView
30             android:layout_width="wrap_content"
31             android:layout_height="wrap_content"
32             android:layout_weight="1"
33             android:src="@mipmap/default_holo"/>
34         <ImageView
35             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     @Override
25     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);//通过工厂实现ImageSwitcher
30         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);



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Android中使用ImageViewSwitcher实现图片切换轮播导航效果,
  • android 圆角ImageView类,可设置弧度,androidimageview
  • 回收ImageView占用的图像内存,imageview占用图像
  • android:ImageView选择本地图片并显示

相关文章

  • 2017-05-26flexboxlayout,flexbox
  • 2017-05-26android studio快捷键,androidstudio
  • 2017-05-26Android SDK下载和更新失败的解决方法!!!,androidsdk
  • 2017-05-26Android 动画资源 详解
  • 2017-05-26ViewPager结合Fragment进行无限滑动,viewpagerfragment
  • 2017-05-26新闻客户端应用项目源码,客户端项目源码
  • 2017-05-26Android 手机卫士--9patch图,
  • 2017-05-26Android中button点击后字体的变色效果,androidbutton
  • 2017-05-26自定义控件添加自定义属性问题,控件添加自定义属性
  • 2017-08-23关于 android canvas 的简单总结

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Android热补丁技术—dexposed原理简析(手机淘宝采用方案)
    • 解决关于 在android studio 出现的 DELETE_FAILED_INTERNAL_ERROR Error while Installing APK 问题,whileinstallingapk
    • Andoid自定义View系统学习参考,andoidview
    • nginx设置泛域名解析的https证书过程
    • 深入了解Volley如何执行一个Request的流程
    • 解决VS2017不连接visual studio emulator for android,vs2017android
    • 切换横竖屏的时候Activity的生命周期变化情况,activity生命周期
    • 一个特别适合新手练习的Android小项目——每日一妹纸
    • 如何让光标处于EditText的末尾,光标edittext末尾
    • Android.mk模板(持续更新中),android.mk更新中

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有