Demonstrates the handling of touch screen events

Put your problem here if it does not fit any of the other categories.

Demonstrates the handling of touch screen events

Postby CHAKYANAS » Thu Mar 20, 2008 7:40 pm

package com.e2esp.android.drawing;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

//Need the following import to get access to the app resources, since this
//class is in a sub-package.


/**
* Demonstrates the handling of touch screen events to implement a simple
* painting app.
*/
public class TouchPaint extends Activity {

@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(new MyView(this));


}

public class MyView extends View {
Bitmap mBitmap;
Canvas mCanvas;
private final Rect mRect = new Rect();
private final Paint mPaint;
private boolean mCurDown;
private int mCurX;
private int mCurY;
private float mCurPressure;
private float mCurSize;
private int mCurWidth;

boolean isActionMove = false;
private int[] xCoord;
private int[] yCoord;
private int count = 0;


public MyView(Context c) {
super(c);
mPaint = new Paint();
mPaint.setAntiAlias(true);
//mPaint.setARGB(0, 0, 0, 0);
mPaint.setColor(Color.BLACK);
xCoord = new int[100];
yCoord = new int[100];
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
int curW = mBitmap != null ? mBitmap.width() : 0;
int curH = mBitmap != null ? mBitmap.height() : 0;
if (curW >= w && curH >= h) {
return;
}

if (curW < w) curW = w;
if (curH < h) curH = h;

Bitmap newBitmap = Bitmap.createBitmap(curW, curH, false);
Canvas newCanvas = new Canvas();
newCanvas.setDevice(newBitmap);
if (mBitmap != null) {
newCanvas.drawBitmap(mBitmap, 0, 0, null);
}
mBitmap = newBitmap;
mCanvas = newCanvas;
mCanvas.drawColor(Color.WHITE);
}

@Override
protected void onDraw(Canvas canvas) {
if (mBitmap != null) {
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}


@Override
public boolean onMotionEvent(MotionEvent event) {
int action = event.getAction();



int X = (int)event.getX();
int Y = (int)event.getY();

mCurPressure = event.getPressure();
mCurSize = event.getSize();

mCurWidth = (int)(mCurSize*(getWidth()/3));

switch (action ) {

case MotionEvent.ACTION_DOWN:
mCurX = (int)event.getX();
mCurY = (int)event.getY();
break;

case MotionEvent.ACTION_MOVE:

// saving the coordinates
xCoord[count] = X;
yCoord[count] = Y;
count++;

mCanvas.drawLine(mCurX, mCurY, X, Y, mPaint);

// Paint paint = new Paint();
// int pressureLevel = (int)(mCurPressure*255);
// paint.setARGB(pressureLevel, 255, 255, 255);
// paint.setColor(Color.WHITE);
// mCanvas.drawLine(mCurX, mCurY, X, Y, paint);

//isActionMove = true;
break;

case MotionEvent.ACTION_UP:
// if (isActionMove)

// rub the previous lines
/* for (int i=0; i<count; i++) {
Paint paint = new Paint();
paint.setColor(Color.WHITE);
mCanvas.drawLine(0, 0, xCoord[i], yCoord[i], paint);
}
*/
// Paint clearPaint = new Paint();
// clearPaint.setXfermode(new PorterDuff(PorterDuff.Mode.CLEAR));
// mCanvas.drawPaint(clearPaint);
// mCanvas.drawRect(0, 0, xCoord[count] + 10, yCoord[count] + 10, clearPaint);

mCanvas.drawLine(mCurX, mCurY, X, Y, mPaint);
break;

}
invalidate();

return true;
}

}
}
User avatar
CHAKYANAS
Developer
Developer
 
Posts: 44
Joined: Mon Mar 03, 2008 5:08 am
Location: INDIA

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 18 guests