自定义view-横向快速索引
2018-02-22 09:08
197 查看
自定义view类: public class MyCustomSearch extends View { private static final String[] WORDS = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"}; private static final String[] WORDSTWO = {"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"}; private float cellheight; private float cellwidth; int lastpositon = -1; private int measuredHeight; private int measuredWidth; private MyOnClickListener myOnClickListener; private Paint paint; private String word; private float x; private float y; protected static final int DEFAULT_RADIUS = 150; private float dimension; public MyCustomSearch(Context paramContext) { this(paramContext, null); } public MyCustomSearch(Context paramContext, @Nullable AttributeSet paramAttributeSet) { this(paramContext, paramAttributeSet, 0); } public MyCustomSearch(Context paramContext, @Nullable AttributeSet paramAttributeSet, int paramInt) { super(paramContext, paramAttributeSe 11e69 t, paramInt); TypedArray a = paramContext.getTheme().obtainStyledAttributes(paramAttributeSet, R.styleable.mycustomatrrs, paramInt, 0); dimension = a.getDimension(R.styleable.mycustomatrrs_textSize, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_RADIUS, getResources().getDisplayMetrics())); Log.d("TAG", dimension + ""); //初始化 initView(); } private void initView() { paint = new Paint(); paint.setAntiAlias(true);//设置抗锯齿 paint.setColor(Color.BLACK);//设置字体颜色 paint.setTextSize(dimension);//设置字体大小 paint.setFakeBoldText(true); } @Override protected void onMeasure(int paramInt1, int paramInt2) { super.onMeasure(paramInt1, paramInt2); //获取该控件的宽高 measuredWidth = getMeasuredWidth(); measuredHeight = getMeasuredHeight(); //获取单元格的宽高 cellwidth = (this.measuredWidth * 1.0F / 16); cellheight = (this.measuredHeight * 0.5f); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //定义一个变量用来绘制第二行,让其从第一个开始依次绘制 int a = 0; for (int i = 0; i < WORDS.length; i++) { word = WORDS[i]; if (i >= 16) { x = (cellwidth / 2 - paint.measureText(word) / 2 + a * cellwidth); y = (cellheight + cellheight / 2 + paint.descent() / 2); a++; } else { x = (cellwidth / 2 - paint.measureText(word) / 2 + i * cellwidth); y = (cellheight / 2 + paint.descent() / 2); } canvas.drawText(word, x, y, paint); } } @Override public boolean onTouchEvent(MotionEvent event) { String word = null; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: x = (int) event.getX(); y = (int) event.getY(); int touchposition = (int) (x / cellwidth); //为了防止多次点击字母重复 if (touchposition != lastpositon) { //为了防止下标越界,将可点击的范围设置在数组长度之内 if (touchposition >= 0 && touchposition < WORDS.length) { if (y > cellheight && touchposition < WORDSTWO.length) { word = WORDSTWO[touchposition]; } else { word = WORDS[touchposition]; } } if (myOnClickListener != null) { myOnClickListener.mOnClick(word); } lastpositon = touchposition; } break; case MotionEvent.ACTION_MOVE: x = (int) event.getX(); y = (int) event.getY(); int touchposition1 = (int) (x / cellwidth); //为了防止多次点击字母重复 if (touchposition1 != lastpositon) { //为了防止下标越界,将可点击的范围设置在数组长度之内 if (touchposition1 >= 0 && touchposition1 < WORDS.length) { if (y > cellheight && touchposition1 < WORDSTWO.length) { word = WORDSTWO[touchposition1]; } else { word = WORDS[touchposition1]; } } if (myOnClickListener != null) { myOnClickListener.mOnClick(word); } lastpositon = touchposition1; } break; } return true; } public void setMyOnClickListener(MyOnClickListener paramMyOnClickListener) { this.myOnClickListener = paramMyOnClickListener; } public interface MyOnClickListener { void mOnClick(String paramString); } } 使用:
<com.bawie.yodo.custom.MyCustomSearch android:id="@+id/brand_custom" mycustomatrrs:textSize="@dimen/x14" android:layout_width="match_parent" android:layout_height="75dp" />自定义属性atrrs:
<resources> <declare-styleable name="mycustomatrrs"> <attr name="textSize" format="dimension"></attr> </declare-styleable> </resources>
相关文章推荐
- 自定义View之快速索引栏的实现
- android自定义View----通讯录导航栏(快速查找索引)
- Android开发-自定义View-AndroidStudio(十五)快速索引(2)
- 自定义View:快速索引实现
- Android开发-自定义View-AndroidStudio(十五)快速索引(2)
- Android 自定义View-实现快速索引
- 快速索引 (对View的自定义,黑马程序员)
- 自定义View 实现字母快速索引控件
- 快速索引 (对View的自定义)
- 自定义view实战笔记--快速索引
- 【Android】自定义View快速建立字母索引列表
- 快速索引 (对View的自定义)
- Android开发-自定义View-AndroidStudio(十四)快速索引(1)
- 使用RecyclerView自定义目录快速索引
- 快速索引 (对View的自定义)
- Android开发-自定义View-AndroidStudio(十四)快速索引(1)
- Android 首字母分组快速索引 自定义WaveSiderBarView
- 【Android 仿微信通讯录 导航分组列表-下】自定义View为RecyclerView打造右侧索引导航栏IndexBar
- 自定义ViewGroup 实现拖动跟快速滚动的效果
- 自定义View,实现侧边索引