zoukankan      html  css  js  c++  java
  • 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);
    52             }
    53         }
    54     }
    55     //实现ViewFactory的方法实例化imageView(这里未设置ImageView的属性)
    56     @Override
    57     public View makeView() {
    58         //实例化一个用于切换的ImageView视图
    59         ImageView iv = new ImageView(this);
    60         //默认展示的第一个视图为images[0]
    61         iv.setImageResource(images[0]);
    62         return iv;
    63     }
    64     @Override
    65     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实现起来,还是简单了很多。大家可以谈谈自己的看法,欢迎留言讨论。

  • 相关阅读:
    斜率优化dp学习
    拓扑排序
    P2486 [SDOI2011]染色 区间合并+树链剖分(加深对线段树的理解)
    网络流24题!!!!
    费用流板子
    网络流dinic板子
    小花梨的数组
    C. 小花梨判连通
    splay树
    hdu4467 graph
  • 原文地址:https://www.cnblogs.com/panhouye/p/6132402.html
Copyright © 2011-2022 走看看