Draw Shapes, Text and Bitmap

Description

The following code shows how to Draw Shapes, Text and Bitmap.

Example

Main Activity Java code


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.View;
/* w  ww.  j a v  a 2  s . c om*/
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ShapesAndTextBitmapView view = new ShapesAndTextBitmapView(this);
      setContentView(view);
   }
}
class ShapesAndTextBitmapView extends View {

   private Paint paint;
   private Bitmap bitmap;

   public ShapesAndTextBitmapView(Context context) {
      super(context);
      bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.copter);
   }

   @Override
   protected void onDraw(Canvas canvas) {
      canvas.drawRGB(0, 0, 0);
      drawShapes(canvas);
      drawBitmap(canvas);
   }

   private void drawShapes(Canvas canvas) {
      int side = canvas.getWidth() / 5;
      paint = new Paint();
      paint.setARGB(255, 255, 0, 0);
      canvas.drawRect(side, canvas.getHeight() - 60 - side, side + side, canvas.getHeight() - 60, paint);
      paint.setARGB(255, 0, 255, 0);
      canvas.drawCircle(side * 2 + side / 2, canvas.getHeight() - 60 - side / 2, side / 2, paint);
      paint.setARGB(255, 0, 0, 255);
      paint.setStyle(Paint.Style.FILL);
      Path triangle = new Path();
      triangle.moveTo(side * 3 + 30, canvas.getHeight() - 60 - side);
      triangle.lineTo(side * 3 + 60, canvas.getHeight() - 60);
      triangle.lineTo(side * 3, canvas.getHeight() - 60);
      triangle.lineTo(side * 3 + 30, canvas.getHeight() - 60 - side);
      canvas.drawPath(triangle, paint);
   }

   private void drawBitmap(Canvas canvas) {
      paint = new Paint();
      canvas.drawBitmap(bitmap, 0, 0, paint);
   }
}
Draw Shapes, Text and Bitmap




















Home »
  Android »
    Android UI »




UI Basics
Action Bar
Animation
Button
Canvas
CheckBox
Clock Date Picker
Dialog
EditText
Event
Fragment
Gesture
GridView
ImageView
Layout
ListView
Map
Menu
Model
OpenGL
ProgressBar
RadioButton
Spinner
Tab
TextView
Thread
Toast
Video
View
WebView