I have two Classes (Draw and DrawView) I'm trying to get the buttons
to function. The buttons need to change stroke color, change stroke
width, undo redo, erase and etc... I can see the buttons in emulator
but cannot get them to function.
Someone please help..this is diving me crazy...I'm fairly new to
android app development.
Here is what I have...
package com.example;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class Draw extends Activity implements OnClickListener {
DrawView drawView;
private Paint currentPaint = new Paint();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this, null);
// Set full screen view hiding the status bar
getWindow().setFlags
(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Shows the title
requestWindowFeature(Window.FEATURE_NO_TITLE);
//uses the main.xml layout
setContentView(R.layout.main);
}
@Override
public void onClick(View view) {
//super.onClick(view);
switch (view.getId()) {
case R.id.blackbutton:
showToastMessage("Color Black"); // message when
button is
clicked
currentPaint = new Paint();
currentPaint.setAntiAlias(true);
currentPaint.setColor(Color.RED);
currentPaint.setStyle(Paint.Style.STROKE);
currentPaint.setStrokeJoin(Paint.Join.ROUND);
currentPaint.setStrokeCap(Paint.Cap.ROUND); // round
stroke
ends on start and stop
currentPaint.setStrokeWidth(5);
break;
case R.id.redbutton:
showToastMessage("Color Red");
break;
case R.id.greenbutton:
showToastMessage("Color Green");
break;
case R.id.bluebutton:
showToastMessage("Color Blue");
break;
case R.id.undobutton:
showToastMessage("Undo");
break;
case R.id.erasebutton:
showToastMessage("Eraser");
break;
case R.id.clearbutton:
showToastMessage("Screen Cleared");
setContentView(R.layout.main);
break;
}
}
private void showToastMessage(String msg){
Toast toast = Toast.makeText(this, msg,
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.TOP, 0, 50);
toast.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
\
// DrawView is a view. It listens to mouse click events and draws a
point at the point that it was clicked on.
package com.example;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class DrawView extends View {
private Paint currentPaint = new Paint();
private Path path = new Path();
/**
* Optimizes painting by invalidating the smallest possible
area.
*/
private float lastTouchX;
private float lastTouchY;
private final RectF dirtyRect = new RectF();
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
currentPaint.setAntiAlias(true);
currentPaint.setColor(Color.MAGENTA);
currentPaint.setStyle(Paint.Style.STROKE);
currentPaint.setStrokeJoin(Paint.Join.ROUND);
currentPaint.setStrokeCap(Paint.Cap.ROUND);
currentPaint.setStrokeWidth(5);
setBackgroundColor(Color.WHITE);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, currentPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
setLastTouchX(eventX);
setLastTouchY(eventY);
// There is no end point yet, so don't waste
cycles
invalidating.
return true;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
// Start tracking the dirty region.
resetDirtyRect(eventX, eventY);
// When the hardware tracks events faster than
they are
delivered, the
// event will contain a history of those
skipped points.
int historySize = event.getHistorySize();
for (int i = 0; i < historySize; i++) {
float historicalX = event.getHistoricalX(i);
float historicalY = event.getHistoricalY(i);
expandDirtyRect(historicalX, historicalY);
path.lineTo(historicalX, historicalY);
}
// After replaying history, connect the line
to the touch
point.
path.lineTo(eventX, eventY);
break;
default:
debug("Ignored touch event: " +
event.toString());
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
private void debug(String string) {
// TODO Auto-generated method stub
}
private void expandDirtyRect(float historicalX, float
historicalY)
{
// TODO Auto-generated method stub
}
private void resetDirtyRect(float eventX, float eventY)
{
// TODO Auto-generated method stub
}
public void setLastTouchX(float lastTouchX) {
this.lastTouchX = lastTouchX;
}
public float getLastTouchX() {
return lastTouchX;
}
public void setLastTouchY(float lastTouchY) {
this.lastTouchY = lastTouchY;
}
public float getLastTouchY() {
return lastTouchY;
}
public RectF getDirtyRect() {
return dirtyRect;
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en