![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOhTCu29hyphenhyphen0XodSqDISq8Y11F-FP94A_EX7IWBweyqHeUIAwgqmSJeL4DAHlVwyR5yXleHb0_f7b9_3Blt6__Yij4USmoreIvKoMPfs0B6JYnq9FjTjG5kDQD_Dgg4iH9Ha55oUGlnEjs/s400/Photo+on+2011-03-12+at+22.07.jpg)
Simplest possible android graphic example - display an image (i.e lotus in the drawable-hdpi folder) make liness create the canvas, use paint, for loop for alpha channel etc
done in Eclipse. Second example below that draws image where user touches screen
//thanks to http://www.droidnova.com/2d-tutorial-series-part-ii,772.html
I've also been reading O'Reilly's Android Application Development, its got a little bit on Graphics and Matrix Transformations...
add this line for text - easy!!! paint.setColor(Color.RED); canvas.drawText("Android", 125, 30, paint);
package amdroid.TransformationalActivity.com; //my silly spelling. still works!
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
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.view.View;
public class TransformationalActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Panel(this));
}
}
class Panel extends View {
private Bitmap mBitmap;
public Panel(Context context) {
super(context);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lotus);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
Paint paint = new Paint();
canvas.drawLine(33, 0, 33, 100, paint);
paint.setColor(Color.RED); paint.setStrokeWidth(10); canvas.drawLine(56, 0, 56, 100, paint);
paint.setColor(Color.GREEN); paint.setStrokeWidth(25);
for (int y = 30, alpha = 255; alpha > 2; alpha >>= 1, y += 10) {
paint.setAlpha(alpha);
canvas.drawLine(0, y, 100, y, paint);
}
//add this for text - EASY!! no need to import a font for this
paint.setColor(Color.RED); canvas.drawText("Hello Androids everywhere", 125, 30, paint);
canvas.drawBitmap(mBitmap, 100, 100, null);
}
}/////////////////
//////////////////////////////
Second example - draw image where user touches screen:
package amdroid.TransformationalActivity.com;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class TransformationalActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Panel(this));
}
}
//////////
class Panel extends SurfaceView implements SurfaceHolder.Callback {
private Bitmap mBitmap;
private ViewThread mThread;
private int mX;
private int mY;
public Panel(Context context) {
super(context);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lotus);
getHolder().addCallback(this);
mThread = new ViewThread(this);
}
public void doDraw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
canvas.drawBitmap(mBitmap, mX, mY, null);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (!mThread.isAlive()) {
mThread = new ViewThread(this);
mThread.setRunning(true);
mThread.start();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mThread.isAlive()) {
mThread.setRunning(false);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mX = (int) event.getX() - mBitmap.getWidth() / 2;
mY = (int) event.getY() - mBitmap.getHeight() / 2;
return super.onTouchEvent(event);
}
}
/////////
class ViewThread extends Thread {
private Panel mPanel;
private SurfaceHolder mHolder;
private boolean mRun = false;
public ViewThread(Panel panel) {
mPanel = panel;
mHolder = mPanel.getHolder();
}
public void setRunning(boolean run) {
mRun = run;
}
@Override
public void run() {
Canvas canvas = null;
while (mRun) {
canvas = mHolder.lockCanvas();
if (canvas != null) {
mPanel.doDraw(canvas);
mHolder.unlockCanvasAndPost(canvas);
}
}
}
}
//////////
I am trying to have a user tap the screen and an image be display there, so i copied this code. When I run it, the program crashes. The error message says: The application Touch (process org.example.touch) has stopped unexpectedly. Please try again. I tried again and it still does not work. Please help!!! Thanks!!
ReplyDeleteHi. A very useful code. I need to draw images and let the previous one to be visible not to disappear. Could you guide me how to do this.
ReplyDelete