Back to project page Canvas-Test.
The source code is released under:
Apache License
If you think the Android project Canvas-Test listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package foobar.canvastest.drawing.scenes; /*w w w .ja v a 2 s.co m*/ import android.graphics.Canvas; import android.graphics.Paint; import foobar.canvastest.drawing.Drawable; /** * An example of a scene you can draw. Although some android projects like to put * this code in the SurfaceView, for me it is convenient to put all the drawing * code in its own class. It hides all that boilerplate code, and gives the added * bonus of being able to switch out the scene being drawn while reusing the same view. * * To make your own scene, create a new class that implements Drawable. Do what you need * to in the draw() method to produce your image. To put your picture or animation on * to the screen, call * yourMySurfaceViewInstance.setScene(new YourSceneClass()); * * You can do this, for example, when a button is clicked, or, for simplicity, when the * surface is created by MainCanvasActivity. */ public class ExampleScene implements Drawable { //A few "paint brushes" to draw with private Paint mPaint1 = new Paint(); private Paint mPaintText = new Paint(); private Paint mPaintBg = new Paint(); /** * Create the drawing. */ public void draw(Canvas c) { //Save the basic matrix state. This is the state of all rotations, translations, scale operations, etc. c.save(); c.drawColor(0xff000000); //Draw a blue background c.drawPaint(mPaintBg); c.drawText("Hello Drawing!", 20f, 100f, mPaintText); c.drawLine(100, 200, 200, 350, mPaint1); c.drawLine(100, 350, 200, 200, mPaint1); //Restore the basic matrix state. c.restore(); } public ExampleScene() { //Initialize paint styles mPaintText.setColor(0xffffffff); mPaintText.setAntiAlias(true); mPaintText.setStrokeWidth(1); mPaintText.setStrokeCap(Paint.Cap.ROUND); mPaintText.setStyle(Paint.Style.FILL_AND_STROKE); mPaintText.setTextAlign(Paint.Align.LEFT); mPaintText.setTextSize(50); mPaint1.setColor(0xbb33bbff); mPaint1.setAntiAlias(true); mPaint1.setStrokeWidth(10); mPaint1.setStrokeCap(Paint.Cap.ROUND); mPaint1.setStyle(Paint.Style.FILL_AND_STROKE); mPaintBg.setColor(0x990099ff); } }