课件源码?
其中也有些没讲啊,就是事件处理中的如何确认是那个索引?
其中也有些没讲啊,就是事件处理中的如何确认是那个索引?
2015-10-13
【蜘蛛侠TG@abin789】-6801
【蜘蛛侠TG@abin789】-6802
【蜘蛛侠TG@abin789】-6803
【蜘蛛侠TG@abin789】-6804
【蜘蛛侠TG@abin789】-6805
【蜘蛛侠TG@abin789】-6806
【蜘蛛侠TG@abin789】-6807
【蜘蛛侠TG@abin789】-6808
【蜘蛛侠TG@abin789】-6809
【蜘蛛侠TG@abin789】-6810
【蜘蛛侠TG@abin789】-6811
【蜘蛛侠TG@abin789】-6812
【蜘蛛侠TG@abin789】-6813
【蜘蛛侠TG@abin789】-6814
【蜘蛛侠TG@abin789】-6815
【蜘蛛侠TG@abin789】-6816
【蜘蛛侠TG@abin789】-6817
【蜘蛛侠TG@abin789】-6818
【蜘蛛侠TG@abin789】-6819
【蜘蛛侠TG@abin789】-6820
【蜘蛛侠TG@abin789】-6821
【蜘蛛侠TG@abin789】-6822
【蜘蛛侠TG@abin789】-6823
【蜘蛛侠TG@abin789】-6824
【蜘蛛侠TG@abin789】-6825
【蜘蛛侠TG@abin789】-6826
【蜘蛛侠TG@abin789】-6827
【蜘蛛侠TG@abin789】-6828
【蜘蛛侠TG@abin789】-6829
【蜘蛛侠TG@abin789】-6830
【蜘蛛侠TG@abin789】-6831
【蜘蛛侠TG@abin789】-6832
【蜘蛛侠TG@abin789】-6833
【蜘蛛侠TG@abin789】-6834
【蜘蛛侠TG@abin789】-6835
【蜘蛛侠TG@abin789】-6836
【蜘蛛侠TG@abin789】-6837
【蜘蛛侠TG@abin789】-6838
【蜘蛛侠TG@abin789】-6839
【蜘蛛侠TG@abin789】-6840
【蜘蛛侠TG@abin789】-6841
【蜘蛛侠TG@abin789】-6842
【蜘蛛侠TG@abin789】-6843
【蜘蛛侠TG@abin789】-6844
【蜘蛛侠TG@abin789】-6845
【蜘蛛侠TG@abin789】-6846
【蜘蛛侠TG@abin789】-6847
【蜘蛛侠TG@abin789】-6848
【蜘蛛侠TG@abin789】-6849
【蜘蛛侠TG@abin789】-6850
【蜘蛛侠TG@abin789】-6851
【蜘蛛侠TG@abin789】-6852
【蜘蛛侠TG@abin789】-6853
【蜘蛛侠TG@abin789】-6854
【蜘蛛侠TG@abin789】-6855
【蜘蛛侠TG@abin789】-6856
【蜘蛛侠TG@abin789】-6857
【蜘蛛侠TG@abin789】-6858
【蜘蛛侠TG@abin789】-6859
【蜘蛛侠TG@abin789】-6860
【蜘蛛侠TG@abin789】-6861
【蜘蛛侠TG@abin789】-6862
【蜘蛛侠TG@abin789】-6863
【蜘蛛侠TG@abin789】-6864
【蜘蛛侠TG@abin789】-6865
【蜘蛛侠TG@abin789】-6866
【蜘蛛侠TG@abin789】-6867
【蜘蛛侠TG@abin789】-6868
【蜘蛛侠TG@abin789】-6869
【蜘蛛侠TG@abin789】-6870
【蜘蛛侠TG@abin789】-6871
【蜘蛛侠TG@abin789】-6872
【蜘蛛侠TG@abin789】-6873
【蜘蛛侠TG@abin789】-6874
【蜘蛛侠TG@abin789】-6875
【蜘蛛侠TG@abin789】-6876
【蜘蛛侠TG@abin789】-6877
【蜘蛛侠TG@abin789】-6878
【蜘蛛侠TG@abin789】-6879
【蜘蛛侠TG@abin789】-6880
【蜘蛛侠TG@abin789】-6881
【蜘蛛侠TG@abin789】-6882
【蜘蛛侠TG@abin789】-6883
【蜘蛛侠TG@abin789】-6884
【蜘蛛侠TG@abin789】-6885
【蜘蛛侠TG@abin789】-6886
【蜘蛛侠TG@abin789】-6887
【蜘蛛侠TG@abin789】-6888
【蜘蛛侠TG@abin789】-6889
【蜘蛛侠TG@abin789】-6890
【蜘蛛侠TG@abin789】-6891
【蜘蛛侠TG@abin789】-6892
【蜘蛛侠TG@abin789】-6893
【蜘蛛侠TG@abin789】-6894
【蜘蛛侠TG@abin789】-6895
【蜘蛛侠TG@abin789】-6896
【蜘蛛侠TG@abin789】-6897
【蜘蛛侠TG@abin789】-6898
【蜘蛛侠TG@abin789】-6899
【蜘蛛侠TG@abin789】-6900
【蜘蛛侠TG@abin789】-6901
【蜘蛛侠TG@abin789】-6902
【蜘蛛侠TG@abin789】-6903
【蜘蛛侠TG@abin789】-6904
【蜘蛛侠TG@abin789】-6905
【蜘蛛侠TG@abin789】-6906
【蜘蛛侠TG@abin789】-6907
【蜘蛛侠TG@abin789】-6908
【蜘蛛侠TG@abin789】-6909
【蜘蛛侠TG@abin789】-6910
【蜘蛛侠TG@abin789】-6911
【蜘蛛侠TG@abin789】-6912
【蜘蛛侠TG@abin789】-6913
【蜘蛛侠TG@abin789】-6914
【蜘蛛侠TG@abin789】-6915
【蜘蛛侠TG@abin789】-6916
【蜘蛛侠TG@abin789】-6917
【蜘蛛侠TG@abin789】-6918
【蜘蛛侠TG@abin789】-6919
【蜘蛛侠TG@abin789】-6920
【蜘蛛侠TG@abin789】-6921
【蜘蛛侠TG@abin789】-6922
【蜘蛛侠TG@abin789】-6923
【蜘蛛侠TG@abin789】-6924
【蜘蛛侠TG@abin789】-6925
【蜘蛛侠TG@abin789】-6926
【蜘蛛侠TG@abin789】-6927
【蜘蛛侠TG@abin789】-6928
【蜘蛛侠TG@abin789】-6929
【蜘蛛侠TG@abin789】-6930
【蜘蛛侠TG@abin789】-6931
【蜘蛛侠TG@abin789】-6932
【蜘蛛侠TG@abin789】-6933
【蜘蛛侠TG@abin789】-6934
【蜘蛛侠TG@abin789】-6935
【蜘蛛侠TG@abin789】-6936
【蜘蛛侠TG@abin789】-6937
【蜘蛛侠TG@abin789】-6938
【蜘蛛侠TG@abin789】-6939
【蜘蛛侠TG@abin789】-6940
【蜘蛛侠TG@abin789】-6941
【蜘蛛侠TG@abin789】-6942
【蜘蛛侠TG@abin789】-6943
【蜘蛛侠TG@abin789】-6944
【蜘蛛侠TG@abin789】-6945
【蜘蛛侠TG@abin789】-6946
【蜘蛛侠TG@abin789】-6947
【蜘蛛侠TG@abin789】-6948
【蜘蛛侠TG@abin789】-6949
【蜘蛛侠TG@abin789】-6950
【蜘蛛侠TG@abin789】-6951
【蜘蛛侠TG@abin789】-6952
【蜘蛛侠TG@abin789】-6953
【蜘蛛侠TG@abin789】-6954
【蜘蛛侠TG@abin789】-6955
【蜘蛛侠TG@abin789】-6956
【蜘蛛侠TG@abin789】-6957
【蜘蛛侠TG@abin789】-6958
【蜘蛛侠TG@abin789】-6959
【蜘蛛侠TG@abin789】-6960
【蜘蛛侠TG@abin789】-6961
【蜘蛛侠TG@abin789】-6962
【蜘蛛侠TG@abin789】-6963
【蜘蛛侠TG@abin789】-6964
【蜘蛛侠TG@abin789】-6965
【蜘蛛侠TG@abin789】-6966
【蜘蛛侠TG@abin789】-6967
【蜘蛛侠TG@abin789】-6968
【蜘蛛侠TG@abin789】-6969
【蜘蛛侠TG@abin789】-6970
【蜘蛛侠TG@abin789】-6971
【蜘蛛侠TG@abin789】-6972
【蜘蛛侠TG@abin789】-6973
【蜘蛛侠TG@abin789】-6974
【蜘蛛侠TG@abin789】-6975
【蜘蛛侠TG@abin789】-6976
【蜘蛛侠TG@abin789】-6977
【蜘蛛侠TG@abin789】-6978
【蜘蛛侠TG@abin789】-6979
【蜘蛛侠TG@abin789】-6980
【蜘蛛侠TG@abin789】-6981
【蜘蛛侠TG@abin789】-6982
【蜘蛛侠TG@abin789】-6983
【蜘蛛侠TG@abin789】-6984
【蜘蛛侠TG@abin789】-6985
【蜘蛛侠TG@abin789】-6986
【蜘蛛侠TG@abin789】-6987
【蜘蛛侠TG@abin789】-6988
【蜘蛛侠TG@abin789】-6989
【蜘蛛侠TG@abin789】-6990
【蜘蛛侠TG@abin789】-6991
【蜘蛛侠TG@abin789】-6992
【蜘蛛侠TG@abin789】-6993
【蜘蛛侠TG@abin789】-6994
【蜘蛛侠TG@abin789】-6995
【蜘蛛侠TG@abin789】-6996
【蜘蛛侠TG@abin789】-6997
【蜘蛛侠TG@abin789】-6998
【蜘蛛侠TG@abin789】-6999
【蜘蛛侠TG@abin789】-7000
【蜘蛛侠TG@abin789】-7001
【蜘蛛侠TG@abin789】-7002
【蜘蛛侠TG@abin789】-7003
【蜘蛛侠TG@abin789】-7004
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);
}
}
举报