Android中使用ImageViewSwitcher實現圖片切換輪播導航效果

前面寫過了使用ViewFlipper和ViewPager實現屏幕中視圖切換的效果(ViewPager未實現輪播)附連接:html

Android中使用ViewFlipper實現屏幕切換java

Android中使用ViewPager實現屏幕頁面切換和頁面切換效果android

 

今天咱們在換一種實現方式ImageViewSwitcher數組

ImageSwitcher是Android中控制圖片展現效果的一個控件,如:幻燈片效果app

ImageSwitcher粗略的理解就是ImageView的選擇器。ide

ImageSwitcher的原理:ImageSwitcher有兩個子View:ImageView,當左右滑動的時候,就在這兩個ImageView之間來回切換來顯示圖片。佈局

既然有兩個子ImageView,那麼咱們要建立兩個ImageView給ImageSwitcher。建立ImageViewSwitcher中的ImageView是經過ViewFactory工廠來實現的。動畫

下面咱們展現下本次實現效果(能夠輪播哦):this

好了,廢話很少說,開始擼代碼:spa

第一步: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實現起來,仍是簡單了不少。你們能夠談談本身的見解,歡迎留言討論。

相關文章
相關標籤/搜索