您的位置:首页 > 移动开发 > Android开发

Android开发-自定义View-AndroidStudio(十四)快速索引(1)

2016-12-27 14:38 369 查看
转载请注明出处:http://blog.csdn.net/iwanghang/
觉得博文有用,请点赞,请评论,请关注,谢谢!~

老规矩,先上GIF动态图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:
A1、A2是监听手势滑动,设置右侧List的字母颜色,B1、B2是设置接口回调,当触摸字母改变,传递给MainActivity。



MainActivity.java:
package com.iwanghang.indexeddemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements IndexView.WordListener {

private TextView tv_main;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv_main = (TextView) findViewById(R.id.tv_main);
}

/**
* 实现IndexView中的字母改变的接口
*/
@Override
public void wordListener(String word) {
// 当触摸的字母发生改变,这里接到回传,重新设置TextView
tv_main.setText(word);
}
}
IndexView.java:
package com.iwanghang.indexeddemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
* 绘制快速索引的字母
*/
public class IndexView extends View{

private 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 int itemWidth;
private int itemHeight;

private String word;

private int wordWidth;
private int wordHeight;
private float wordX;
private float wordY;

private Paint paint;

public IndexView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setTextSize(25);
paint.setColor(Color.WHITE);
paint.setAntiAlias(true); // 抗锯齿,字体圆滑
paint.setTypeface(Typeface.DEFAULT_BOLD);//设置粗体字
}

// 测量包裹字母的条目的宽高
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
itemWidth = getMeasuredWidth();
itemHeight = getMeasuredHeight()/26;
}

// 在画布上 绘制字母
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < 26; i++) {

// 根据onTouchEvent 监听手势滑动位置,设置当前滑动的字母的下标 来设置字母颜色 A2
if(touchIndex == i){
//设置灰色
paint.setColor(Color.GRAY);
}else{
//设置白色
paint.setColor(Color.WHITE);
}

word = words[i];

Rect rect = new Rect();
// 画笔
// 0,1的取一个字母
paint.getTextBounds(word, 0, 1, rect);

// 字母的高和宽
wordWidth = rect.width();
wordHeight = rect.height();

// 计算每个字母在视图上的坐标位置
//wordX = itemWidth / 2 - wordWidth / 2;
//wordY = itemHeight / 2 + wordHeight / 2 + i * itemHeight;

wordX = itemWidth / 2 - wordWidth / 2;
wordY = itemHeight / 2 + wordHeight / 2 + i * itemHeight;

canvas.drawText(word, wordX, wordY, paint);
}
}

// 字母的下标位置
private int touchIndex = -1;

/**
* 监听手势滑动位置,设置当前滑动的字母的下标 A1
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
float Y = event.getY();
int index = (int) (Y/itemHeight); // 字母索引
if(index != touchIndex){
touchIndex = index;
invalidate(); // 强制绘制onDraw();
}

System.out.println(touchIndex);
if (touchIndex > -1 && touchIndex < 26) {
WordListener listener = (WordListener) getContext(); // 回调接口 B2
listener.wordListener(words[touchIndex]); // 回传触摸的字母,告诉MainActivity
}

break;
case MotionEvent.ACTION_UP:
touchIndex = -1;
invalidate();
break;
}
return true; // 返回true,拦截事件分发
}

// 回调接口 B1
public interface WordListener { // 下载是否成功.监听.按钮回调接口
void wordListener(String word); // 回传一个字符串
}

}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.iwanghang.indexeddemo.MainActivity">

<ListView
android:id="@+id/lv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<TextView
android:id="@+id/tv_main"
android:layout_centerInParent="true"
android:textSize="30sp"
android:gravity="center"
android:textColor="#FFFFFF"
android:background="#44000000"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="A" />

<com.iwanghang.indexeddemo.IndexView
android:id="@+id/iv_words"
android:layout_alignParentRight="true"
android:background="#44000000"
android:layout_width="30dp"
android:layout_height="match_parent" />

</RelativeLayout>


转载请注明出处:http://blog.csdn.net/iwanghang/

欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式



微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



觉得博文有用,请点赞,请评论,请关注,谢谢!~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息