其实很简单的
public class MultiNavigationView extends View
{
private int mPressBtnIndex = NavKey.NULL;
private OnNavClickListener mNavViewListener;
private OnNavLongClckListener mOnNavLongClckListener;
private boolean mHasPerformedLongPress;
private boolean mHasPerformedPress;
private CheckForLongPress mPendingCheckForLongPress;
public static class NavKey
{
public static final int CENTER = 0;
public static final int UP = 1;
public static final int RIGHT = 2;
public static final int DOWN = 3;
public static final int LEFT = 4;
public static final int NULL = -1;
}
public interface OnNavClickListener
{
void onNavClick(int key);
};
public void setOnNavViewListener(OnNavClickListener navClickListener) {
mNavViewListener = navClickListener;
}
public interface OnNavLongClckListener
{
void onNavLongClick(int key);
};
public void setOnNavLongClckListener(OnNavLongClckListener onNavLongClckListener) {
mOnNavLongClckListener = onNavLongClckListener;
}
public MultiNavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MultiNavigationView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MultiNavigationView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = null;
switch (mPressBtnIndex) {
case NavKey.CENTER:
drawable = getResources().getDrawable(R.drawable.dpad_5way_center);
break;
case NavKey.UP:
drawable = getResources().getDrawable(R.drawable.dpad_5way_up);
break;
case NavKey.RIGHT:
drawable = getResources().getDrawable(R.drawable.dpad_5way_right);
break;
case NavKey.DOWN:
drawable = getResources().getDrawable(R.drawable.dpad_5way_down);
break;
case NavKey.LEFT:
drawable = getResources().getDrawable(R.drawable.dpad_5way_left);
break;
default:
drawable = getResources().getDrawable(R.drawable.dpad_5way_normal);
break;
}
drawable.setBounds(0, 0, getWidth(), getHeight());
drawable.draw(canvas);
super.onDraw(canvas);
}
@SuppressLint ("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mHasPerformedPress = false;
mHasPerformedLongPress = false;
checkForLongClick();
float x = event.getX();
float y = event.getY();
float w = getWidth();
float h = getHeight();
if (x < w * 2f / 3f && x > w / 3f && y < h * 2f / 3f && y > w / 3f) {
setPressed(NavKey.CENTER);
}
else if (x > y && y < w - x) {
setPressed(NavKey.UP);
}
else if (x > y && y > w - x) {
setPressed(NavKey.RIGHT);
}
else if (x < y && x > h - y) {
setPressed(NavKey.DOWN);
}
else if (x < y && x < h - y) {
setPressed(NavKey.LEFT);
}
break;
case MotionEvent.ACTION_UP:
performClick();
setPressed(NavKey.NULL);
break;
case MotionEvent.ACTION_CANCEL:
removeCallbacks(mPendingCheckForLongPress);
default:
break;
}
invalidate();
return true;
}
private void setPressed(int index) {
mPressBtnIndex = index;
}
private void checkForLongClick() {
if (mPendingCheckForLongPress == null) {
mPendingCheckForLongPress = new CheckForLongPress();
}
postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());
}
class CheckForLongPress implements Runnable
{
public void run() {
mHasPerformedLongPress = performLongClick();
}
}
public boolean performClick() {
mHasPerformedPress = true;
if (mNavViewListener != null && !mHasPerformedLongPress) {
mNavViewListener.onNavClick(mPressBtnIndex);
}
return true;
}
@Override
public boolean performLongClick() {
if (mOnNavLongClckListener != null && !mHasPerformedPress) {
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
mOnNavLongClckListener.onNavLongClick(mPressBtnIndex);
}
return true;
}
}