1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
package com.example.ando_dessin_test;
//merci a http://supertos.free.fr/supertos.php?page=1066
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Desactiver la barre de titre de notre application
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Passer la fenêtre en full-creen == cacher la barre de notification
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new RenderView(this));
}
// Création d'une classe interne RenderView pour gérer un affichage simple
// permettant
// de montrer que nous occupons bien tout l'écran
class RenderView extends View {
public RenderView(Context context) {
super(context);
}
// Dessinons sur la totalité de l'écran
protected void onDraw(Canvas canvas) {
canvas.drawRGB(0, 0, 0);
// Instance de Paint pour définir l'attribut couleur de notre point,
// ainsi que
// sa taille.
Paint paint = new Paint();
// Nous allons dessiner nos points par rapport à la résolution de
// l'écran
int iWidth = canvas.getWidth(); // Largeur
int iHeight = canvas.getHeight(); // Hauteur
// float radius = 0;//Radius caused an error so I initialized this
// variable
Random rand = new Random();
// j'ai l'impression detre dans les etoiles avec ces petits points
// Affichons 10000 points de toutes les couleurs
for (int i = 0; i < 10000; i++) {
// Affecter une couleur de manière aléatoire
paint.setARGB(255, rand.nextInt(256), rand.nextInt(256),
rand.nextInt(256));
// Puis dessiner nos points dans le canevas
canvas.drawPoint(rand.nextInt(iWidth), rand.nextInt(iHeight),
paint);
}
// aficchons notre fameux cercle
// Canvas canvas = new Canvas();
Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(Color.RED);
circlePaint.setStyle(Style.STROKE);
circlePaint.setStrokeWidth(10);
float rayonDuCercle = canvas.getWidth() / 2;
canvas.drawCircle((float) canvas.getWidth() / 2,
(float) canvas.getHeight() / 2, rayonDuCercle, circlePaint);
float rayonDuCerclePetit = canvas.getWidth() / 6;
canvas.drawCircle((float) canvas.getWidth() / 2,
(float) canvas.getHeight() / 2, rayonDuCerclePetit,
circlePaint);
/*
* // Random rand = new Random(); //Affichons 100 segments de toutes
* les couleurs for (int i=0; i < 100; i++) { // Affecter une
* couleur de manière aléatoire paint.setARGB(255,
* rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); //
* Définir l'épaisseur du segment paint.setStrokeWidth
* (rand.nextInt(10)); // Puis dessiner nos points dans le cavenas
* canvas.drawLine(rand.nextInt(iWidth), rand.nextInt(iHeight),
* rand.nextInt(iWidth), rand.nextInt(iHeight), paint); }
*/
paint.setARGB(255, rand.nextInt(256), rand.nextInt(256),
rand.nextInt(256));
// Définir l'épaisseur du segment
paint.setStrokeWidth(2);
// Puis dessiner nos points dans le cavenas
// okcanvas.drawLine((float)((float)Math.cos(0)*rayonDuCerclePetit)+canvas.getWidth()/2,
// canvas.getHeight()/2,canvas.getWidth()/2+rayonDuCercle,
// canvas.getHeight()/2, paint);
// okcanvas.drawLine((float)((float)Math.cos(Math.PI/2)*rayonDuCerclePetit)+canvas.getWidth()/2,
// ((float)Math.sin(Math.PI/2)*rayonDuCerclePetit)+canvas.getHeight()/2,canvas.getWidth()/2+rayonDuCercle*(float)Math.cos(Math.PI/2),
// rayonDuCercle*(float)Math.sin(Math.PI/2)+canvas.getHeight()/2,
// paint);
// okkk
// canvas.drawLine((float)((float)Math.cos(Math.PI/4)*rayonDuCerclePetit)+canvas.getWidth()/2,
// ((float)Math.sin(Math.PI/4)*rayonDuCerclePetit)+canvas.getHeight()/2,canvas.getWidth()/2+rayonDuCercle*(float)Math.cos(Math.PI/4),
// rayonDuCercle*(float)Math.sin(Math.PI/4)+canvas.getHeight()/2,
// paint);
// initPaint(paint,);
// pourquoi j'utilise 2 boucles ? Parce que pour limiter l'espace
// memoire j'utilise le même canva et la seul difference entre ces
// deux element est la couleur.
// cette boucle affiche des rayons
for (int i = 0; i < 8; i++) {
setRayon(canvas, (Math.PI / 4) * i, paint, rayonDuCercle,
rayonDuCerclePetit);
}
paint.setTextAlign(Align.RIGHT);
// cette boucle affichage du texte
for (int i = 1; i < 9; i++) {
setText(canvas, paint, String.valueOf(i), (Math.PI / 4) * i,
rayonDuCercle * 0.7, 80);
// si je suis le dernier element j'affiche le 9 du centre
if (i == 8) {
paint.setTextAlign(Align.CENTER);
setText(canvas, paint, String.valueOf(i + 1), (Math.PI / 4)
* i, rayonDuCercle * 0, 80);
}
}
}
/*
* info pour unen raison que j'ai pas chercher a comprendre l'angle
* positif s'affiche comme si c'été un ange negatif . Donc je bidouille
* en multipliant l'angle par -1
*/
void setRayon(Canvas canvas, double angleRad, Paint paint,
float rayonDuCercle, float rayonDuCerclePetit) {
// angleRad=angleRad*-1;
angleRad = (Math.toRadians((double) 70) + angleRad) * -1;
canvas.drawLine(
(float) ((float) Math.cos(angleRad) * rayonDuCerclePetit)
+ canvas.getWidth() / 2,
((float) Math.sin(angleRad) * rayonDuCerclePetit)
+ canvas.getHeight() / 2,
canvas.getWidth() / 2 + rayonDuCercle
* (float) Math.cos(angleRad),
rayonDuCercle * (float) Math.sin(angleRad)
+ canvas.getHeight() / 2, paint);
}
/*
* info pour unen raison que j'ai pas chercher a comprendre l'angle
* positif s'affiche comme si c'été un ange negatif . Donc je bidouille
* en multipliant l'angle par -1
*/
void setText(Canvas canvas, Paint paint, String Txt, double angleRad,
double rayonTxt, int sizeText) {
paint.setColor(Color.WHITE);
paint.setTextSize(sizeText);
angleRad = (Math.toRadians((double) 50) + angleRad) * -1;
// axe des ordonees enhaut a gauche -> canvas.drawText(Txt, (float)
// Math.cos(angleRad)*(float) rayonTxt,
// (float)Math.sin(angleRad)*(float) rayonTxt, paint);
// ligne suivante j'ai centree l'axe
canvas.drawText(Txt, (float) Math.cos(angleRad) * (float) rayonTxt
+ canvas.getWidth() / 2, (float) Math.sin(angleRad)
* (float) rayonTxt + canvas.getHeight() / 2, paint);
}
/*
* private void initPaint(Paint paint, Drawable drawable, float
* textHeight, float baselineOffset) { paint = new Paint();
* paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.BLUE); paint
* = new Paint(); paint.setStyle(Paint.Style.FILL_AND_STROKE);
* paint.setAntiAlias(true); paint.setColor(Color.WHITE);
* paint.setTextSize(30); paint.setTextAlign(Align.CENTER); FontMetrics
* fontMetrics = paint.getFontMetrics(); baselineOffset =
* fontMetrics.bottom; // bottom is the maximum amount that the text
* descends // I'm not sure why this is different from descent...
* textHeight = fontMetrics.bottom - fontMetrics.top; // drawable =
* getResources().getDrawable(R.drawable.ic_launcher); }
*/
}
} |
Partager