程序没有讲完整,最后的contains和getSectionByPoint两个方法没有讲
谢谢分享,但是程序没有讲完整,最后的contains和getSectionByPoint两个方法没有讲
谢谢分享,但是程序没有讲完整,最后的contains和getSectionByPoint两个方法没有讲
2016-04-15
【蜘蛛侠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; // 索引条离右侧边缘的距离
private float mPreviewPadding; // 点击中间显示的索引文本,距离四周的距离
private float mDensity; // 当前屏幕密度除以160
private float mScaledDensity; // 当前屏幕密度除以160(设置字体的尺寸)
private float mAlphaRate; // 索引条透明度(用来显示和隐藏索引条)0-1
private int mState = STATE_HIDDEN; // 索引条当前状态
private int mListViewWidth; // 屏幕的宽度
private int mListViewHeight; // 屏幕的高度
private int mCurrentSection = -1; // 当前所点击的索引值
private boolean mIsIndexing = false;
private SectionIndexer mIndexer = null;
private String[] mSections;
private RectF mIdexbarRect; // 绘制索引条区域
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;
// 获取当前屏幕的密度比值
mDensity = mContext.getResources().getDisplayMetrics().density;
// 获取字体密度比值
mScaledDensity = mContext.getResources().getDisplayMetrics().scaledDensity;
setAdapter(mListView.getAdapter());
// 根据屏幕的 密度比值来调整索引条的宽度(单位:像素)
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();
}
}
/**
* 绘制索引条和预览文本
*
* @param mPaint
*/
public void draw(Canvas mCanvas) {
// 1、绘制索引条和背景
// 2、绘制预览文本和背景
if (mState == STATE_HIDDEN) {
return;
}
/**
* 绘制索引条的区域
*/
Paint mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setAlpha((int) (64 * mAlphaRate)); // 设置透明度,逐渐显示
// 绘制索引条(四个角都是圆角区域)
mCanvas.drawRoundRect(mIdexbarRect, 5 * mDensity, 5 * mDensity, mPaint);
/**
* 绘制预览文本背景和文字
*/
if (mSections != null && mSections.length > 0) {
// 绘制预览文本背景和文字 , 大于0则点击
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]);
// 获得显示文本的区域宽高度 = 上下间距padding + 字母往下超过基准线的高度(正值) +
// 字母在基准线的上面的高度(负值,负负得正)
float previewSize = 2 * mPreviewPadding
+ previewTextPaint.descent()
- previewTextPaint.ascent();
// 预览文本区域所在位置
// 左=(屏幕宽度-显示文本区域宽度)/ 2 上= (屏幕高度-显示文本区域高度)/ 2
RectF previewRectF = new RectF(
(mListViewWidth - previewSize) / 2,
(mListViewHeight - previewSize) / 2,
(mListViewWidth - previewSize) / 2 + previewSize,
(mListViewHeight - previewSize) / 2 + previewSize);
// 绘制预览文本背景
mCanvas.drawRoundRect(previewRectF, 5 * mDensity, 5 * mDensity,
previewPaint);
// 绘制预览文本的字体
mCanvas.drawText(
mSections[mCurrentSection],
(previewRectF.left + (previewSize - previewTextWidth)
/ 2 + 1),
(previewRectF.top + mPreviewPadding
- previewTextPaint.ascent() + 1),
previewTextPaint);
}
}
/**
* 设置索引字母
*/
// 设置绘制索引字母的属性
Paint indexPaint = new Paint();
indexPaint.setColor(Color.WHITE);
indexPaint.setAlpha((int) (255 * mAlphaRate));
indexPaint.setTextSize(12 * mScaledDensity);
// 取得索引中每个字母的区域高度 = (索引区域高度 - 上下margin值 )/索引的长度length
float sectionHeight = ((mIdexbarRect.height() - 2 * mIndexbarMargin) / mSections.length);
// 设置每个字母区域中的文字的padding值 = (每个字母区域 - 文字高度)/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/* 索引字母区域 */
+ sectionPaddingTop - indexPaint.ascent(),
indexPaint);
}
}
/**
* 保持最新的高度与宽度 重新加载索引条的区域
*
* @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立即执行,不延迟
// 清除消息
mHandler.removeMessages(0);
mHandler.sendEmptyMessageAtTime(0, SystemClock.uptimeMillis() + delay);
}
/**
* 判定状态来显示,并移除消息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不断刷新,知道mAlphaRate > 0.9
fade(10);
break;
case STATE_SHOW:
setState(STATE_HIDDENING);
break;
}
};
};
/**
* 改变状态
*
* @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); // 显示的时间
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);
}
}
举报