課程
/移動(dòng)開發(fā)
/Android
/Android高級(jí)特效-索引
謝謝分享,但是程序沒(méi)有講完整,最后的contains和getSectionByPoint兩個(gè)方法沒(méi)有講
2016-04-15
源自:Android高級(jí)特效-索引 3-2
正在回答
【蜘蛛俠TG@abin789】-6001【蜘蛛俠TG@abin789】-6002【蜘蛛俠TG@abin789】-6003【蜘蛛俠TG@abin789】-6004【蜘蛛俠TG@abin789】-6005【蜘蛛俠TG@abin789】-6006【蜘蛛俠TG@abin789】-6007【蜘蛛俠TG@abin789】-6008【蜘蛛俠TG@abin789】-6009【蜘蛛俠TG@abin789】-6010【蜘蛛俠TG@abin789】-6011【蜘蛛俠TG@abin789】-6012【蜘蛛俠TG@abin789】-6013【蜘蛛俠TG@abin789】-6014【蜘蛛俠TG@abin789】-6015【蜘蛛俠TG@abin789】-6016【蜘蛛俠TG@abin789】-6017【蜘蛛俠TG@abin789】-6018【蜘蛛俠TG@abin789】-6019【蜘蛛俠TG@abin789】-6020【蜘蛛俠TG@abin789】-6021【蜘蛛俠TG@abin789】-6022【蜘蛛俠TG@abin789】-6023【蜘蛛俠TG@abin789】-6024【蜘蛛俠TG@abin789】-6025【蜘蛛俠TG@abin789】-6026【蜘蛛俠TG@abin789】-6027【蜘蛛俠TG@abin789】-6028【蜘蛛俠TG@abin789】-6029【蜘蛛俠TG@abin789】-6030【蜘蛛俠TG@abin789】-6031【蜘蛛俠TG@abin789】-6032【蜘蛛俠TG@abin789】-6033【蜘蛛俠TG@abin789】-6034【蜘蛛俠TG@abin789】-6035【蜘蛛俠TG@abin789】-6036【蜘蛛俠TG@abin789】-6037【蜘蛛俠TG@abin789】-6038【蜘蛛俠TG@abin789】-6039【蜘蛛俠TG@abin789】-6040【蜘蛛俠TG@abin789】-6041【蜘蛛俠TG@abin789】-6042【蜘蛛俠TG@abin789】-6043【蜘蛛俠TG@abin789】-6044【蜘蛛俠TG@abin789】-6045【蜘蛛俠TG@abin789】-6046【蜘蛛俠TG@abin789】-6047【蜘蛛俠TG@abin789】-6048【蜘蛛俠TG@abin789】-6049【蜘蛛俠TG@abin789】-6050【蜘蛛俠TG@abin789】-6051【蜘蛛俠TG@abin789】-6052【蜘蛛俠TG@abin789】-6053【蜘蛛俠TG@abin789】-6054【蜘蛛俠TG@abin789】-6055【蜘蛛俠TG@abin789】-6056【蜘蛛俠TG@abin789】-6057【蜘蛛俠TG@abin789】-6058【蜘蛛俠TG@abin789】-6059【蜘蛛俠TG@abin789】-6060【蜘蛛俠TG@abin789】-6061【蜘蛛俠TG@abin789】-6062【蜘蛛俠TG@abin789】-6063【蜘蛛俠TG@abin789】-6064【蜘蛛俠TG@abin789】-6065【蜘蛛俠TG@abin789】-6066【蜘蛛俠TG@abin789】-6067【蜘蛛俠TG@abin789】-6068【蜘蛛俠TG@abin789】-6069【蜘蛛俠TG@abin789】-6070【蜘蛛俠TG@abin789】-6071【蜘蛛俠TG@abin789】-6072【蜘蛛俠TG@abin789】-6073【蜘蛛俠TG@abin789】-6074【蜘蛛俠TG@abin789】-6075【蜘蛛俠TG@abin789】-6076【蜘蛛俠TG@abin789】-6077【蜘蛛俠TG@abin789】-6078【蜘蛛俠TG@abin789】-6079【蜘蛛俠TG@abin789】-6080【蜘蛛俠TG@abin789】-6081【蜘蛛俠TG@abin789】-6082【蜘蛛俠TG@abin789】-6083【蜘蛛俠TG@abin789】-6084【蜘蛛俠TG@abin789】-6085【蜘蛛俠TG@abin789】-6086【蜘蛛俠TG@abin789】-6087【蜘蛛俠TG@abin789】-6088【蜘蛛俠TG@abin789】-6089【蜘蛛俠TG@abin789】-6090【蜘蛛俠TG@abin789】-6091【蜘蛛俠TG@abin789】-6092【蜘蛛俠TG@abin789】-6093【蜘蛛俠TG@abin789】-6094【蜘蛛俠TG@abin789】-6095【蜘蛛俠TG@abin789】-6096【蜘蛛俠TG@abin789】-6097【蜘蛛俠TG@abin789】-6098【蜘蛛俠TG@abin789】-6099【蜘蛛俠TG@abin789】-6100【蜘蛛俠TG@abin789】-6101【蜘蛛俠TG@abin789】-6102【蜘蛛俠TG@abin789】-6103【蜘蛛俠TG@abin789】-6104【蜘蛛俠TG@abin789】-6105【蜘蛛俠TG@abin789】-6106【蜘蛛俠TG@abin789】-6107【蜘蛛俠TG@abin789】-6108【蜘蛛俠TG@abin789】-6109【蜘蛛俠TG@abin789】-6110【蜘蛛俠TG@abin789】-6111【蜘蛛俠TG@abin789】-6112【蜘蛛俠TG@abin789】-6113【蜘蛛俠TG@abin789】-6114【蜘蛛俠TG@abin789】-6115【蜘蛛俠TG@abin789】-6116【蜘蛛俠TG@abin789】-6117【蜘蛛俠TG@abin789】-6118【蜘蛛俠TG@abin789】-6119【蜘蛛俠TG@abin789】-6120【蜘蛛俠TG@abin789】-6121【蜘蛛俠TG@abin789】-6122【蜘蛛俠TG@abin789】-6123【蜘蛛俠TG@abin789】-6124【蜘蛛俠TG@abin789】-6125【蜘蛛俠TG@abin789】-6126【蜘蛛俠TG@abin789】-6127【蜘蛛俠TG@abin789】-6128【蜘蛛俠TG@abin789】-6129【蜘蛛俠TG@abin789】-6130【蜘蛛俠TG@abin789】-6131【蜘蛛俠TG@abin789】-6132【蜘蛛俠TG@abin789】-6133【蜘蛛俠TG@abin789】-6134【蜘蛛俠TG@abin789】-6135【蜘蛛俠TG@abin789】-6136【蜘蛛俠TG@abin789】-6137【蜘蛛俠TG@abin789】-6138【蜘蛛俠TG@abin789】-6139【蜘蛛俠TG@abin789】-6140【蜘蛛俠TG@abin789】-6141【蜘蛛俠TG@abin789】-6142【蜘蛛俠TG@abin789】-6143【蜘蛛俠TG@abin789】-6144【蜘蛛俠TG@abin789】-6145【蜘蛛俠TG@abin789】-6146【蜘蛛俠TG@abin789】-6147【蜘蛛俠TG@abin789】-6148【蜘蛛俠TG@abin789】-6149【蜘蛛俠TG@abin789】-6150【蜘蛛俠TG@abin789】-6151【蜘蛛俠TG@abin789】-6152【蜘蛛俠TG@abin789】-6153【蜘蛛俠TG@abin789】-6154【蜘蛛俠TG@abin789】-6155【蜘蛛俠TG@abin789】-6156【蜘蛛俠TG@abin789】-6157【蜘蛛俠TG@abin789】-6158【蜘蛛俠TG@abin789】-6159【蜘蛛俠TG@abin789】-6160【蜘蛛俠TG@abin789】-6161【蜘蛛俠TG@abin789】-6162【蜘蛛俠TG@abin789】-6163【蜘蛛俠TG@abin789】-6164【蜘蛛俠TG@abin789】-6165【蜘蛛俠TG@abin789】-6166【蜘蛛俠TG@abin789】-6167【蜘蛛俠TG@abin789】-6168【蜘蛛俠TG@abin789】-6169【蜘蛛俠TG@abin789】-6170【蜘蛛俠TG@abin789】-6171【蜘蛛俠TG@abin789】-6172【蜘蛛俠TG@abin789】-6173【蜘蛛俠TG@abin789】-6174【蜘蛛俠TG@abin789】-6175【蜘蛛俠TG@abin789】-6176【蜘蛛俠TG@abin789】-6177【蜘蛛俠TG@abin789】-6178【蜘蛛俠TG@abin789】-6179【蜘蛛俠TG@abin789】-6180【蜘蛛俠TG@abin789】-6181【蜘蛛俠TG@abin789】-6182【蜘蛛俠TG@abin789】-6183【蜘蛛俠TG@abin789】-6184【蜘蛛俠TG@abin789】-6185【蜘蛛俠TG@abin789】-6186【蜘蛛俠TG@abin789】-6187【蜘蛛俠TG@abin789】-6188【蜘蛛俠TG@abin789】-6189【蜘蛛俠TG@abin789】-6190【蜘蛛俠TG@abin789】-6191【蜘蛛俠TG@abin789】-6192【蜘蛛俠TG@abin789】-6193【蜘蛛俠TG@abin789】-6194【蜘蛛俠TG@abin789】-6195【蜘蛛俠TG@abin789】-6196【蜘蛛俠TG@abin789】-6197【蜘蛛俠TG@abin789】-6198【蜘蛛俠TG@abin789】-6199【蜘蛛俠TG@abin789】-6200
package com.user.Widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.widget.Adapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
public class IndexScroller {
private Context mContext;
private ListView mListView = null;
private float mIndexbarWidth; // 索引條的寬度,
private float mIndexbarMargin; // 索引條離右側(cè)邊緣的距離
private float mPreviewPadding; // 點(diǎn)擊中間顯示的索引文本,距離四周的距離
private float mDensity; // 當(dāng)前屏幕密度除以160
private float mScaledDensity; // 當(dāng)前屏幕密度除以160(設(shè)置字體的尺寸)
private float mAlphaRate; // 索引條透明度(用來(lái)顯示和隱藏索引條)0-1
private int mState = STATE_HIDDEN; // 索引條當(dāng)前狀態(tài)
private int mListViewWidth; // 屏幕的寬度
private int mListViewHeight; // 屏幕的高度
private int mCurrentSection = -1; // 當(dāng)前所點(diǎn)擊的索引值
private boolean mIsIndexing = false;
private SectionIndexer mIndexer = null;
private String[] mSections;
private RectF mIdexbarRect; // 繪制索引條區(qū)域
public static final int STATE_HIDDEN = 0;
public static final int STATE_SHOWING = 1;
public static final int STATE_SHOW = 2;
public static final int STATE_HIDDENING = 3;
public IndexScroller(Context mContext, ListView mListView) {
this.mContext = mContext;
this.mListView = mListView;
// 獲取當(dāng)前屏幕的密度比值
mDensity = mContext.getResources().getDisplayMetrics().density;
// 獲取字體密度比值
mScaledDensity = mContext.getResources().getDisplayMetrics().scaledDensity;
setAdapter(mListView.getAdapter());
// 根據(jù)屏幕的 密度比值來(lái)調(diào)整索引條的寬度(單位:像素)
mIndexbarWidth = 20 * mDensity;
mIndexbarMargin = 10 * mDensity;
mPreviewPadding = 5 * mDensity;
}
public void setAdapter(Adapter adapter) {
if (adapter instanceof SectionIndexer) {
mIndexer = (SectionIndexer) adapter;
mSections = (String[]) mIndexer.getSections();
/**
* 繪制索引條和預(yù)覽文本
*?
* @param mPaint
*/
public void draw(Canvas mCanvas) {
// 1、繪制索引條和背景
// 2、繪制預(yù)覽文本和背景
if (mState == STATE_HIDDEN) {
return;
* 繪制索引條的區(qū)域
Paint mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setAlpha((int) (64 * mAlphaRate)); // 設(shè)置透明度,逐漸顯示
// 繪制索引條(四個(gè)角都是圓角區(qū)域)
mCanvas.drawRoundRect(mIdexbarRect, 5 * mDensity, 5 * mDensity, mPaint);
* 繪制預(yù)覽文本背景和文字
if (mSections != null && mSections.length > 0) {
// 繪制預(yù)覽文本背景和文字 , 大于0則點(diǎn)擊
if (mCurrentSection > 0) {
Paint previewPaint = new Paint(); // 繪制文本畫筆
previewPaint.setColor(Color.BLACK);
previewPaint.setAlpha(96);
Paint previewTextPaint = new Paint(); // 繪制文字畫筆
previewTextPaint.setColor(Color.WHITE);
previewTextPaint.setTextSize(50 * mScaledDensity);
// 獲取文本字體的寬度
float previewTextWidth = previewTextPaint
.measureText(mSections[mCurrentSection]);
// 獲得顯示文本的區(qū)域?qū)捀叨?= 上下間距padding + 字母往下超過(guò)基準(zhǔn)線的高度(正值) +
// 字母在基準(zhǔn)線的上面的高度(負(fù)值,負(fù)負(fù)得正)
float previewSize = 2 * mPreviewPadding
+ previewTextPaint.descent()
- previewTextPaint.ascent();
// 預(yù)覽文本區(qū)域所在位置
// 左=(屏幕寬度-顯示文本區(qū)域?qū)挾龋? 2 上= (屏幕高度-顯示文本區(qū)域高度)/ 2
RectF previewRectF = new RectF(
(mListViewWidth - previewSize) / 2,
(mListViewHeight - previewSize) / 2,
(mListViewWidth - previewSize) / 2 + previewSize,
(mListViewHeight - previewSize) / 2 + previewSize);
// 繪制預(yù)覽文本背景
mCanvas.drawRoundRect(previewRectF, 5 * mDensity, 5 * mDensity,
previewPaint);
// 繪制預(yù)覽文本的字體
mCanvas.drawText(
mSections[mCurrentSection],
(previewRectF.left + (previewSize - previewTextWidth)
/ 2 + 1),
(previewRectF.top + mPreviewPadding
- previewTextPaint.ascent() + 1),
previewTextPaint);
* 設(shè)置索引字母
// 設(shè)置繪制索引字母的屬性
Paint indexPaint = new Paint();
indexPaint.setColor(Color.WHITE);
indexPaint.setAlpha((int) (255 * mAlphaRate));
indexPaint.setTextSize(12 * mScaledDensity);
// 取得索引中每個(gè)字母的區(qū)域高度 = (索引區(qū)域高度 - 上下margin值 )/索引的長(zhǎng)度length
float sectionHeight = ((mIdexbarRect.height() - 2 * mIndexbarMargin) / mSections.length);
// 設(shè)置每個(gè)字母區(qū)域中的文字的padding值 = (每個(gè)字母區(qū)域 - 文字高度)/2
float sectionPaddingTop = ((sectionHeight - (indexPaint.descent() - indexPaint
.ascent())) / 2);
for (int i = 0; i < mSections.length; i++) {
// 取得繪制的文字的paddingLeft值
float sectionPaddingLeft = (mIndexbarWidth - indexPaint
.measureText(mSections[i])) / 2;
// 開始繪制
mCanvas.drawText(mSections[i],
(mIdexbarRect.left + sectionPaddingLeft), mIdexbarRect.top
+ mIndexbarMargin + sectionPaddingTop * i/* 索引字母區(qū)域 */
+ sectionPaddingTop - indexPaint.ascent(),
indexPaint);
* 保持最新的高度與寬度 重新加載索引條的區(qū)域
* @param w
* @param h
* @param oldw
* @param oldh
public void onSizeChanged(int w, int h, int oldw, int oldh) {
mListViewWidth = w;
mListViewHeight = h;
mIdexbarRect = new RectF(w - mIndexbarMargin - mIndexbarWidth,
mIndexbarMargin, w - mIndexbarMargin, h - mIndexbarMargin);
private void fade(long delay) {
// 0立即執(zhí)行,不延遲
// 清除消息
mHandler.removeMessages(0);
mHandler.sendEmptyMessageAtTime(0, SystemClock.uptimeMillis() + delay);
* 判定狀態(tài)來(lái)顯示,并移除消息message
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
switch (mState) {
case STATE_HIDDENING:
mAlphaRate -= mAlphaRate * 0.2;
if (mAlphaRate < 0.1) {
mAlphaRate = 0;
setState(STATE_HIDDEN);
mListView.invalidate();
fade(10);
break;
case STATE_HIDDEN:
setState(STATE_SHOWING);
case STATE_SHOWING:
mAlphaRate += (1 - mAlphaRate) * 0.2;
if (mAlphaRate > 0.9) {
mAlphaRate = 1;
setState(STATE_SHOW);
mListView.invalidate(); // listview不斷刷新,知道m(xù)AlphaRate > 0.9
case STATE_SHOW:
setState(STATE_HIDDENING);
};
* 改變狀態(tài)
* @param state
private void setState(int state) {
if (state < STATE_HIDDEN || state > STATE_HIDDENING) {
mState = state;
fade(0);
fade(3000); // 顯示的時(shí)間
* 管理觸摸索引條
* @param ev
* @return
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mState != STATE_HIDDEN && cantains(ev.getX(), ev.getY())) {
mIsIndexing = true;
mCurrentSection = getSectionByPoint(ev.getY());
mListView.setSelection(mIndexer
.getPositionForSection(mCurrentSection));
return true;
case MotionEvent.ACTION_MOVE:
if (mIsIndexing) {
if (cantains(ev.getX(), ev.getY())) {
case MotionEvent.ACTION_UP:
mIsIndexing = false;
mCurrentSection = -1;
if (mState == STATE_SHOW) {
return false;
* @param x
* @param y
private boolean cantains(float x, float y) {
return (x >= mIdexbarRect.left && y >= mIdexbarRect.top && y <= mIdexbarRect.top
+ mIdexbarRect.height());
private int getSectionByPoint(float y) {
if (mSections == null || mSections.length == 0)
return 0;
if (y < mIdexbarRect.top + mIndexbarMargin)
if (y >= mIdexbarRect.top + mIdexbarRect.height() - mIndexbarMargin)
return mSections.length - 1;
return (int) ((y - mIdexbarRect.top - mIndexbarMargin) / ((mIdexbarRect
.height() - 2 * mIndexbarMargin) / mSections.length));
* show()
public void show() {
if (mState == STATE_HIDDEN)
else if (mState == STATE_HIDDENING)
* hide()
public void hide() {
if (mState == STATE_SHOW)
舉報(bào)
APP中的熟面孔,索引列表的實(shí)現(xiàn),有效增加了查找的便捷度
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2022-04-01
【蜘蛛俠TG@abin789】-6001
【蜘蛛俠TG@abin789】-6002
【蜘蛛俠TG@abin789】-6003
【蜘蛛俠TG@abin789】-6004
【蜘蛛俠TG@abin789】-6005
【蜘蛛俠TG@abin789】-6006
【蜘蛛俠TG@abin789】-6007
【蜘蛛俠TG@abin789】-6008
【蜘蛛俠TG@abin789】-6009
【蜘蛛俠TG@abin789】-6010
【蜘蛛俠TG@abin789】-6011
【蜘蛛俠TG@abin789】-6012
【蜘蛛俠TG@abin789】-6013
【蜘蛛俠TG@abin789】-6014
【蜘蛛俠TG@abin789】-6015
【蜘蛛俠TG@abin789】-6016
【蜘蛛俠TG@abin789】-6017
【蜘蛛俠TG@abin789】-6018
【蜘蛛俠TG@abin789】-6019
【蜘蛛俠TG@abin789】-6020
【蜘蛛俠TG@abin789】-6021
【蜘蛛俠TG@abin789】-6022
【蜘蛛俠TG@abin789】-6023
【蜘蛛俠TG@abin789】-6024
【蜘蛛俠TG@abin789】-6025
【蜘蛛俠TG@abin789】-6026
【蜘蛛俠TG@abin789】-6027
【蜘蛛俠TG@abin789】-6028
【蜘蛛俠TG@abin789】-6029
【蜘蛛俠TG@abin789】-6030
【蜘蛛俠TG@abin789】-6031
【蜘蛛俠TG@abin789】-6032
【蜘蛛俠TG@abin789】-6033
【蜘蛛俠TG@abin789】-6034
【蜘蛛俠TG@abin789】-6035
【蜘蛛俠TG@abin789】-6036
【蜘蛛俠TG@abin789】-6037
【蜘蛛俠TG@abin789】-6038
【蜘蛛俠TG@abin789】-6039
【蜘蛛俠TG@abin789】-6040
【蜘蛛俠TG@abin789】-6041
【蜘蛛俠TG@abin789】-6042
【蜘蛛俠TG@abin789】-6043
【蜘蛛俠TG@abin789】-6044
【蜘蛛俠TG@abin789】-6045
【蜘蛛俠TG@abin789】-6046
【蜘蛛俠TG@abin789】-6047
【蜘蛛俠TG@abin789】-6048
【蜘蛛俠TG@abin789】-6049
【蜘蛛俠TG@abin789】-6050
【蜘蛛俠TG@abin789】-6051
【蜘蛛俠TG@abin789】-6052
【蜘蛛俠TG@abin789】-6053
【蜘蛛俠TG@abin789】-6054
【蜘蛛俠TG@abin789】-6055
【蜘蛛俠TG@abin789】-6056
【蜘蛛俠TG@abin789】-6057
【蜘蛛俠TG@abin789】-6058
【蜘蛛俠TG@abin789】-6059
【蜘蛛俠TG@abin789】-6060
【蜘蛛俠TG@abin789】-6061
【蜘蛛俠TG@abin789】-6062
【蜘蛛俠TG@abin789】-6063
【蜘蛛俠TG@abin789】-6064
【蜘蛛俠TG@abin789】-6065
【蜘蛛俠TG@abin789】-6066
【蜘蛛俠TG@abin789】-6067
【蜘蛛俠TG@abin789】-6068
【蜘蛛俠TG@abin789】-6069
【蜘蛛俠TG@abin789】-6070
【蜘蛛俠TG@abin789】-6071
【蜘蛛俠TG@abin789】-6072
【蜘蛛俠TG@abin789】-6073
【蜘蛛俠TG@abin789】-6074
【蜘蛛俠TG@abin789】-6075
【蜘蛛俠TG@abin789】-6076
【蜘蛛俠TG@abin789】-6077
【蜘蛛俠TG@abin789】-6078
【蜘蛛俠TG@abin789】-6079
【蜘蛛俠TG@abin789】-6080
【蜘蛛俠TG@abin789】-6081
【蜘蛛俠TG@abin789】-6082
【蜘蛛俠TG@abin789】-6083
【蜘蛛俠TG@abin789】-6084
【蜘蛛俠TG@abin789】-6085
【蜘蛛俠TG@abin789】-6086
【蜘蛛俠TG@abin789】-6087
【蜘蛛俠TG@abin789】-6088
【蜘蛛俠TG@abin789】-6089
【蜘蛛俠TG@abin789】-6090
【蜘蛛俠TG@abin789】-6091
【蜘蛛俠TG@abin789】-6092
【蜘蛛俠TG@abin789】-6093
【蜘蛛俠TG@abin789】-6094
【蜘蛛俠TG@abin789】-6095
【蜘蛛俠TG@abin789】-6096
【蜘蛛俠TG@abin789】-6097
【蜘蛛俠TG@abin789】-6098
【蜘蛛俠TG@abin789】-6099
【蜘蛛俠TG@abin789】-6100
【蜘蛛俠TG@abin789】-6101
【蜘蛛俠TG@abin789】-6102
【蜘蛛俠TG@abin789】-6103
【蜘蛛俠TG@abin789】-6104
【蜘蛛俠TG@abin789】-6105
【蜘蛛俠TG@abin789】-6106
【蜘蛛俠TG@abin789】-6107
【蜘蛛俠TG@abin789】-6108
【蜘蛛俠TG@abin789】-6109
【蜘蛛俠TG@abin789】-6110
【蜘蛛俠TG@abin789】-6111
【蜘蛛俠TG@abin789】-6112
【蜘蛛俠TG@abin789】-6113
【蜘蛛俠TG@abin789】-6114
【蜘蛛俠TG@abin789】-6115
【蜘蛛俠TG@abin789】-6116
【蜘蛛俠TG@abin789】-6117
【蜘蛛俠TG@abin789】-6118
【蜘蛛俠TG@abin789】-6119
【蜘蛛俠TG@abin789】-6120
【蜘蛛俠TG@abin789】-6121
【蜘蛛俠TG@abin789】-6122
【蜘蛛俠TG@abin789】-6123
【蜘蛛俠TG@abin789】-6124
【蜘蛛俠TG@abin789】-6125
【蜘蛛俠TG@abin789】-6126
【蜘蛛俠TG@abin789】-6127
【蜘蛛俠TG@abin789】-6128
【蜘蛛俠TG@abin789】-6129
【蜘蛛俠TG@abin789】-6130
【蜘蛛俠TG@abin789】-6131
【蜘蛛俠TG@abin789】-6132
【蜘蛛俠TG@abin789】-6133
【蜘蛛俠TG@abin789】-6134
【蜘蛛俠TG@abin789】-6135
【蜘蛛俠TG@abin789】-6136
【蜘蛛俠TG@abin789】-6137
【蜘蛛俠TG@abin789】-6138
【蜘蛛俠TG@abin789】-6139
【蜘蛛俠TG@abin789】-6140
【蜘蛛俠TG@abin789】-6141
【蜘蛛俠TG@abin789】-6142
【蜘蛛俠TG@abin789】-6143
【蜘蛛俠TG@abin789】-6144
【蜘蛛俠TG@abin789】-6145
【蜘蛛俠TG@abin789】-6146
【蜘蛛俠TG@abin789】-6147
【蜘蛛俠TG@abin789】-6148
【蜘蛛俠TG@abin789】-6149
【蜘蛛俠TG@abin789】-6150
【蜘蛛俠TG@abin789】-6151
【蜘蛛俠TG@abin789】-6152
【蜘蛛俠TG@abin789】-6153
【蜘蛛俠TG@abin789】-6154
【蜘蛛俠TG@abin789】-6155
【蜘蛛俠TG@abin789】-6156
【蜘蛛俠TG@abin789】-6157
【蜘蛛俠TG@abin789】-6158
【蜘蛛俠TG@abin789】-6159
【蜘蛛俠TG@abin789】-6160
【蜘蛛俠TG@abin789】-6161
【蜘蛛俠TG@abin789】-6162
【蜘蛛俠TG@abin789】-6163
【蜘蛛俠TG@abin789】-6164
【蜘蛛俠TG@abin789】-6165
【蜘蛛俠TG@abin789】-6166
【蜘蛛俠TG@abin789】-6167
【蜘蛛俠TG@abin789】-6168
【蜘蛛俠TG@abin789】-6169
【蜘蛛俠TG@abin789】-6170
【蜘蛛俠TG@abin789】-6171
【蜘蛛俠TG@abin789】-6172
【蜘蛛俠TG@abin789】-6173
【蜘蛛俠TG@abin789】-6174
【蜘蛛俠TG@abin789】-6175
【蜘蛛俠TG@abin789】-6176
【蜘蛛俠TG@abin789】-6177
【蜘蛛俠TG@abin789】-6178
【蜘蛛俠TG@abin789】-6179
【蜘蛛俠TG@abin789】-6180
【蜘蛛俠TG@abin789】-6181
【蜘蛛俠TG@abin789】-6182
【蜘蛛俠TG@abin789】-6183
【蜘蛛俠TG@abin789】-6184
【蜘蛛俠TG@abin789】-6185
【蜘蛛俠TG@abin789】-6186
【蜘蛛俠TG@abin789】-6187
【蜘蛛俠TG@abin789】-6188
【蜘蛛俠TG@abin789】-6189
【蜘蛛俠TG@abin789】-6190
【蜘蛛俠TG@abin789】-6191
【蜘蛛俠TG@abin789】-6192
【蜘蛛俠TG@abin789】-6193
【蜘蛛俠TG@abin789】-6194
【蜘蛛俠TG@abin789】-6195
【蜘蛛俠TG@abin789】-6196
【蜘蛛俠TG@abin789】-6197
【蜘蛛俠TG@abin789】-6198
【蜘蛛俠TG@abin789】-6199
【蜘蛛俠TG@abin789】-6200
2016-08-05
package com.user.Widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.widget.Adapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
public class IndexScroller {
private Context mContext;
private ListView mListView = null;
private float mIndexbarWidth; // 索引條的寬度,
private float mIndexbarMargin; // 索引條離右側(cè)邊緣的距離
private float mPreviewPadding; // 點(diǎn)擊中間顯示的索引文本,距離四周的距離
private float mDensity; // 當(dāng)前屏幕密度除以160
private float mScaledDensity; // 當(dāng)前屏幕密度除以160(設(shè)置字體的尺寸)
private float mAlphaRate; // 索引條透明度(用來(lái)顯示和隱藏索引條)0-1
private int mState = STATE_HIDDEN; // 索引條當(dāng)前狀態(tài)
private int mListViewWidth; // 屏幕的寬度
private int mListViewHeight; // 屏幕的高度
private int mCurrentSection = -1; // 當(dāng)前所點(diǎn)擊的索引值
private boolean mIsIndexing = false;
private SectionIndexer mIndexer = null;
private String[] mSections;
private RectF mIdexbarRect; // 繪制索引條區(qū)域
public static final int STATE_HIDDEN = 0;
public static final int STATE_SHOWING = 1;
public static final int STATE_SHOW = 2;
public static final int STATE_HIDDENING = 3;
public IndexScroller(Context mContext, ListView mListView) {
this.mContext = mContext;
this.mListView = mListView;
// 獲取當(dāng)前屏幕的密度比值
mDensity = mContext.getResources().getDisplayMetrics().density;
// 獲取字體密度比值
mScaledDensity = mContext.getResources().getDisplayMetrics().scaledDensity;
setAdapter(mListView.getAdapter());
// 根據(jù)屏幕的 密度比值來(lái)調(diào)整索引條的寬度(單位:像素)
mIndexbarWidth = 20 * mDensity;
mIndexbarMargin = 10 * mDensity;
mPreviewPadding = 5 * mDensity;
}
public void setAdapter(Adapter adapter) {
if (adapter instanceof SectionIndexer) {
mIndexer = (SectionIndexer) adapter;
mSections = (String[]) mIndexer.getSections();
}
}
/**
* 繪制索引條和預(yù)覽文本
*?
* @param mPaint
*/
public void draw(Canvas mCanvas) {
// 1、繪制索引條和背景
// 2、繪制預(yù)覽文本和背景
if (mState == STATE_HIDDEN) {
return;
}
/**
* 繪制索引條的區(qū)域
*/
Paint mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setAlpha((int) (64 * mAlphaRate)); // 設(shè)置透明度,逐漸顯示
// 繪制索引條(四個(gè)角都是圓角區(qū)域)
mCanvas.drawRoundRect(mIdexbarRect, 5 * mDensity, 5 * mDensity, mPaint);
/**
* 繪制預(yù)覽文本背景和文字
*/
if (mSections != null && mSections.length > 0) {
// 繪制預(yù)覽文本背景和文字 , 大于0則點(diǎn)擊
if (mCurrentSection > 0) {
Paint previewPaint = new Paint(); // 繪制文本畫筆
previewPaint.setColor(Color.BLACK);
previewPaint.setAlpha(96);
Paint previewTextPaint = new Paint(); // 繪制文字畫筆
previewTextPaint.setColor(Color.WHITE);
previewTextPaint.setTextSize(50 * mScaledDensity);
// 獲取文本字體的寬度
float previewTextWidth = previewTextPaint
.measureText(mSections[mCurrentSection]);
// 獲得顯示文本的區(qū)域?qū)捀叨?= 上下間距padding + 字母往下超過(guò)基準(zhǔn)線的高度(正值) +
// 字母在基準(zhǔn)線的上面的高度(負(fù)值,負(fù)負(fù)得正)
float previewSize = 2 * mPreviewPadding
+ previewTextPaint.descent()
- previewTextPaint.ascent();
// 預(yù)覽文本區(qū)域所在位置
// 左=(屏幕寬度-顯示文本區(qū)域?qū)挾龋? 2 上= (屏幕高度-顯示文本區(qū)域高度)/ 2
RectF previewRectF = new RectF(
(mListViewWidth - previewSize) / 2,
(mListViewHeight - previewSize) / 2,
(mListViewWidth - previewSize) / 2 + previewSize,
(mListViewHeight - previewSize) / 2 + previewSize);
// 繪制預(yù)覽文本背景
mCanvas.drawRoundRect(previewRectF, 5 * mDensity, 5 * mDensity,
previewPaint);
// 繪制預(yù)覽文本的字體
mCanvas.drawText(
mSections[mCurrentSection],
(previewRectF.left + (previewSize - previewTextWidth)
/ 2 + 1),
(previewRectF.top + mPreviewPadding
- previewTextPaint.ascent() + 1),
previewTextPaint);
}
}
/**
* 設(shè)置索引字母
*/
// 設(shè)置繪制索引字母的屬性
Paint indexPaint = new Paint();
indexPaint.setColor(Color.WHITE);
indexPaint.setAlpha((int) (255 * mAlphaRate));
indexPaint.setTextSize(12 * mScaledDensity);
// 取得索引中每個(gè)字母的區(qū)域高度 = (索引區(qū)域高度 - 上下margin值 )/索引的長(zhǎng)度length
float sectionHeight = ((mIdexbarRect.height() - 2 * mIndexbarMargin) / mSections.length);
// 設(shè)置每個(gè)字母區(qū)域中的文字的padding值 = (每個(gè)字母區(qū)域 - 文字高度)/2
float sectionPaddingTop = ((sectionHeight - (indexPaint.descent() - indexPaint
.ascent())) / 2);
for (int i = 0; i < mSections.length; i++) {
// 取得繪制的文字的paddingLeft值
float sectionPaddingLeft = (mIndexbarWidth - indexPaint
.measureText(mSections[i])) / 2;
// 開始繪制
mCanvas.drawText(mSections[i],
(mIdexbarRect.left + sectionPaddingLeft), mIdexbarRect.top
+ mIndexbarMargin + sectionPaddingTop * i/* 索引字母區(qū)域 */
+ sectionPaddingTop - indexPaint.ascent(),
indexPaint);
}
}
/**
* 保持最新的高度與寬度 重新加載索引條的區(qū)域
*?
* @param w
* @param h
* @param oldw
* @param oldh
*/
public void onSizeChanged(int w, int h, int oldw, int oldh) {
mListViewWidth = w;
mListViewHeight = h;
mIdexbarRect = new RectF(w - mIndexbarMargin - mIndexbarWidth,
mIndexbarMargin, w - mIndexbarMargin, h - mIndexbarMargin);
}
private void fade(long delay) {
// 0立即執(zhí)行,不延遲
// 清除消息
mHandler.removeMessages(0);
mHandler.sendEmptyMessageAtTime(0, SystemClock.uptimeMillis() + delay);
}
/**
* 判定狀態(tài)來(lái)顯示,并移除消息message
*/
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
switch (mState) {
case STATE_HIDDENING:
mAlphaRate -= mAlphaRate * 0.2;
if (mAlphaRate < 0.1) {
mAlphaRate = 0;
setState(STATE_HIDDEN);
}
mListView.invalidate();
fade(10);
break;
case STATE_HIDDEN:
setState(STATE_SHOWING);
break;
case STATE_SHOWING:
mAlphaRate += (1 - mAlphaRate) * 0.2;
if (mAlphaRate > 0.9) {
mAlphaRate = 1;
setState(STATE_SHOW);
}
mListView.invalidate(); // listview不斷刷新,知道m(xù)AlphaRate > 0.9
fade(10);
break;
case STATE_SHOW:
setState(STATE_HIDDENING);
break;
}
};
};
/**
* 改變狀態(tài)
*?
* @param state
*/
private void setState(int state) {
if (state < STATE_HIDDEN || state > STATE_HIDDENING) {
return;
}
mState = state;
switch (mState) {
case STATE_SHOWING:
mAlphaRate = 0;
fade(0);
break;
case STATE_SHOW:
mHandler.removeMessages(0);
break;
case STATE_HIDDENING:
mAlphaRate = 1;
fade(3000); // 顯示的時(shí)間
break;
case STATE_HIDDEN:
mHandler.removeMessages(0);
break;
}
}
/**
* 管理觸摸索引條
*?
* @param ev
* @return
*/
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mState != STATE_HIDDEN && cantains(ev.getX(), ev.getY())) {
setState(STATE_SHOW);
mIsIndexing = true;
mCurrentSection = getSectionByPoint(ev.getY());
mListView.setSelection(mIndexer
.getPositionForSection(mCurrentSection));
return true;
}
break;
case MotionEvent.ACTION_MOVE:
if (mIsIndexing) {
if (cantains(ev.getX(), ev.getY())) {
mCurrentSection = getSectionByPoint(ev.getY());
mListView.setSelection(mIndexer
.getPositionForSection(mCurrentSection));
return true;
}
}
break;
case MotionEvent.ACTION_UP:
if (mIsIndexing) {
mIsIndexing = false;
mCurrentSection = -1;
}
if (mState == STATE_SHOW) {
setState(STATE_HIDDENING);
break;
}
}
return false;
}
/**
*?
* @param x
* @param y
* @return
*/
private boolean cantains(float x, float y) {
return (x >= mIdexbarRect.left && y >= mIdexbarRect.top && y <= mIdexbarRect.top
+ mIdexbarRect.height());
}
/**
*?
* @param y
* @return
*/
private int getSectionByPoint(float y) {
if (mSections == null || mSections.length == 0)
return 0;
if (y < mIdexbarRect.top + mIndexbarMargin)
return 0;
if (y >= mIdexbarRect.top + mIdexbarRect.height() - mIndexbarMargin)
return mSections.length - 1;
return (int) ((y - mIdexbarRect.top - mIndexbarMargin) / ((mIdexbarRect
.height() - 2 * mIndexbarMargin) / mSections.length));
}
/**
* show()
*/
public void show() {
if (mState == STATE_HIDDEN)
setState(STATE_SHOWING);
else if (mState == STATE_HIDDENING)
setState(STATE_HIDDENING);
}
/**
* hide()
*/
public void hide() {
if (mState == STATE_SHOW)
setState(STATE_HIDDENING);
}
}