继上一篇:自定义view实现一个游标效果,在此基础上新增支持功能:
当手指点击某个时间域时,也可以触发游标滑动到当前点击选择的时间域。
下面贴代码:
package com.baicells.omcserver.view;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewParent;
import com.baicells.omcserver.R;
import java.util.ArrayList;
public class CursorView extends View {
private int width;
private int height;
private int radius;
private int strokeWidth;
private int curTextColor;
private int inCurTextColor;
private ArrayList<String> cursorText;
private Paint borderPaint;
private Paint innerBgPaint;
private Paint textPaint;
private int current;
private float offsetX;
private RectF currentRectF;
private boolean isInCurrent;
private GestureDetector mDetector;
private OnItemSelectListener mListener;
public CursorView(Context context) {
super(context);
}
public CursorView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mDetector = new GestureDetector(context, mGestureListener);
borderPaint = new Paint();
borderPaint.setAntiAlias(true);
borderPaint.setStyle(Paint.Style.FILL.STROKE);
innerBgPaint = new Paint();
innerBgPaint.setAntiAlias(true);
innerBgPaint.setStyle(Paint.Style.FILL_AND_STROKE);
textPaint = new Paint();
textPaint.setAntiAlias(true);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CursorView);
int indexCount = typedArray.getIndexCount();
for (int i = 0; i < indexCount; i++) {
int index = typedArray.getIndex(i);
switch (index) {
case R.styleable.CursorView_current_bg_color:
innerBgPaint.setColor(typedArray.getColor(index, Color.GREEN));
break;
case R.styleable.CursorView_current_text_color:
curTextColor = typedArray.getColor(index, Color.WHITE);
break;
case R.styleable.CursorView_incurrent_text_color:
inCurTextColor = typedArray.getColor(index, Color.BLACK);