亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android仿IOS ViewPager滑動(dòng)進(jìn)度條

 更新時(shí)間:2017年01月21日 15:40:33   作者:shineflowers  
這篇文章主要為大家詳細(xì)介紹了Android仿IOS ViewPager滑動(dòng)進(jìn)度條的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近做項(xiàng)目,碰到如下的需求:ViewPager分頁,如果是6頁(包括6頁)就用圓點(diǎn),如果是6頁以上就用進(jìn)度條來切換。前面一種交互方法最常見,用小圓點(diǎn)來表示當(dāng)前選中的頁面,這些小圓點(diǎn)稱為導(dǎo)航點(diǎn),很多App都是這種實(shí)現(xiàn)方式。當(dāng)用戶第一次安裝或升級(jí)應(yīng)用時(shí),都會(huì)利用導(dǎo)航頁面告訴用戶當(dāng)前版本的主要亮點(diǎn),一般情況下當(dāng)行頁面有三部分組成,背景圖片,導(dǎo)航文字和滑動(dòng)的原點(diǎn),即下面的效果:

這里就不作詳細(xì)的講解,大家可以參考我以前寫過的博客:
ViewPager實(shí)現(xiàn)圖片輪翻效果
今天來實(shí)現(xiàn)ViewPager進(jìn)度條切換,主要邏輯如下:
MainActivity.java

package com.jackie.slidebarviewdemo.activity; 
 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.TextView; 
 
import com.jackie.slidebarviewdemo.R; 
import com.jackie.slidebarviewdemo.widget.SlideBarView; 
 
public class MainActivity extends AppCompatActivity { 
  private SlideBarView mSlideBarView; 
  private TextView mTextView; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
 
    mSlideBarView = (SlideBarView) findViewById(R.id.slide_bar); 
    mTextView = (TextView) findViewById(R.id.text_view); 
 
    mSlideBarView.setTotalPage(80); 
    mSlideBarView.setOnSlideChangeListener(new SlideBarView.OnSlideChangeListener() { 
      @Override 
      public void onSlideChange(int page) { 
        mTextView.setText("當(dāng)前是第" + page + "頁"); 
      } 
    }); 
  } 
} 

SlideBarView.java

package com.jackie.slidebarviewdemo.widget; 
 
import android.content.Context; 
import android.util.AttributeSet; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.LinearLayout; 
import android.widget.PopupWindow; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
 
import com.jackie.slidebarviewdemo.R; 
import com.jackie.slidebarviewdemo.utils.ConvertUtils; 
 
/** 
 * Created by Jackie on 2017/1/17. 
 */ 
 
public class SlideBarView extends RelativeLayout { 
  private LayoutInflater mInflater; 
 
  private RelativeLayout mSlideBarView; 
  private View mSlideBarBlock; 
 
  private PopupWindow mPopupWindow; 
  private TextView mPopupText; 
 
  private int mDp40; 
 
  private String mBound = "no"; // no表示沒到邊界,left為到左邊界了,right表示到右邊界了 
 
  public interface OnSlideChangeListener { 
    void onSlideChange(int page); 
  } 
 
  private OnSlideChangeListener mOnSlideChangeListener; 
  public void setOnSlideChangeListener(OnSlideChangeListener onSlideChangeListener) { 
    this.mOnSlideChangeListener = onSlideChangeListener; 
  } 
 
  public SlideBarView(Context context) { 
    this(context, null); 
  } 
 
  public SlideBarView(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
  } 
 
  public SlideBarView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
 
    init(context); 
    initEvent(); 
  } 
 
  private void init(Context context) { 
    mInflater = LayoutInflater.from(context); 
    View slideBar = mInflater.inflate(R.layout.slide_bar, null); 
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
    addView(slideBar, params); 
 
    mSlideBarView = (RelativeLayout) slideBar.findViewById(R.id.slide_bar_view); 
    mSlideBarBlock = slideBar.findViewById(R.id.slide_bar_block); 
 
    mDp40 = ConvertUtils.dip2px(context, 40); 
  } 
 
  private void initEvent() { 
    mSlideBarView.setOnTouchListener(new OnTouchListener() { 
      int currentX = 0; 
      int startX = 0; 
 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
        switch (event.getAction()) { 
          case MotionEvent.ACTION_DOWN: 
            currentX = (int) event.getX(); 
            startX = (int) event.getX(); 
 
            // 設(shè)置滑塊的滑動(dòng), 手指第一次點(diǎn)下去把滑塊放到手指上 
            int downLeft = currentX - mSlideBarBlock.getMeasuredWidth() / 2; 
            int downTop = mSlideBarBlock.getTop(); 
            int downRight = downLeft + mSlideBarBlock.getWidth(); 
            int downBottom = mSlideBarBlock.getBottom(); 
 
            //邊界檢測(cè) 
            if (downLeft < 0) { 
              downLeft = 0; 
              downRight = mSlideBarBlock.getMeasuredWidth(); 
            } else if (downRight > mSlideBarView.getMeasuredWidth()) { 
              downLeft = mSlideBarView.getMeasuredWidth() - mSlideBarBlock.getMeasuredWidth(); 
              downRight = mSlideBarView.getMeasuredWidth(); 
            } 
 
            mSlideBarBlock.layout(downLeft, downTop, downRight, downBottom); 
            break; 
          case MotionEvent.ACTION_MOVE: 
            currentX = (int) event.getX(); 
            int currentPage = currentX * mTotalPage / mSlideBarView.getMeasuredWidth(); 
            if (currentPage < 0) { 
              currentPage = 0; 
            } else if (currentPage > mTotalPage) { 
              currentPage = mTotalPage; 
            } 
 
            // 設(shè)置滑塊的滑動(dòng) 
            int moveLeft = currentX - mSlideBarBlock.getMeasuredWidth() / 2; 
            int moveTop = mSlideBarBlock.getTop(); 
            int moveRight = moveLeft + mSlideBarBlock.getMeasuredWidth(); 
            int moveBottom = mSlideBarBlock.getBottom(); 
 
            //邊界處理 
            if (moveLeft < 0) { 
              mBound = "left"; 
 
              moveLeft = 0; 
              moveRight = mSlideBarBlock.getMeasuredWidth(); 
            } else if (moveRight >= mSlideBarView.getMeasuredWidth()) { 
              mBound = "right"; 
 
              moveLeft = mSlideBarView.getMeasuredWidth() - mSlideBarBlock.getMeasuredWidth(); 
              moveRight = mSlideBarView.getMeasuredWidth(); 
            } else { 
              mBound = "no"; 
            } 
 
            mSlideBarBlock.layout(moveLeft, moveTop, moveRight, moveBottom); 
            startX = currentX; 
 
            //設(shè)置popupWindow的彈出位置 
            if (mOnSlideChangeListener != null) { 
              if (currentPage == mTotalPage) { 
                //防止ViewPager越界 
                currentPage = mTotalPage - 1; 
              } 
 
              mOnSlideChangeListener.onSlideChange(currentPage); 
 
              if (mPopupWindow != null) { 
                mPopupText.setText(currentPage + ""); 
 
                //設(shè)置PopupWindow的滑動(dòng) 
                if (!mPopupWindow.isShowing()) { 
                  int[] location = new int[2]; 
                  mSlideBarView.getLocationInWindow(location); 
                  mPopupWindow.showAsDropDown(mSlideBarView, currentX, location[1] - mDp40); 
                } else { 
                  if ("no".equals(mBound)) { 
                    int[] location = new int[2] ; 
                    mSlideBarView.getLocationInWindow(location); 
                    mPopupWindow.update(currentX, location[1] - mDp40, mPopupWindow.getWidth(), mPopupWindow.getHeight(), true); 
                  } 
                } 
              } 
            } 
            break; 
          case MotionEvent.ACTION_UP: 
            currentX = 0; 
            startX = 0; 
            mPopupWindow.dismiss(); 
            break; 
        } 
 
        return true; 
      } 
    }); 
 
    // 初始化PopupWindow 
    View contentView = mInflater.inflate(R.layout.popup_window, null); 
    mPopupText = (TextView) contentView.findViewById(R.id.popup_text); 
    mPopupWindow = new PopupWindow(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    mPopupWindow.setContentView(contentView); 
    mPopupWindow.setOutsideTouchable(true); 
    mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.popup_window_bg)); 
    mPopupWindow.setAnimationStyle(0); 
  } 
 
  int mTotalPage = 0; 
  public void setTotalPage(int totalPage) { 
    this.mTotalPage = totalPage; 
  } 
} 

相關(guān)的單位轉(zhuǎn)化工具,大家可以拷貝到自己的項(xiàng)目中直接使用。
ConvertUtils.java

package com.jackie.slidebarviewdemo.utils; 
 
import android.content.Context; 
 
public class ConvertUtils { 
  public static int dip2px(Context context, float dpValue) { 
    final float scale = context.getResources().getDisplayMetrics().density; 
    return (int) (dpValue * scale + 0.5f); 
  } 
 
  public static int px2dip(Context context, float pxValue) { 
    final float scale = context.getResources().getDisplayMetrics().density; 
    return (int) (pxValue / scale + 0.5f); 
  } 
   
  public static int px2sp(Context context, float pxValue) { 
    final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;  
    return (int) (pxValue / fontScale + 0.5f);  
  }  
   
  public static int sp2px(Context context, float spValue) { 
    final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;  
    return (int) (spValue * fontScale + 0.5f);  
  } 
} 

自定義組合控件,然后實(shí)現(xiàn)相關(guān)的手勢(shì),思路很清晰,代碼也很詳細(xì),這里就直接貼代碼了。
activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:orientation="vertical"> 
 
    <com.jackie.slidebarviewdemo.widget.SlideBarView 
      android:id="@+id/slide_bar" 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:layout_marginLeft="20dp" 
      android:layout_marginRight="20dp"/> 
 
    <TextView 
      android:id="@+id/text_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginTop="20dp" 
      android:textColor="#000" 
      android:textSize="20dp" 
      android:text="當(dāng)前是第0頁"/> 
  </LinearLayout> 
</RelativeLayout> 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
 
  <RelativeLayout 
    android:id="@+id/slide_bar_view" 
    android:layout_width="match_parent" 
    android:layout_height="50dp"> 
 
    <View 
      android:layout_width="match_parent" 
      android:layout_height="5dp" 
      android:layout_centerInParent="true" 
      android:background="@drawable/shape_slide_bar_bg"/> 
 
    <View 
      android:id="@+id/slide_bar_block" 
      android:layout_width="20dp" 
      android:layout_height="14dp" 
      android:background="#b9b9b9" 
      android:layout_centerVertical="true" /> 
  </RelativeLayout> 
</RelativeLayout> 

popup_window.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
  <RelativeLayout 
    android:layout_width="30dp" 
    android:layout_height="30dp"> 
    <TextView 
      android:id="@+id/popup_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#fff" 
      android:textSize="16dp" 
      android:gravity="center" 
      android:layout_centerInParent="true" /> 
  </RelativeLayout> 
</RelativeLayout> 

附上相關(guān)的資源文件:
shape_slide_bar_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape="rectangle"> 
  <solid android:color="#dcdcdc" /> 
  <corners android:radius="1dp"/> 
</shape> 

popup_window_bg.9.png


效果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android使用自定義View實(shí)現(xiàn)橫行時(shí)間軸效果

    Android使用自定義View實(shí)現(xiàn)橫行時(shí)間軸效果

    這篇文章主要給大家介紹了關(guān)于Android使用自定義View實(shí)現(xiàn)橫行時(shí)間軸效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • flutter 屏幕尺寸適配和字體大小適配的實(shí)現(xiàn)

    flutter 屏幕尺寸適配和字體大小適配的實(shí)現(xiàn)

    這篇文章主要介紹了flutter 屏幕尺寸適配和字體大小適配的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Android開發(fā)筆記之:對(duì)實(shí)踐TDD的一些建議說明

    Android開發(fā)筆記之:對(duì)實(shí)踐TDD的一些建議說明

    本篇文章是對(duì)Android中實(shí)踐TDD的一些建議進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Android實(shí)現(xiàn)自動(dòng)朗讀功能(TTS)

    Android實(shí)現(xiàn)自動(dòng)朗讀功能(TTS)

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自動(dòng)朗讀功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Android編程實(shí)現(xiàn)popupwindow定時(shí)消失的方法

    Android編程實(shí)現(xiàn)popupwindow定時(shí)消失的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)popupwindow定時(shí)消失的方法,結(jié)合實(shí)例形式分析了Android使用定時(shí)器實(shí)現(xiàn)popupwindow定時(shí)消失的相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Android 實(shí)現(xiàn)背景圖和狀態(tài)欄融合方法

    Android 實(shí)現(xiàn)背景圖和狀態(tài)欄融合方法

    下面小編就為大家分享一篇Android 實(shí)現(xiàn)背景圖和狀態(tài)欄融合方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • android列表控件實(shí)現(xiàn)展開、收縮功能

    android列表控件實(shí)現(xiàn)展開、收縮功能

    這篇文章主要為大家詳細(xì)介紹了android支持展開/收縮功能的列表控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android控件之ProgressBar用法實(shí)例分析

    Android控件之ProgressBar用法實(shí)例分析

    這篇文章主要介紹了Android控件之ProgressBar用法,以一個(gè)完整實(shí)例形式較為詳細(xì)的分析了ProgressBar控件操作進(jìn)度顯示的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • 利用Kotlin Tools如何快速添加Kotlin依賴詳解

    利用Kotlin Tools如何快速添加Kotlin依賴詳解

    這篇文章主要給大家介紹了關(guān)于利用Kotlin Tools如何快速添加Kotlin依賴的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • Android實(shí)現(xiàn)下載工具的簡(jiǎn)單代碼

    Android實(shí)現(xiàn)下載工具的簡(jiǎn)單代碼

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)下載工具的簡(jiǎn)單代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03

最新評(píng)論