Android中使用imageviewswitcher 實(shí)現(xiàn)圖片切換輪播導(dǎo)航的方法
前面寫(xiě)過(guò)了使用ViewFlipper和ViewPager實(shí)現(xiàn)屏幕中視圖切換的效果(ViewPager未實(shí)現(xiàn)輪播)附鏈接:
ANDROID中使用VIEWFLIPPER類(lèi)實(shí)現(xiàn)屏幕切換(關(guān)于坐標(biāo)軸的問(wèn)題已補(bǔ)充更改)
Android 中使用 ViewPager實(shí)現(xiàn)屏幕頁(yè)面切換和頁(yè)面輪播效果
今天我們?cè)趽Q一種實(shí)現(xiàn)方式ImageViewSwitcher。
ImageSwitcher是Android中控制圖片展示效果的一個(gè)控件,如:幻燈片效果
ImageSwitcher粗略的理解就是ImageView的選擇器。
ImageSwitcher的原理:ImageSwitcher有兩個(gè)子View:ImageView,當(dāng)左右滑動(dòng)的時(shí)候,就在這兩個(gè)ImageView之間來(lái)回切換來(lái)顯示圖片。
既然有兩個(gè)子ImageView,那么我們要?jiǎng)?chuàng)建兩個(gè)ImageView給ImageSwitcher。創(chuàng)建ImageViewSwitcher中的ImageView是通過(guò)ViewFactory工廠來(lái)實(shí)現(xiàn)的。
下面我們展示下本次實(shí)現(xiàn)效果(可以輪播哦):

好了,廢話不多說(shuō),開(kāi)始擼代碼:
第一步:Layout中建立主布局(FrameLayout)文件activity_main.xml(包含導(dǎo)航原點(diǎn)的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>
這里大家也可以通過(guò)配置文件來(lái)布局下面的導(dǎo)航圓點(diǎn),不必寫(xiě)死在布局文件中。
第二步:Java中功能實(shí)現(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;//聲明導(dǎo)航圓點(diǎn)的布局
//圖片id數(shù)組
int[] images={R.mipmap.a1,R.mipmap.a2,R.mipmap.a3,R.mipmap.a4};
//實(shí)例化存儲(chǔ)導(dǎo)航圓點(diǎn)的集合
ArrayList<ImageView> points = new ArrayList<>();
int index;//聲明index,記錄圖片id數(shù)組下標(biāo)
float startX;//手指接觸屏幕時(shí)X的坐標(biāo)(演示左右滑動(dòng))
float endX;//手指離開(kāi)屏幕時(shí)的坐標(biāo)(演示左右滑動(dòng))
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
is = (ImageSwitcher) findViewById(R.id.is);
is.setFactory(this);//通過(guò)工廠實(shí)現(xiàn)ImageSwitcher
initpoint();
is.setOnTouchListener(this);//設(shè)置觸摸事件
}
//初始化導(dǎo)航圓點(diǎn)的方法
private void initpoint() {
point_layout= (LinearLayout) findViewById(R.id.point_layout);
int count = point_layout.getChildCount();//獲取布局中圓點(diǎn)數(shù)量
for(int i =0;i<count;i++){
//將布局中的圓點(diǎn)加入到圓點(diǎn)集合中
points.add((ImageView) point_layout.getChildAt(i));
}
//設(shè)置第一張圖片(也就是圖片數(shù)組的0下標(biāo))的圓點(diǎn)狀態(tài)為觸摸實(shí)心狀態(tài)
points.get(0).setImageResource(R.mipmap.touched_holo);
}
//設(shè)選中圖片對(duì)應(yīng)的導(dǎo)航原點(diǎn)的狀態(tài)
public void setImageBackground(int selectImage) {
for(int i=0;i<points.size();i++){
//如果選中圖片的下標(biāo)等于圓點(diǎn)集合中下標(biāo)的id,則改變圓點(diǎn)狀態(tài)
if(i==selectImage){
points.get(i).setImageResource(R.mipmap.touched_holo);
}else{
points.get(i).setImageResource(R.mipmap.default_holo);
}
}
}
//實(shí)現(xiàn)ViewFactory的方法實(shí)例化imageView(這里未設(shè)置ImageView的屬性)
@Override
public View makeView() {
//實(shí)例化一個(gè)用于切換的ImageView視圖
ImageView iv = new ImageView(this);
//默認(rèn)展示的第一個(gè)視圖為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();//獲取按下屏幕時(shí)X軸的坐標(biāo)
//手指抬起
}else if (event.getAction()==MotionEvent.ACTION_UP){
endX=event.getX();
//判斷結(jié)束坐標(biāo)大于起始坐標(biāo)則為下一張(為避免誤操作,設(shè)置30的判斷區(qū)間)
if(startX-endX>30){
//三目運(yùn)算判斷當(dāng)前圖片已經(jīng)為最后一張,則從頭開(kāi)始
index = index+1<images.length?++index:0;
//使用系統(tǒng)自帶的切換出入動(dòng)畫(huà)效果(也可以向ViewFlipper中一樣自定義動(dòng)畫(huà)效果)
is.setInAnimation(this,android.R.anim.fade_in);
is.setOutAnimation(this,android.R.anim.fade_out);
//判斷結(jié)束坐標(biāo)小于于起始坐標(biāo)則為上一張(為避免誤操作,設(shè)置30的判斷區(qū)間)
}else if(endX-startX>30){
//三目運(yùn)算判斷當(dāng)前圖片已經(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);
}
//設(shè)置ImageSwitcher的圖片資源
is.setImageResource(images[index]);
//調(diào)用方法設(shè)置圓點(diǎn)對(duì)應(yīng)狀態(tài)
setImageBackground(index);
}
return true;
}
}
個(gè)人感覺(jué),就圖片切換輪播來(lái)講,ImageViewSwitcher相對(duì)于ViewFlipper和ViewPager實(shí)現(xiàn)起來(lái),還是簡(jiǎn)單了很多。
以上所述是小編給大家介紹的Android中使用imageviewswitcher 實(shí)現(xiàn)圖片切換輪播導(dǎo)航的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android開(kāi)發(fā)實(shí)現(xiàn)圖片切換APP
- Android UI控件之ImageSwitcher實(shí)現(xiàn)圖片切換效果
- Android自定義ImageView實(shí)現(xiàn)點(diǎn)擊兩張圖片切換效果
- Android使用ViewFlipper實(shí)現(xiàn)圖片切換功能
- Android 圖片切換器(dp、sp、px) 的單位轉(zhuǎn)換器
- Android控件ImageSwitcher實(shí)現(xiàn)左右圖片切換功能
- Android自定義ViewPager實(shí)現(xiàn)個(gè)性化的圖片切換效果
- Android中ViewPager組件的基本用法及實(shí)現(xiàn)圖片切換的示例
- Android基于ImageSwitcher實(shí)現(xiàn)圖片切換功能
- android實(shí)現(xiàn)點(diǎn)擊按鈕控制圖片切換
相關(guān)文章
解決genymotion模擬器無(wú)法聯(lián)網(wǎng)的正確方法100%成功
android 5.1版不能聯(lián)網(wǎng),三個(gè)步驟的設(shè)置就可以解決你的genymotion模擬器無(wú)法聯(lián)網(wǎng)的問(wèn)題2018-03-03
Android實(shí)現(xiàn)朋友圈點(diǎn)贊列表
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)朋友圈點(diǎn)贊列表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android數(shù)據(jù)雙向綁定原理實(shí)現(xiàn)和應(yīng)用場(chǎng)景
本文介紹了Android數(shù)據(jù)雙向綁定的原理和實(shí)現(xiàn)方式,包括基于觀察者模式和數(shù)據(jù)綁定框架的實(shí)現(xiàn)方法,以及應(yīng)用場(chǎng)景和優(yōu)缺點(diǎn)的分析,幫助開(kāi)發(fā)者了解和應(yīng)用數(shù)據(jù)雙向綁定技術(shù),提升應(yīng)用的交互性和響應(yīng)速度2023-04-04
Android?WebView升級(jí)詳細(xì)操作指南
Android的WebView差異化很?chē)?yán)重,下面這篇文章主要給大家介紹了關(guān)于Android?WebView升級(jí)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-07-07
Android編程顯示網(wǎng)絡(luò)上的圖片實(shí)例詳解
這篇文章主要介紹了Android編程顯示網(wǎng)絡(luò)上的圖片,結(jié)合實(shí)例形式詳細(xì)分析了Android顯示網(wǎng)絡(luò)圖片的流程與具體操作技巧,需要的朋友可以參考下2016-10-10
Android 自定義Dialog去除title導(dǎo)航欄的解決方法
今天小編就為大家分享一篇Android 自定義Dialog去除title導(dǎo)航欄的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android端權(quán)限隱私的合規(guī)化處理實(shí)戰(zhàn)記錄
大家應(yīng)該都發(fā)現(xiàn)了,現(xiàn)在很多應(yīng)用市場(chǎng)都要求應(yīng)用上架需要用戶協(xié)議,這篇文章主要給大家介紹了關(guān)于Android端權(quán)限隱私合規(guī)化處理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08

