9
Développer sur Android Android Lab Test www.AndroidLabTest.com Facebook Par Bruno Delb www.youtube.com/androidlabtest www.twitter.com/brunodelb | www.facebook.com/brunodelb | blog.brunodelb.com www.facebook.com/Androidlabtest Youtube Site officiel Leçon : Le Canvas, affichage d’un texte

Android Lab Test : Ecrire un texte sur le canevas (français)

Embed Size (px)

Citation preview

Page 1: Android Lab Test : Ecrire un texte sur le canevas (français)

Développer sur AndroidAndroid Lab Test

www.AndroidLabTest.com

Face

bo

ok

Par Bruno Delb

www.youtube.com/androidlabtest

www.twitter.com/brunodelb | www.facebook.com/brunodelb | blog.brunodelb.com

www.facebook.com/Androidlabtest

You

tub

e

Site

off

icie

l

Leçon : Le Canvas, affichage d’un texte

Page 2: Android Lab Test : Ecrire un texte sur le canevas (français)

Le Canvas, affichage d’un texte

• Dans cette leçon, vous allez apprendre à afficher un

texte.

• Pour cela, vous allez utiliser la méthode drawText(), le

Bitmap, le Paint et le Canvas.

Page 3: Android Lab Test : Ecrire un texte sur le canevas (français)

Le Canvas, affichage d’un texte

• Avant tout, créez un Bitmap de la taille de l’écran avec la méthode createBitmap().

Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager().getDefaultDisplay().getWidth(), (int) getWindowManager().getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888

);

• Puis pour charger le bitmap dans un ImageView, utilisez la méthode setImgeBitmap().

imageView.setImageBitmap(bitmap);

Page 4: Android Lab Test : Ecrire un texte sur le canevas (français)

Le Canvas, affichage d’un texte

• Créez un Paint. Le Paint décrit le style du dessin qui va être fait

(dans notre cas, c’est l’affichage d’un texte).

Paint paint = new Paint();

paint.setColor(Color.BLUE);

paint.setTextSize(30);

Page 5: Android Lab Test : Ecrire un texte sur le canevas (français)

Le Canvas, affichage d’un texte

• Créez un Canvas basé sur un bitmap.

Canvas canvas = new Canvas(bitmap);

• Puis affichez le texte avec la méthode drawText() en utilisant le

Paint.

canvas.drawText("Helloworld", 10, 50, paint);

Page 6: Android Lab Test : Ecrire un texte sur le canevas (français)

Layout main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<ImageView

android:id="@+id/imageView"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

</LinearLayout>

Page 7: Android Lab Test : Ecrire un texte sur le canevas (français)

Fichier Main.java

public class Main extends Activity {

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ImageView imageView = (ImageView) this.findViewById(R.id.imageView);

Bitmap bitmap = Bitmap.createBitmap(

(int) getWindowManager().getDefaultDisplay().getWidth(),

(int) getWindowManager().getDefaultDisplay().getHeight(),

Bitmap.Config.ARGB_8888

);

imageView.setImageBitmap(bitmap);

Paint paint = new Paint();

paint.setColor(Color.BLUE);

paint.setTextSize(30);

Canvas canvas = new Canvas(bitmap);

canvas.drawText("Helloworld", 10, 50, paint);

}

}

Page 8: Android Lab Test : Ecrire un texte sur le canevas (français)

Testez sur votre mobile

Canvas_DrawText

Page 9: Android Lab Test : Ecrire un texte sur le canevas (français)

Retrouvez-moi sur ma chaîne AndroidLabTest …

Sur ma chaîne Youtubehttps://www.youtube.com/user/mobiledevlabtest

Qui suis-je ?

Bruno Delb,

auteur du 1er livre francophone de développement d’application Java sur mobile (2002),

développeur d’applications mobiles & sociales,

parlez-moi de vos projets.

Et bien sûr sur mon site Web :http://blog.brunodelb.com