J'ai chargé le premier bitmap (image1) sur la toile ie taille 320 X 312. Le second bitmap (image2) est 41 x 41 chargé à x, y - 235, 147. chevauché au-dessus de l'image1. Maintenant, quand je fais une ligne de dessin de x, y - 174, 254 à x, y - 249, 177 dans l'événement tactile. La ligne à destination 249, 177 est "en dessous" de l'image2 dessinée à 235,147 donc les derniers 14 ou alors pixels de la ligne tracée sont cachés sous image2. J'espère que l'a expliqué clairement. Comment puis-je afficher la longueur totale de la ligne?Dessiner sur la classe basée sur la vue - Android
public GameView(Context context) {
super(context);
setFocusable(true);
bMap = BitmapFactory.decodeResource(getResources(),
R.drawable.image1);
bMapMutable = bMap.copy(Bitmap.Config.ARGB_8888, true);
mCanvas = new Canvas();
mPath = new Path();
bMap2 = BitmapFactory.decodeResource(getResources(),
R.drawable.image2);
bMapMutable2 = bMap2.copy(Bitmap.Config.ARGB_8888, true);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(bMapMutable, 0, 0, null);
float left = (float) ((scale == 1) ? 235 : 235 * 1.5);
float top = (float) ((scale == 1) ? 147 : 147 * 1.5);
canvas.drawBitmap(bMapMutable2, left, top, null);
canvas.drawPath(mPath, mPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (touchCount <= 2)
{
touchCount += 1;
touch_start(x, y);
invalidate();
}
if (touchCount == 2)
{
if (scale == 1.0)
{
mPath.moveTo(174,254);
}
else
{
mPath.moveTo(261,381);
}
if (scale == 1.0)
mPath.lineTo(249, 177);
else
mPath.lineTo(374, 266);
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
touchCount = 0;
}
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
if (touchCount == 1)
{
mX = x;
mY = y;
}
else
{
mX2 = x;
mY2 = y;
}
}
Semble que vous travaillez avec des valeurs de pixels. Êtes-vous sûr de ne pas utiliser de pixels indépendants de la densité (dip) à la place? – mibollma
Je suis d'accord mibollma. Ceci est juste un test pour le faire fonctionner. – David