Android自定义控件5----继承View通讯录索引功能之2在按下和移动的时候操作字母变色

效果图:

Android自定义控件5----继承View通讯录索引功能之2在按下和移动的时候操作字母变色_第1张图片

项目结构:

Android自定义控件5----继承View通讯录索引功能之2在按下和移动的时候操作字母变色_第2张图片

核心代码:

自定义IndexView
package com.example;

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;

import com.orhanobut.logger.Logger;

/**
 * 自定义通讯录
 */
public class IndexView extends View {
    /**
     * 每条的宽和高
     */
    private int itemWidth;
    private int itemHeight;
    private Paint paint;
    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"};
    public IndexView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        //设置颜色
        paint.setColor(Color.WHITE);
        //设置抗锯齿
        paint.setAntiAlias(true);
        //设置粗体字
        paint.setTypeface(Typeface.DEFAULT_BOLD);
//      设置字体大小
        paint.setTextSize(DensityUtil.dip2px(context,20));
    }
    /**
     * 测量方法
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        itemWidth = getMeasuredWidth();
        itemHeight = getMeasuredHeight() / words.length;
        Logger.t("111").d("onMeasure>>>");
    }

    /**
     * 绘制
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Logger.t("111").d("onDraw>>>");
        for (int i = 0; i < words.length; i++) {
            if(touchIndex ==i){
                //设置灰色
                paint.setColor(Color.GRAY);
            }else{
                //设置白色
                paint.setColor(Color.WHITE);
            }
            String word = words[i];//A
            Rect rect = new Rect();
            //画笔
            //0,1的取一个字母
            paint.getTextBounds(word, 0, 1, rect);
            //字母的高和宽
            int wordWidth = rect.width();
            int wordHeight = rect.height();
            //计算每个字母在视图上的坐标位置
            float wordX = itemWidth / 2 - wordWidth / 2;
            float wordY = itemHeight / 2 + wordHeight / 2 + i * itemHeight;
            canvas.drawText(word, wordX, wordY, paint);
        }
    }

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

    /**
     * 设置事件
     * 按下的字母背景设置成灰色,放开时所有字母的背景设置为白色
     * 手指按下文字变色
     * 1.重写onTouchEvent(),返回true,在down/move的过程中计算
     * int touchIndex = Y / itemHeight; 强制绘制
     * 

* 2.在onDraw()方法对于的下标设置画笔变色 *

* 3.在up的时候 * touchIndex = -1; * 强制绘制 * * @param event * @return */ @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(); } break; case MotionEvent.ACTION_UP: touchIndex = -1; invalidate();//强制绘制onDraw(); break; } return true; } }

工具类
DensityUtil
package com.example;

import android.content.Context;

/**
 * 工具类,px和dp的相互转化
 */
public class DensityUtil {
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
activity_main.xml


    
    
    


源码下载:

Myself ---- mytongxunlu2

http://download.csdn.net/download/zhaihaohao1/10111351

参考视频:

http://www.gulixueyuan.com/course/124/learn#lesson/1924

你可能感兴趣的:(自定义)