How to use canvas in your android apps - Part 2
In the second part of this tutorial series, I will show you what you can do with canvas and paint.

If you complete the first part, your onDraw method in Panel class is similar as this:
We will use the the Paint type object this time. You can use it to draw lines, circles and text on your canvas. For example this two lines will draw a red circle for you.
- canvas.drawCircle(20, 50, 25, paint);
A blue circle with antialiasing turned on:
- paint.setAntiAlias(true);
- canvas.drawCircle(60, 50, 25, paint);
Now we will create some triangles. Create a Path object to store our triangle's segments. Use .offset to draw in many locations.
- paint.setStrokeWidth(2);
- Path path = new Path();
- path.moveTo(4, -10);
- path.lineTo(20, 0);
- path.lineTo(-9, 0);
- path.close();
- path.offset(60, 40);
- canvas.drawPath(path, paint);
- path.offset(90, 100);
- canvas.drawPath(path, paint);
- path.offset(80, 150);
- canvas.drawPath(path, paint);
Now draw a text using FILL style:
You can rotate the text, and you can draw it where you want:
Draw bounding rect before rotating text:
- Rect rect = new Rect();
- paint.getTextBounds(rotatedtext, 0, rotatedtext.length(), rect);
- canvas.translate(x, y);
- canvas.drawText("Rotated helloandroid :)", 0, 0, paint);
- canvas.drawRect(rect, paint);
- canvas.translate(-x, -y);
- canvas.rotate(-45, x + rect.exactCenterX(),y + rect.exactCenterY());
- canvas.drawText(rotatedtext, x, y, paint);
Now draw a draw a thick dashed line in angle:
- DashPathEffect dashPath = new DashPathEffect(new float[]{10,40}, 1);
- paint.setPathEffect(dashPath);
- paint.setStrokeWidth(8);
- canvas.drawLine(0, 60 , 320, 300, paint);
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
25 weeks 6 days ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
25 weeks 6 days ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
25 weeks 6 days ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
25 weeks 6 days ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
25 weeks 6 days ago
Poll
Useful resources
Android Development Projects
- Publish Increase the number of downloads on Google Play by jaumin6
- Solution for issue required: Launch of Video Player from Website in webview by vijayknarang
- Shootle Mobile Suite 1.0 by Shootle
- Publish One Android App - 15th by leelida
- Photo Sharing App for Iphone and Android. by defclanng
- Developing UI for an external uvc-usb based camera to be interfaced with the Android Devices. : by saurabhdixit1987
- Nonpublic project #4551034 by Barron1408
- New Web Design by waynebenson
- Codes for a mobile app by mobipromo360
- Mobile Trackng App Refinement by BIOMEDESQCEO



