我正在做一个画布以在Android Studio的画布中在板上画一个正方形,
我需要知道在我的onTouchEvent
中单击了什么路径
但是我可以从那里拿出的event.x and event.y,
因为我的形状非常复杂,所以仅根据x和y来计算单击的路径将非常困难。
还有另一种方法可以检查屏幕上单击的路径吗?
这是我的画布:
public class PianoKeysWidget extends View {
public class ChessSlice extends Path {
public ChessSlice() {
key = "C";
octave = 0;
isKeyBlack = false;
}
public String key;
public int octave;
public boolean isKeyBlack;
}
Paint WhiteKeyPaint, BlackKeyPaint, OuterRimPaint;
Boolean isTouching = false;
float touchY, touchX;
int globalKeyWidth = 20;
PianoKeysInterface mainInterface;
ChessSlice framePath;
ArrayList<ChessSlice> pianoPathsArray = new ArrayList<ChessSlice>();
int width = 340; // default numbers until the screen will change it when it gets the actuall view
int height = 1200; // default numbers until the screen will change it when it gets the actuall view
String TAG = "alignmentWidget";
/**
* construuctor
*
* @param context
*/
public PianoKeysWidget(Context context) {
super(context);
this.postInvalidate();
init();
}
/**
* constructor
*
* @param context
* @param attrs
*/
public PianoKeysWidget(Context context, AttributeSet attrs) {
super(context, attrs);
this.postInvalidate();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = getWidth();
height = getHeight();
init();
}
/**
* constructor
*
* @param context
* @param attrs
* @param defStyleAttr
*/
public PianoKeysWidget(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.postInvalidate();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Bitmap getBitmap(VectorDrawable vectorDrawable) {
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
return bitmap;
}
private static Bitmap getBitmap(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (drawable instanceof BitmapDrawable) {
return BitmapFactory.decodeResource(context.getResources(), drawableId);
} else if (drawable instanceof VectorDrawable) {
return getBitmap((VectorDrawable) drawable);
} else {
throw new IllegalArgumentException("unsupported drawable type");
}
}
//////////////////////////////////
//////////////On Draw//////////////
//////////////////////////////////
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap bitmap = getBitmap(getContext(), R.drawable.ic_accessibility_black_24dp);
if (pianoPathsArray.size() == 0) { // initlizes the init
init();
}
// draws the first circle
////////////////////////////////////////////
//go through the array and paints the corisponding cells
///////////////////////////////////////////
for (int i = 0; i < pianoPathsArray.size(); i++) {
canvas.drawPath(pianoPathsArray.get(i), WhiteKeyPaint);
if (pianoPathsArray.get(i).isKeyBlack) {
canvas.drawPath(pianoPathsArray.get(i), BlackKeyPaint);
}
canvas.drawPath(pianoPathsArray.get(i), OuterRimPaint);
// if (pianoPathsArray.get(i).color.equals("white")) {
// canvas.drawPath(pianoPathsArray.get(i), WhiteKeyPaint);
// } else {
// canvas.drawPath(pianoPathsArray.get(i), BlackKeyPaint);
// }
//
//
//
// //draw the queens
// if (pianoPathsArray.get(i).isOcupied) {
// canvas.drawBitmap(bitmap, pianoPathsArray.get(i).centerX - 40, pianoPathsArray.get(i).centerY - 45, BlackKeyPaint);
// }
}
//draw the frame
canvas.drawPath(framePath, OuterRimPaint);
}
private void activateErrorAnimationTimer(final ChessSlice chessSlice) {
}
private void init() {
if (pianoPathsArray.size() > 0) { // initlizes the init
return;
}
//gets teh width and height, initlized only after ondraw happend so it wouldn't be 0 0
width = getWidth();
height = getHeight();
//defining paints
///////////////////////////////////////////
WhiteKeyPaint = new Paint();
WhiteKeyPaint.setColor(getResources().getColor(R.color.lightKeyColor));
WhiteKeyPaint.setStyle(Paint.Style.FILL_AND_STROKE);
BlackKeyPaint = new Paint();
BlackKeyPaint.setColor(getResources().getColor(R.color.darkKeyColor));
BlackKeyPaint.setStyle(Paint.Style.FILL_AND_STROKE);
OuterRimPaint = new Paint();
OuterRimPaint.setStrokeWidth(10f);
OuterRimPaint.setColor(getResources().getColor(R.color.colorAccent));
OuterRimPaint.setStyle(Paint.Style.STROKE);
// applyes hardware Acceleration
///////////////////////////////////////////
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
setLayerType(LAYER_TYPE_SOFTWARE, BlackKeyPaint);
}
int overAllKeys = Globals.getInstance().numberOfPianoKeys;
int numberWhiteKeys = getWhiteKeysCount(overAllKeys)-1;
int numberBlackKeys = getBlackKeysCount(overAllKeys);
Log.d ("testings", "whitekeys: "+numberWhiteKeys);
Log.d ("testings", "blackKeys: "+numberBlackKeys);
// gets the main slices paths
///////////////////////////////////////////
for (int i = 0; i <= numberWhiteKeys; i++) {
pianoPathsArray.add(getSlicesPathsWhite(i, numberWhiteKeys));
}
for (int i = 0; i <= numberBlackKeys; i++) {
pianoPathsArray.add(getSlicesPathsBlack(i, numberBlackKeys, numberWhiteKeys));
}
//draw frame
framePath = new ChessSlice();
framePath.moveTo(0, 0);
framePath.lineTo(width, 0);
framePath.lineTo(width, height);
framePath.lineTo(0, height);
framePath.lineTo(0, 0);
}
private int getBlackKeysCount(int overAllKeys) {
int allKeys = overAllKeys;
int blackKeys = 0;
while (allKeys > 12) {
blackKeys = blackKeys + 5;
allKeys = allKeys - 12;
}
if (allKeys == 12) {
blackKeys = blackKeys + 5;
} else if (allKeys == 11) {
blackKeys = blackKeys + 5;
} else if (allKeys == 10) {
blackKeys = blackKeys + 4;
}else if (allKeys == 9) {
blackKeys = blackKeys + 4;
}else if (allKeys == 8) {
blackKeys = blackKeys + 3;
}else if (allKeys == 7) {
blackKeys = blackKeys + 3;
}else if (allKeys == 6) {
blackKeys = blackKeys + 2;
}else if (allKeys == 5) {
blackKeys = blackKeys + 2;
}else if (allKeys == 4) {
blackKeys = blackKeys + 2;
}else if (allKeys == 3) {
blackKeys = blackKeys + 1;
}else if (allKeys == 2) {
blackKeys = blackKeys + 1;
}else if (allKeys == 1) {
blackKeys = blackKeys + 0;
}
return blackKeys;
}
private int getWhiteKeysCount(int overAllKeys) {
int allKeys = overAllKeys;
int whiteKeys = 0;
while (allKeys > 12) {
whiteKeys = whiteKeys + 7;
allKeys = allKeys - 12;
}
if (allKeys == 12) {
whiteKeys = whiteKeys + 8;
} else if (allKeys == 11) {
whiteKeys = whiteKeys + 7;
} else if (allKeys == 10) {
whiteKeys = whiteKeys + 6;
}else if (allKeys == 9) {
whiteKeys = whiteKeys + 6;
}else if (allKeys == 8) {
whiteKeys = whiteKeys + 5;
}else if (allKeys == 7) {
whiteKeys = whiteKeys + 5;
}else if (allKeys == 6) {
whiteKeys = whiteKeys + 4;
}else if (allKeys == 5) {
whiteKeys = whiteKeys + 3;
}else if (allKeys == 4) {
whiteKeys = whiteKeys + 2;
}else if (allKeys == 3) {
whiteKeys = whiteKeys + 2;
}else if (allKeys == 2) {
whiteKeys = whiteKeys + 1;
}else if (allKeys == 1) {
whiteKeys = whiteKeys + 1;
}
return whiteKeys;
}
public ChessSlice getSlicesPathsWhite(int i, int numberWhiteKeys) {
int KeyWidth = width / numberWhiteKeys;
int rowHeight = height;
int startX = 0+(i*KeyWidth);
int startY = 0;
ChessSlice segmentPath = new ChessSlice();
segmentPath.moveTo(startX, startY);
segmentPath.lineTo(startX+KeyWidth, startY);
segmentPath.lineTo(startX+KeyWidth, startY+rowHeight);
segmentPath.lineTo(startX, startY+rowHeight);
segmentPath.lineTo(startX, startY);
segmentPath.key = getCorrectKeyForNumber(i);
segmentPath.octave = getCorrectOctavForNumber(i);
segmentPath.isKeyBlack = false;
return segmentPath;
}
public ChessSlice getSlicesPathsBlack(int i, int numberBlackeys, int numberWhiteKeys) {
int KeyWidth = width / numberWhiteKeys;
int rowHeight = height/2;
int modifierForBlackKeys = getBlackKeyModifier(i);
int startX = (KeyWidth/2)+(i*KeyWidth)+(modifierForBlackKeys*KeyWidth);
int startY = 0;
ChessSlice segmentPath = new ChessSlice();
segmentPath.moveTo(startX, startY);
segmentPath.lineTo(startX+KeyWidth, startY);
segmentPath.lineTo(startX+KeyWidth, startY+rowHeight);
segmentPath.lineTo(startX, startY+rowHeight);
segmentPath.lineTo(startX, startY);
segmentPath.key = getCorrectBlackKeyForNumber(i);
segmentPath.octave = getCorrectBlackOctavForNumber(i);
segmentPath.isKeyBlack = true;
return segmentPath;
}
private int getBlackKeyModifier(int i) {
int modifier = 0;
if (i >= 10) {
modifier = 4;
}else if (i >= 7) {
modifier = 3;
} else if (i >= 5) {
modifier = 2;
} else if (i >= 2) {
modifier = 1;
}
return modifier;
}
private String getCorrectKeyForNumber (int number) {
int functionNum = number;
String key = "C";
while (functionNum > 6) {
functionNum = functionNum - 7;
}
if (functionNum == 0) {
key = "C";
}else if (functionNum == 1) {
key = "D";
}else if (functionNum == 2) {
key = "E";
}else if (functionNum == 3) {
key = "F";
}else if (functionNum == 4) {
key = "G";
}else if (functionNum == 5) {
key = "A";
}else if (functionNum == 6) {
key = "B";
}
return key;
}
private String getCorrectBlackKeyForNumber (int number) {
int functionNum = number;
String key = "C#";
while (functionNum > 4) {
functionNum = functionNum - 5;
}
if (functionNum == 0) {
key = "C#";
}else if (functionNum == 1) {
key = "D#";
}else if (functionNum == 2) {
key = "F#";
}else if (functionNum == 3) {
key = "G#";
}else if (functionNum == 4) {
key = "A#";
}
return key;
}
private int getCorrectOctavForNumber (int number) {
int functionNum = number;
int octave = 0;
while (functionNum > 6) {
octave = octave + 1;
functionNum = functionNum - 7;
}
return octave;
}
private int getCorrectBlackOctavForNumber (int number) {
int functionNum = number;
int octave = 0;
while (functionNum > 4) {
octave = octave + 1;
functionNum = functionNum - 5;
}
return octave;
}
// private TouchModel calculateTouchKey (Float touchX, Float touchY) {
//
// int columWidth = width/COLUMS_COUNT;
// int rowHeight = height/ROWS_COUNT;
//
// int selectectColum = (int) Math.floor(touchX/columWidth)+1;
// int selectectRow = (int) Math.floor(touchY/rowHeight)+1;
//
// TouchModel touchModel = new TouchModel(selectectColum, selectectRow);
//
//
// mainInterface.widgetTouched(selectectColum, selectectRow);
//
// return touchModel;
// }
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_DOWN:
touchX = event.getX();
touchY = event.getY();
isTouching = true;
// Log.d ("Testings", "Touch Event: "+event);
// calculateTouchKey(touchX, touchY);
// Log.d (TAG, "touched: "+touchX+" and: "+touchY);
// Log.d (TAG, "x: "+selectedCells.touchX+" y: "+selectedCells.touchY);
break;
default:
isTouching = false;
}
invalidate();
return true;
}
public void initlizeWidgetVars (PianoKeysInterface chessBoardInterface) {
mainInterface = chessBoardInterface;
mainInterface.setChessArray(pianoPathsArray);
}
public void updateChessArray(ArrayList<ChessSlice> localChessArray) {
pianoPathsArray = localChessArray;
this.postInvalidate();
}
}
答案 0 :(得分:0)
通过以下方式修复它:
for (int i = 0; i < pianoPathsArray.size(); i++) {
RectF boundrySlice=new RectF();
pianoPathsArray.get(i).computeBounds(boundrySlice, true);
if(boundrySlice.contains(touchX,touchY)){
selectedPath= pianoPathsArray.get(i);// where selectedPath is declared Globally.
}
}