Android侧边栏滑动切换的view效果
短信预约 -IT技能 免费直播动态提醒
废话不多说了,直接给大家贴代码了,具体代码如下所示:
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;
}
}
以上所述是小编给大家介绍的Android侧边栏滑动切换的view效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程网网站的支持!
您可能感兴趣的文章:Android仿人人网滑动侧边栏效果Android实现仿通讯录侧边栏滑动SiderBar效果代码Android实现滑动侧边栏
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341