废话不多说了,直接给大家贴代码了,具体代码如下所示:
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,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; }}
以上所述是小编给大家介绍的AndroID侧边栏滑动切换的vIEw效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android侧边栏滑动切换的view效果全部内容,希望文章能够帮你解决Android侧边栏滑动切换的view效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)