Android中使用imageviewswitcher 實現(xiàn)圖片切換輪播導航的方法
前面寫過了使用ViewFlipper和ViewPager實現(xiàn)屏幕中視圖切換的效果(ViewPager未實現(xiàn)輪播)附鏈接:
ANDROID中使用VIEWFLIPPER類實現(xiàn)屏幕切換(關于坐標軸的問題已補充更改)
Android 中使用 ViewPager實現(xiàn)屏幕頁面切換和頁面輪播效果
今天我們在換一種實現(xiàn)方式ImageViewSwitcher。
ImageSwitcher是Android中控制圖片展示效果的一個控件,如:幻燈片效果
ImageSwitcher粗略的理解就是ImageView的選擇器。
ImageSwitcher的原理:ImageSwitcher有兩個子View:ImageView,當左右滑動的時候,就在這兩個ImageView之間來回切換來顯示圖片。
既然有兩個子ImageView,那么我們要創(chuàng)建兩個ImageView給ImageSwitcher。創(chuàng)建ImageViewSwitcher中的ImageView是通過ViewFactory工廠來實現(xiàn)的。
下面我們展示下本次實現(xiàn)效果(可以輪播哦):
好了,廢話不多說,開始擼代碼:
第一步: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"> <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>
這里大家也可以通過配置文件來布局下面的導航圓點,不必寫死在布局文件中。
第二步:Java中功能實現(xiàn)代碼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數(shù)組 int[] images={R.mipmap.a1,R.mipmap.a2,R.mipmap.a3,R.mipmap.a4}; //實例化存儲導航圓點的集合 ArrayList<ImageView> points = new ArrayList<>(); int index;//聲明index,記錄圖片id數(shù)組下標 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);//通過工廠實現(xiàn)ImageSwitcher initpoint(); is.setOnTouchListener(this);//設置觸摸事件 } //初始化導航圓點的方法 private void initpoint() { point_layout= (LinearLayout) findViewById(R.id.point_layout); int count = point_layout.getChildCount();//獲取布局中圓點數(shù)量 for(int i =0;i<count;i++){ //將布局中的圓點加入到圓點集合中 points.add((ImageView) point_layout.getChildAt(i)); } //設置第一張圖片(也就是圖片數(shù)組的0下標)的圓點狀態(tài)為觸摸實心狀態(tài) points.get(0).setImageResource(R.mipmap.touched_holo); } //設選中圖片對應的導航原點的狀態(tài) public void setImageBackground(int selectImage) { for(int i=0;i<points.size();i++){ //如果選中圖片的下標等于圓點集合中下標的id,則改變圓點狀態(tài) if(i==selectImage){ points.get(i).setImageResource(R.mipmap.touched_holo); }else{ points.get(i).setImageResource(R.mipmap.default_holo); } } } //實現(xiàn)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(); //判斷結(jié)束坐標大于起始坐標則為下一張(為避免誤操作,設置30的判斷區(qū)間) if(startX-endX>30){ //三目運算判斷當前圖片已經(jīng)為最后一張,則從頭開始 index = index+1<images.length?++index:0; //使用系統(tǒng)自帶的切換出入動畫效果(也可以向ViewFlipper中一樣自定義動畫效果) is.setInAnimation(this,android.R.anim.fade_in); is.setOutAnimation(this,android.R.anim.fade_out); //判斷結(jié)束坐標小于于起始坐標則為上一張(為避免誤操作,設置30的判斷區(qū)間) }else if(endX-startX>30){ //三目運算判斷當前圖片已經(jīng)為第一張,則上一張為數(shù)組內(nèi)最后一張圖片 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]); //調(diào)用方法設置圓點對應狀態(tài) setImageBackground(index); } return true; } }
個人感覺,就圖片切換輪播來講,ImageViewSwitcher相對于ViewFlipper和ViewPager實現(xiàn)起來,還是簡單了很多。
以上所述是小編給大家介紹的Android中使用imageviewswitcher 實現(xiàn)圖片切換輪播導航的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android開發(fā)實現(xiàn)圖片切換APP
- Android UI控件之ImageSwitcher實現(xiàn)圖片切換效果
- Android自定義ImageView實現(xiàn)點擊兩張圖片切換效果
- Android使用ViewFlipper實現(xiàn)圖片切換功能
- Android 圖片切換器(dp、sp、px) 的單位轉(zhuǎn)換器
- Android控件ImageSwitcher實現(xiàn)左右圖片切換功能
- Android自定義ViewPager實現(xiàn)個性化的圖片切換效果
- Android中ViewPager組件的基本用法及實現(xiàn)圖片切換的示例
- Android基于ImageSwitcher實現(xiàn)圖片切換功能
- android實現(xiàn)點擊按鈕控制圖片切換
相關文章
Android通過自定義Activity實現(xiàn)懸浮的Dialog詳解
這篇文章主要給大家介紹了關于Android通過自定義Activity實現(xiàn)懸浮的Dialog的相關資料,文中給出了詳細的示例代碼供大家參考學習,對大家具有一定的參考學習價值,感興趣的朋友們下面來一起看看吧。2017-05-05Android 使用fast-verification實現(xiàn)驗證碼填寫功能的實例代碼
這篇文章主要介紹了Android 使用fast-verification實現(xiàn)驗證碼填寫功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04RecyclerView Adapter輔助類詳解及示例代碼
本文主要介紹RecyclerView Adapter輔助類的知識,這里整理了詳細資料及簡單示例代碼,幫助大家學習這部分的內(nèi)容,有興趣的小伙伴可以參考下2016-09-09Android編程實現(xiàn)WebView添加進度條的方法
這篇文章主要介紹了Android編程實現(xiàn)WebView添加進度條的方法,涉及Android WebView界面及控件功能相關操作技巧,需要的朋友可以參考下2017-02-02Android實現(xiàn)ViewPager無限循環(huán)效果(二)
這篇文章主要為大家詳細介紹了Android實現(xiàn)ViewPager無限循環(huán)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05