Android Adventures - Beginning Bitmap Graphics
Written by Mike James   
Monday, 27 July 2015
Article Index
Android Adventures - Beginning Bitmap Graphics
A First Graphic
Simple Animation
Timer and threads
Listing and Summary

Listing

The complete listing of the animation program is:

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends ActionBarActivity {

 private int width=800, height=800;

 private float x=463,y=743,vx=1,vy=1,r=30;
 private Canvas c;
 private Paint paint;
 private ImageView imageview;


 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);


  Bitmap b = Bitmap.createBitmap(width,    
                height,Bitmap.Config.ARGB_8888);
  c = new Canvas(b);
  c.drawColor(Color.WHITE);

  paint = new Paint();
  paint.setAntiAlias(true);
  paint.setStyle(Paint.Style.FILL);

  imageview=(ImageView) findViewById(R.id.imageView);
  imageview.setImageBitmap(b);
Timer timer=new Timer();
 
  timer.schedule(
   new TimerTask(){

    @Override
    public void run() {

     runOnUiThread(
      new Runnable() {
       @Override
       public void run() {
        update();
       }
      }
     );
    }
   },0,10 );
 }

 void update(){
  paint.setColor(Color.WHITE);
  c.drawCircle(x, y, r, paint);
  x=x+vx;
  y=y+vy;
  if(x+r>=width)vx=-vx;
  if(x-r<=0)vx=-vx;
  if(y+r>=height)vy=-vy;
  if(y-r<=0)vy=-vy;
  paint.setColor(Color.RED);
  c.drawCircle(x, y, r, paint);
  imageview.invalidate();

  }

} 

Where Next

There is so much to learn about graphics it is difficult to pick out things you need to know. If you want to find out more about how the standard UI works you need to look into the OnDraw event and how to create your own View object that render graphics. You need to find out about Android's vector graphics using shapes and path. You need to know about the different types of animation that are available and eventually you need to learn about OpenGL and its support for hardware accelerated 2D and 3D graphics. 

Summary

  • The subject of Android graphics is huge and there is always more than one way to approach any task. This chapter is a first look at what you might call UI-based graphics.

  • The Bitmap is the basic graphics object. It consists of a rectangle of pixels that can be set to any color.

  • The ImageView is a general purpose UI component that can be used to display a range of graphics objects including a Bitmap.

  • You can draw on a Bitmap using a Canvas object which has a large number of different drawing methods.

  • The color and drawing style used by many of the Canvas methods is determined by the properties of a Paint object.
     
  • The Canvas object also supports transformations which can be use to modify where a graphic is drawn, its size, rotation etc.

  • Transformations can be used to standardize the drawing of graphics objects at the origin.

  • Transformations can also be used to change the default pixel coordinate system to anything you want to use.

  • Simple animation is possible using nothing but a Bitmap, Canvas and an ImageView

  • Only the UI thread can modify the UI.

  • The Android Timer can be used to animate 2D graphics but you have ensure that its runs the code on the UI thread using the RunOnUIThread method. 

You can download the code for the animation program from the CodeBin. (Note you have to register first).

 

androidJavaSmallAndroid Programming In Java:
Starting With an App
Third Edition

Is now available in paperback and ebook.

Available from Amazon.

 

 

  1. Getting Started With Android Studio 3
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Android Events
         Extract: Using Lambdas 
  5. Basic Controls
  6. Layout Containers
  7. The ConstraintLayout
        Extract: Guidelines and Barriers
  8. UI Graphics A Deep Dive
        Extract: Programming the UI ***NEW
  9. Menus & The Action Bar
  10. Menus, Context & Popup
  11. Resources
  12. Beginning Bitmap Graphics
        Extract: Simple Animation
  13. Staying Alive! Lifecycle & State
  14. Spinners
  15. Pickers
  16. ListView And Adapters

If you are interested in creating custom template also see:

Custom Projects In Android Studio

Androidgears

 

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter,FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

pythondata

 



 

Comments




or email your comment to: comments@i-programmer.info

 



Last Updated ( Wednesday, 12 October 2016 )