求源码。。。
show()、hide()、getSectionsByPoint(ev.getY())、cantains(float x, float y)这四个方法呢? 老师,你这悬念留的也是醉了...能不能靠谱点 还是极客学院靠谱点,手敲+源码.....
show()、hide()、getSectionsByPoint(ev.getY())、cantains(float x, float y)这四个方法呢? 老师,你这悬念留的也是醉了...能不能靠谱点 还是极客学院靠谱点,手敲+源码.....
2016-08-05
【蜘蛛侠TG@abin789】-5601
【蜘蛛侠TG@abin789】-5602
【蜘蛛侠TG@abin789】-5603
【蜘蛛侠TG@abin789】-5604
【蜘蛛侠TG@abin789】-5605
【蜘蛛侠TG@abin789】-5606
【蜘蛛侠TG@abin789】-5607
【蜘蛛侠TG@abin789】-5608
【蜘蛛侠TG@abin789】-5609
【蜘蛛侠TG@abin789】-5610
【蜘蛛侠TG@abin789】-5611
【蜘蛛侠TG@abin789】-5612
【蜘蛛侠TG@abin789】-5613
【蜘蛛侠TG@abin789】-5614
【蜘蛛侠TG@abin789】-5615
【蜘蛛侠TG@abin789】-5616
【蜘蛛侠TG@abin789】-5617
【蜘蛛侠TG@abin789】-5618
【蜘蛛侠TG@abin789】-5619
【蜘蛛侠TG@abin789】-5620
【蜘蛛侠TG@abin789】-5621
【蜘蛛侠TG@abin789】-5622
【蜘蛛侠TG@abin789】-5623
【蜘蛛侠TG@abin789】-5624
【蜘蛛侠TG@abin789】-5625
【蜘蛛侠TG@abin789】-5626
【蜘蛛侠TG@abin789】-5627
【蜘蛛侠TG@abin789】-5628
【蜘蛛侠TG@abin789】-5629
【蜘蛛侠TG@abin789】-5630
【蜘蛛侠TG@abin789】-5631
【蜘蛛侠TG@abin789】-5632
【蜘蛛侠TG@abin789】-5633
【蜘蛛侠TG@abin789】-5634
【蜘蛛侠TG@abin789】-5635
【蜘蛛侠TG@abin789】-5636
【蜘蛛侠TG@abin789】-5637
【蜘蛛侠TG@abin789】-5638
【蜘蛛侠TG@abin789】-5639
【蜘蛛侠TG@abin789】-5640
【蜘蛛侠TG@abin789】-5641
【蜘蛛侠TG@abin789】-5642
【蜘蛛侠TG@abin789】-5643
【蜘蛛侠TG@abin789】-5644
【蜘蛛侠TG@abin789】-5645
【蜘蛛侠TG@abin789】-5646
【蜘蛛侠TG@abin789】-5647
【蜘蛛侠TG@abin789】-5648
【蜘蛛侠TG@abin789】-5649
【蜘蛛侠TG@abin789】-5650
【蜘蛛侠TG@abin789】-5651
【蜘蛛侠TG@abin789】-5652
【蜘蛛侠TG@abin789】-5653
【蜘蛛侠TG@abin789】-5654
【蜘蛛侠TG@abin789】-5655
【蜘蛛侠TG@abin789】-5656
【蜘蛛侠TG@abin789】-5657
【蜘蛛侠TG@abin789】-5658
【蜘蛛侠TG@abin789】-5659
【蜘蛛侠TG@abin789】-5660
【蜘蛛侠TG@abin789】-5661
【蜘蛛侠TG@abin789】-5662
【蜘蛛侠TG@abin789】-5663
【蜘蛛侠TG@abin789】-5664
【蜘蛛侠TG@abin789】-5665
【蜘蛛侠TG@abin789】-5666
【蜘蛛侠TG@abin789】-5667
【蜘蛛侠TG@abin789】-5668
【蜘蛛侠TG@abin789】-5669
【蜘蛛侠TG@abin789】-5670
【蜘蛛侠TG@abin789】-5671
【蜘蛛侠TG@abin789】-5672
【蜘蛛侠TG@abin789】-5673
【蜘蛛侠TG@abin789】-5674
【蜘蛛侠TG@abin789】-5675
【蜘蛛侠TG@abin789】-5676
【蜘蛛侠TG@abin789】-5677
【蜘蛛侠TG@abin789】-5678
【蜘蛛侠TG@abin789】-5679
【蜘蛛侠TG@abin789】-5680
【蜘蛛侠TG@abin789】-5681
【蜘蛛侠TG@abin789】-5682
【蜘蛛侠TG@abin789】-5683
【蜘蛛侠TG@abin789】-5684
【蜘蛛侠TG@abin789】-5685
【蜘蛛侠TG@abin789】-5686
【蜘蛛侠TG@abin789】-5687
【蜘蛛侠TG@abin789】-5688
【蜘蛛侠TG@abin789】-5689
【蜘蛛侠TG@abin789】-5690
【蜘蛛侠TG@abin789】-5691
【蜘蛛侠TG@abin789】-5692
【蜘蛛侠TG@abin789】-5693
【蜘蛛侠TG@abin789】-5694
【蜘蛛侠TG@abin789】-5695
【蜘蛛侠TG@abin789】-5696
【蜘蛛侠TG@abin789】-5697
【蜘蛛侠TG@abin789】-5698
【蜘蛛侠TG@abin789】-5699
【蜘蛛侠TG@abin789】-5700
【蜘蛛侠TG@abin789】-5701
【蜘蛛侠TG@abin789】-5702
【蜘蛛侠TG@abin789】-5703
【蜘蛛侠TG@abin789】-5704
【蜘蛛侠TG@abin789】-5705
【蜘蛛侠TG@abin789】-5706
【蜘蛛侠TG@abin789】-5707
【蜘蛛侠TG@abin789】-5708
【蜘蛛侠TG@abin789】-5709
【蜘蛛侠TG@abin789】-5710
【蜘蛛侠TG@abin789】-5711
【蜘蛛侠TG@abin789】-5712
【蜘蛛侠TG@abin789】-5713
【蜘蛛侠TG@abin789】-5714
【蜘蛛侠TG@abin789】-5715
【蜘蛛侠TG@abin789】-5716
【蜘蛛侠TG@abin789】-5717
【蜘蛛侠TG@abin789】-5718
【蜘蛛侠TG@abin789】-5719
【蜘蛛侠TG@abin789】-5720
【蜘蛛侠TG@abin789】-5721
【蜘蛛侠TG@abin789】-5722
【蜘蛛侠TG@abin789】-5723
【蜘蛛侠TG@abin789】-5724
【蜘蛛侠TG@abin789】-5725
【蜘蛛侠TG@abin789】-5726
【蜘蛛侠TG@abin789】-5727
【蜘蛛侠TG@abin789】-5728
【蜘蛛侠TG@abin789】-5729
【蜘蛛侠TG@abin789】-5730
【蜘蛛侠TG@abin789】-5731
【蜘蛛侠TG@abin789】-5732
【蜘蛛侠TG@abin789】-5733
【蜘蛛侠TG@abin789】-5734
【蜘蛛侠TG@abin789】-5735
【蜘蛛侠TG@abin789】-5736
【蜘蛛侠TG@abin789】-5737
【蜘蛛侠TG@abin789】-5738
【蜘蛛侠TG@abin789】-5739
【蜘蛛侠TG@abin789】-5740
【蜘蛛侠TG@abin789】-5741
【蜘蛛侠TG@abin789】-5742
【蜘蛛侠TG@abin789】-5743
【蜘蛛侠TG@abin789】-5744
【蜘蛛侠TG@abin789】-5745
【蜘蛛侠TG@abin789】-5746
【蜘蛛侠TG@abin789】-5747
【蜘蛛侠TG@abin789】-5748
【蜘蛛侠TG@abin789】-5749
【蜘蛛侠TG@abin789】-5750
【蜘蛛侠TG@abin789】-5751
【蜘蛛侠TG@abin789】-5752
【蜘蛛侠TG@abin789】-5753
【蜘蛛侠TG@abin789】-5754
【蜘蛛侠TG@abin789】-5755
【蜘蛛侠TG@abin789】-5756
【蜘蛛侠TG@abin789】-5757
【蜘蛛侠TG@abin789】-5758
【蜘蛛侠TG@abin789】-5759
【蜘蛛侠TG@abin789】-5760
【蜘蛛侠TG@abin789】-5761
【蜘蛛侠TG@abin789】-5762
【蜘蛛侠TG@abin789】-5763
【蜘蛛侠TG@abin789】-5764
【蜘蛛侠TG@abin789】-5765
【蜘蛛侠TG@abin789】-5766
【蜘蛛侠TG@abin789】-5767
【蜘蛛侠TG@abin789】-5768
【蜘蛛侠TG@abin789】-5769
【蜘蛛侠TG@abin789】-5770
【蜘蛛侠TG@abin789】-5771
【蜘蛛侠TG@abin789】-5772
【蜘蛛侠TG@abin789】-5773
【蜘蛛侠TG@abin789】-5774
【蜘蛛侠TG@abin789】-5775
【蜘蛛侠TG@abin789】-5776
【蜘蛛侠TG@abin789】-5777
【蜘蛛侠TG@abin789】-5778
【蜘蛛侠TG@abin789】-5779
【蜘蛛侠TG@abin789】-5780
【蜘蛛侠TG@abin789】-5781
【蜘蛛侠TG@abin789】-5782
【蜘蛛侠TG@abin789】-5783
【蜘蛛侠TG@abin789】-5784
【蜘蛛侠TG@abin789】-5785
【蜘蛛侠TG@abin789】-5786
【蜘蛛侠TG@abin789】-5787
【蜘蛛侠TG@abin789】-5788
【蜘蛛侠TG@abin789】-5789
【蜘蛛侠TG@abin789】-5790
【蜘蛛侠TG@abin789】-5791
【蜘蛛侠TG@abin789】-5792
【蜘蛛侠TG@abin789】-5793
【蜘蛛侠TG@abin789】-5794
【蜘蛛侠TG@abin789】-5795
【蜘蛛侠TG@abin789】-5796
【蜘蛛侠TG@abin789】-5797
【蜘蛛侠TG@abin789】-5798
【蜘蛛侠TG@abin789】-5799
【蜘蛛侠TG@abin789】-5800
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);
}
}
举报