ANDROID中使用IMAGEVIEWSWITCHER實現 圖片切換 輪播導航效果

http://www.cnblogs.com/panhouye/p/6132402.html
前面寫過了使用ViewFlippe r和ViewPager實現屏幕中視圖切換的效果(ViewPager未實現輪播)附鏈接:

今天我們在換一種實現方式ImageViewSwitcher
ImageSwitcher是Android中控製圖片展示效果的一個控件,如:幻燈片效果
ImageSwitcher粗略的理解就是ImageView的選擇器。
ImageSwitcher的原理:ImageSwitcher有兩個子View:ImageView,當左右滑動的時候,就在這兩個ImageView之間來回切換來顯示圖片。
既然有兩個子ImageView,那麼我們要創建兩個ImageView給ImageSwitcher。創建ImageViewSwitcher中的ImageView是通過ViewFactory工廠來實現的。
下面我們展示下本次實現效果(可以輪播哦)
个人感觉,就图片切换轮播来讲,ImageViewSwitcher相对于ViewFlipper和ViewPager实现起来,还是简单了很多。大家可以谈谈自己的看法,欢迎留言讨论。

<?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>

=============================================================

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實現起來,還是簡單了很多。

https://www.jianshu.com/p/c052ae42fbc9


http://blog.csdn.net/stevenhu_223/article/details/45577781

https://www.jianshu.com/p/24b30a3d052f



留言

這個網誌中的熱門文章

Use Case Description(描述使用案例)

列出不重複的隨機亂數

子類別建構子super觀念