PowenKo, Android Tutorial 087, canvas in your android apps


package com.powenko.Tutorial_UI_canvas_text_circle_line_image;




import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class MyCanvas extends View {

	public MyCanvas(Context context, AttributeSet attrs) {
	        super(context, attrs);
	     
	 
	}
	public MyCanvas(Context context) {
        super(context);
     
 
}
	@Override
	public void onDraw(Canvas canvas) {
	       
	        Paint paint = new Paint();
	       
	
	        // draw image
	        Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
	                        R.drawable.icon);
	        canvas.drawColor(Color.BLACK);
	        canvas.drawBitmap(kangoo, 130, 10, null);
	        
	        // draw circle
	        paint.setColor(Color.RED);
	        canvas.drawCircle(30, 50, 25, paint);
	        
	        // draw Anti Alias circle
	        paint.setAntiAlias(true);
	        paint.setColor(Color.BLUE);
	        canvas.drawCircle(100, 50, 25, paint);
	        
	        //   
	        paint.setStyle(Paint.Style.FILL_AND_STROKE);
            paint.setStrokeWidth(2);
            paint.setColor(Color.LTGRAY );
            Path path = new Path();
            path.moveTo(4, -11);
            path.lineTo(20, 0);
            path.lineTo(-11, 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);
	        
            
            // text 
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            paint.setTextSize(20);
            canvas.drawText("PowenKo.com ,1", 50, 130,paint);
            
            // text 2
            paint.setColor(Color.GRAY);
            paint.setTextSize(25);
            String rotatedtext = "PowenKo.com ,2";
	        
            // text3 
            int x = 50;
            int y = 185;
            Rect rect = new Rect();
            paint.getTextBounds(rotatedtext, 0, rotatedtext.length(), rect);
            canvas.translate(x, y);
            paint.setStyle(Paint.Style.FILL);
             
            canvas.drawText("PowenKo.com ,3", 0, 0, paint);
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawRect(rect, paint);     
            canvas.translate(-x, -y);
             
                                   
            paint.setColor(Color.RED);
            canvas.rotate(-45, x + rect.exactCenterX(),y + rect.exactCenterY());
            paint.setStyle(Paint.Style.FILL);
            canvas.drawText(rotatedtext, x, y, paint);
            
	}
}
package com.powenko.Tutorial_UI_canvas_text_circle_line_image;


import android.app.Activity;
import android.os.Bundle;

public class Tutorial_UI_canvas_text_circle_line_imageActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);      
        MyCanvas b = new MyCanvas(this);       
        setContentView(b);       
    }
 
    
}

Leave a Reply