Help on Canvas Problem

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Help on Canvas Problem

Postby thebassman » Thu Jan 01, 2009 12:09 am

Hi Fellows,

I just have this problem why my button is not refreshing the View's canvas:
Somehow the butt1.setOnClickListener is not working.
The program works this way:
I have canvas from a View, draw some lines on it - was ok during the OnCreate where the onDraw of the view
calls the DrawAny function.
Both the Canvas with drawings & the button butt1 are then seen on the screen.
But when I press the butt1 button which calls the Draw2 function, nothing happens..

I think I'm missing something here.
Id appreciate any help on this...


public class MyGuitar extends Activity {
/** Called when the activity is first created. */
Draw2View dmv;
Button butt1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
butt1 = new Button(this);
dmv = new Draw2View(this);

final LinearLayout lin = new LinearLayout(this);
lin.setOrientation(LinearLayout.VERTICAL);

butt1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
lin.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
butt1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
dmv.Draw2();
}
});
lin.addView(butt1);
lin.addView(dmv);
setContentView(lin);


}


private class Draw2View extends View {

public Draw2View(Context context){
super(context);

}
int x = 0;
int y = 0;int y1 = 0;
int GapString=40;
int GapFret = 70;
int xx = 60;
int yy = 50;

final int NOTE_RADIUS = 12;
public Canvas canvas = new Canvas();
public Paint paint = new Paint();

public Runnable mUpdateTimeTask = new Runnable() {
public void run() {
Draw2View.this.y1 += 100;
Draw2View.this.invalidate();
}
};


@Override
public void onDraw(final Canvas canvas1) {
super.onDraw(canvas1);

this.canvas = canvas1;

DrawAny();
}


public void DrawAny(){
this.paint.setStyle(Paint.Style.STROKE);
canvas.save();
this.paint.setColor(Color.GRAY);
canvas.drawPaint(paint);

this.paint.setStyle(Paint.Style.FILL);
this.paint.setColor(Color.argb(120, 200, 140, 100));
canvas.drawRect(xx, yy, xx + GapString * 5, yy + GapFret * 4, paint);

this.paint.setColor(Color.GRAY);
this.paint.setAntiAlias(true);
this.paint.setStyle(Paint.Style.STROKE);
this.paint.setStrokeWidth(5);
this.paint.setColor(Color.BLACK);
Path path = new Path();

path.moveTo(xx, yy);
path.lineTo(xx, yy + GapFret * 4);
canvas.drawPath(path, paint);
this.paint.setStrokeWidth(4);
path.offset(GapString, 0);
canvas.drawPath(path, paint);
this.paint.setStrokeWidth(3);
path.offset(GapString, 0);
canvas.drawPath(path, paint);
this.paint.setStrokeWidth(2);
path.offset(GapString, 0);
canvas.drawPath(path, paint);
this.paint.setStrokeWidth(1);
path.offset(GapString, 0);
canvas.drawPath(path, paint);
this.paint.setStrokeWidth(1);
path.offset(GapString, 0);
canvas.drawPath(path, paint);

canvas.restore();
}

public void Draw2(){
this.paint.setStyle(Paint.Style.STROKE);
canvas.save();
this.paint.setColor(Color.RED);
canvas.drawPaint(paint);

this.paint.setStyle(Paint.Style.FILL);

this.paint.setAntiAlias(true);
this.paint.setStrokeWidth(5);
canvas.drawCircle(50,50,20,paint);

canvas.restore();
}

}
}
thebassman
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Dec 31, 2008 6:19 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests