代码如下 | 复制代码 |
import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class SideBar extends View { private static final int MARGIN_BOTTOM = 2 ; private int mItemHeight = 18 ; private OnLetterTouchListener mOnLetterTouchListener; private Paint mPaint; private float mWidthCenter; private int backgroundColor = 0x00F0F0F0 ; private char [] chars = AppCommon.INDEXER_CHARS; public interface OnLetterTouchListener { public abstract void onPressedDown( int index, char ch); public abstract void onPressedUp( int index, char ch); } public SideBar(Context context) { this (context, null ); } public SideBar(Context context, AttributeSet attrs) { this (context, attrs, - 1 ); } public SideBar(Context context, AttributeSet attrs, int defStyle) { super (context, attrs, defStyle); init(); } private int startY = 0 ; //其实位置的y轴坐标 public SideBar setChars( char [] chars) { this .chars = chars; // for(int i = 0 ; i < (26 - chars.length) / 2 ; i++){ // startY += i * mItemHeight; // } init(); postInvalidate(); return this ; } private void init() { mPaint = new Paint(); mPaint.setColor( 0xFFCFCFD0 ); mPaint.setAntiAlias( true ); mPaint.setFakeBoldText( true ); mPaint.setTextSize( 32 ); mPaint.setColor(getResources().getColor(R.color.blue)); mPaint.setSubpixelText( true ); mPaint.setTextAlign(Paint.Align.CENTER); setBackgroundColor(backgroundColor); } public boolean onTouchEvent(MotionEvent event) { super .onTouchEvent(event); int idx = ( int ) event.getY() / mItemHeight; if (idx >= chars.length) { idx = chars.length - 1 ; } else if (idx < 0 ) { idx = 0 ; } if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { setBackgroundResource(R.color.sidebar_select_background); if (mOnLetterTouchListener != null ) { mOnLetterTouchListener.onPressedDown(idx, chars[idx]); } } else if (event.getAction() == MotionEvent.ACTION_UP) { setBackgroundDrawable( new BitmapDrawable()); setBackgroundColor(backgroundColor); if (mOnLetterTouchListener != null ) { mOnLetterTouchListener.onPressedUp(idx, chars[idx]); } } return true ; } @Override protected void onSizeChanged( int w, int h, int oldw, int oldh) { mItemHeight = (h - MARGIN_BOTTOM) / 26 ; mWidthCenter = getMeasuredWidth() / 2 ; super .onSizeChanged(w, h, oldw, oldh); } protected void onDraw(Canvas canvas) { for ( int i = 0 ; i < chars.length; i++) { canvas.drawText(String.valueOf(chars[i]), mWidthCenter,startY + mItemHeight + (i * mItemHeight), mPaint); } super .onDraw(canvas); } public void setOnLetterTouchListener(OnLetterTouchListener listener) { mOnLetterTouchListener = listener; } } |
凡尔赛法环免广告版 最新版v1.69.1
下载天煞异种 安卓版v1.0.21
下载十三月 (Undecember)最新官方中文版v3.26.0200
下载我不是废柴无限99999钻999999金币游戏 不减反增版v1.2.16
下载我的女友是声优免广告 安卓版v1
我的女友是声优免广告是一款非常好玩的模拟经营类手游,玩家在游
石器帝国去广告版 v8
石器帝国免广告版是游戏的破解版本,在该版本中为玩家去除了广告
校园恋爱模拟器真人版去广告版 v0.0.7
校园恋爱模拟器真人版免广告版是游戏的破解版本,在该版本中为玩
教练我要开球馆内置菜单修改器 安卓版v1.5.4
教练我要开球馆内购版是一款模拟经营类游戏,玩家们将在游戏中创
我的游轮无限砖石 安卓版v1.7.0
我的游轮无限金币钻石版是一款模拟经营类游戏,你将成为一艘游轮