събота, октомври 23, 2010

Android - simplest way to draw image on screen

Main Activity class -> TestDraw.java
package com.testdraw;

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

public class TestDraw extends Activity
{
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(new TestDrawView(this));
  }
}
At least one View is needed to display something on the screen -> TestDrawView.java
package com.testdraw;

import android.view.View;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.*;
import android.graphics.drawable.*;

class TestDrawView extends View
{
  private Context mContext = null;
 
  public TestDrawView(Context context)
  {
    super(context);
    mContext = context;
  }
 
  protected void onDraw(Canvas canvas)
  {
    Resources res = mContext.getResources();
    Bitmap img = BitmapFactory.decodeResource(res, R.drawable.hooker);
    canvas.drawBitmap(img, 0, 0, null);
  }
}
The image that is drawn is a file taken from the res folder of the project: /res/drawable/hooker.png

Няма коментари: