Jump to content

[résolut] Dessin rectangles android


free-tibet
 Share

Recommended Posts

salut à tous !

je suis actuellement sur un projet ou je veux travailler sur une image.

je dispose d'une image en fond, j'aimerais recouvrir par dessus une surface noir translucide, mais avec une partie complètement translucide.

Voici une image du résultat final que je souhaiterai avoir:

720162Sanstitre.png

j'ai bien sur penser à dessiner 4 rectangles tout autour, mais j'espère obtenir une meilleur solution.

Pouvez-vous m'aider ? Merci.

Edited by free-tibet
Link to comment
Share on other sites

En fait je veux selectionner une partie d'une image, je dois pouvoir modifier la position et la taille du cadre de sélection.

J'ai finalement utilisé la solution de dessiner 4 rectangles autour de la partie à sélectionner.

Pour ceux que ça intéresse voici le code de la classe que j'ai créé:

public class TrimRectView extends ImageView {
public TrimRectView(Context context) {
 super(context);
}
private int mTotalWidth, mTotalHeight, mStartX, mStartY, mSelectionWidth, mSelectionHeight;

public void updateRect(int startX, int startY, int sizeX, int sizeY, int totalWidth, int totalHeight) {
 mStartX = startX;
 mStartY = startY;
 mSelectionWidth = sizeX;
 mSelectionHeight = sizeY;
 mTotalWidth = totalWidth;
 mTotalHeight = totalHeight;
 this.postInvalidate();
}

public void onDraw(Canvas c) {
 Paint myPaint = new Paint();
 //rectangle gauche
 myPaint.setColor(Color.argb(100, 0, 0, 0));
 c.drawRect(0, 0, mStartX, mTotalHeight, myPaint);
 //rectangle haut
 myPaint.setColor(Color.argb(100, 0, 0, 0));
 c.drawRect(mStartX, 0, mTotalWidth, mStartY, myPaint);

 //rectangle droite
 myPaint.setColor(Color.argb(100, 0, 0, 0));
 c.drawRect(mStartX+mSelectionWidth, mStartY, mTotalHeight, mTotalHeight, myPaint);
 //rectangle bas
 myPaint.setColor(Color.argb(100, 0, 0, 0));
 c.drawRect(mStartX, mStartY+mSelectionHeight, mStartX+mSelectionWidth, mTotalHeight, myPaint);
}
}

ensuite il suffit d'ajouter un capteur d'évènement de type "onTouchListener" en appelant updateRect()...

ça fonctionne très bien !

Merci quand même !

Link to comment
Share on other sites

suite à ça je me suis poser une question. Je me suis toujours dis que j'avais la réponse mais j'aimerais une confirmation.

les classes de type "View" disposent de méthodes "invalidate()" et "postInvalidate()".

je me suis toujours dis ceci:

invalidate() doit être appeler dans le thread principale, le threadUI, ou alors depuis un thread lancer depuis "runOnUiThread()"

postInvalidate() peut être appeler de n'importe quel thread.

cette affirmation est-elle correcte ? Merci !

Edited by free-tibet
Link to comment
Share on other sites

invalidate() doit être appeler dans le thread principale, le threadUI, ou alors depuis un thread lancer depuis "runOnUiThread()"

postInvalidate() peut être appeler de n'importe quel thread.

cette affirmation est-elle correcte ? Merci !

C'est exactement ce que dit la documentation, en tout cas ;)

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...