前面寫過了使用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布局)
<?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"><ImageSwitcherandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/is"></ImageSwitcher><LinearLayoutandroid:id="@+id/point_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:orientation="horizontal"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:src="@mipmap/default_holo"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:src="@mipmap/default_holo"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:src="@mipmap/default_holo"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:src="@mipmap/default_holo"/></LinearLayout></FrameLayout>
這裡大家也可以通過設定檔來布局下面的導航圓點,不必寫死在布局檔案中。
第二步:Java中功能實現代碼MainActivity.java
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;//手指離開螢幕時的座標(示範左右滑動)@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);is = (ImageSwitcher) findViewById(R.id.is);is.setFactory(this);//通過工廠實現ImageSwitcherinitpoint();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的屬性)@Overridepublic View makeView() {//執行個體化一個用於切換的ImageView視圖ImageView iv = new ImageView(this);//預設展示的第一個視圖為images[0]iv.setImageResource(images[0]);return iv;}@Overridepublic 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實現起來,還是簡單了很多。
以上所述是小編給大家介紹的Android中使用imageviewswitcher 實現圖片切換輪播導航的方法,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!