Upload
bruno-delb
View
57
Download
2
Embed Size (px)
Citation preview
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
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.
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);
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);
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);
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>
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);
}
}
Testez sur votre mobile
Canvas_DrawText
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