Reputation: 11
I have a problem, I don't know how to create buttons on my canvas. This code below I wrote to draw a line on a picture. Then I want to add some buttons on that picture. Hope you guys can help me.
public class DrawShape extends Activity implements View.OnTouchListener {
ImageView imagTest;
Bitmap bitmap;
Canvas canvas;
Paint paint;
Button abc;
float downX, downY, moveX, moveY, upX, upY = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawshape);
imagTest = (ImageView) findViewById(R.id.imgTest);//Add image
Display display = getWindowManager().getDefaultDisplay();
int dw = display.getWidth();
int dh = display.getHeight();
Bitmap loadedBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image1);//load image onto bitmap
Bitmap scaledloadedBitmap = Bitmap.createScaledBitmap(loadedBitmap, dw, dh, false);
Bitmap drawableBitmap = scaledloadedBitmap.copy(Bitmap.Config.ARGB_8888, true);
Matrix matrix = new Matrix();
imagTest.setScaleType(ImageView.ScaleType.MATRIX);
matrix.postRotate(90);
canvas = new Canvas(drawableBitmap);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setShadowLayer(5, 2, 2, Color.BLUE);
paint.setStrokeWidth(10);
imagTest.setImageBitmap(drawableBitmap);
imagTest.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
moveX = event.getX();
moveY = event.getY();
break;
case MotionEvent.ACTION_UP:
upX = event.getX();
upY = event.getY();
canvas.drawLine(downX, downY, upX, upY, paint);//Handle when touch up
imagTest.invalidate();
break;
}
return true;
}
}
Upvotes: 1
Views: 3511
Reputation: 276
You need to create a Custom View Class which extends the View class Button
and override its onDraw()
method, like this:
public class MyButtonClass extends Button {
public MyButtonClass(Context context) {
super(context);
// Here you can set a default parameters for this View, e.g.:
this.setText("My Green Button...");
}
@Override
protected void onDraw(Canvas canvas) {
// Here you need to declare what paint you canvas, e.g.:
canvas.drawColor(Color.GREEN); // Set color
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),
R.drawable.my_button), 0f, 0f, null); // Set drawable
// .......... set others
super.onDraw(canvas);
}
}
In the onCreate()
method, you need to write next:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout groupview =
(LinearLayout) findViewById(R.id.groupview); // Find container for button
MyButtonClass myBtn = new MyButtonClass(this); // Create custom button
groupview.addView(myBtn); // Add your button to layout
}
Upvotes: 0
Reputation: 11
You can not put a button on the canvas. You can put canvas into new layer, then add your buttons into related layer under layer with canvas.
Upvotes: 1