Canvas and drawing image in layout - android

I have a code where I draw image:
class Panel extends View {
public Panel(Context context) {
super(context);
}
#Override
public void onDraw(Canvas canvas) {
Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.calvin_logo_small);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(_scratch, x-point, y-point, null);
}
}
How can I draw this image in my activity, but I don't want to change may layout. I have layout: setContentView(R.layout.main); This is possible to draw in this lauout with canvas? I have this activity and this layout have a lot of components. I only want image in place where I click with canvas. This is idea. I start application where start activity with my setContentView(R.layout.main);. After that I click on the screen and canvas draw picture in place where I clicked. This is possible to do?

Your code should work. Just override the onTouchEvent() for the view, store the co-ordinates of the touch event in field variables and invalidate() the view so that onDraw() would be called. Use the co-ordinates in onDraw() to render the image as you've already done.
To improve performance, you can cache the bitmap if it's not going to change.

Related

How to draw a circle in front of a textview with canvas in android

I made a code where a circle moves around, but whenever it moves in front of a textview, the textview gets in front of him and I want him to be in front of the textview.
I tried drawing the circle after making the textview but it doesn't fix it.
Example:
public MainView(Context context) {
super(context);
}
public void onDraw(Canvas canvas){
((ViewGroup) text.getParent()).removeView(text);//the text was already added to the activity
Paint paint=new Paint();
paint.setColor(Color.WHITE);
canvas.drawCircle(0.0,0.0,500.0, paint);
main.addContentView(text, parameters);//adding a textview named text in the activity
invalidate();
}
Don't create Views inside onDraw, it's called so many times and you will have a lot of TextViews but you only need one, normally created on activity onCreate.
The TextView seems in front due the children is clipped when drawing, try setting clipChildren to false in the ViewGroup container

OnDraw method hides previous drawing

I am kinda new to android and I am creating a small app where I can draw rectangle and circle on custom view.
What I am trying to do is when Circle button is pressed I want to draw circle and when Rectangle button is pressed I want to draw a rectangle.
But the problem, I am having is when I press circle button it draws the circle correctly, but when I click on the rectangle button and tries to draw it, it hides the previous drawn circle and draws a rectangle. If again I select the circle, and draw it, it will hide the rectangle and draws a circle.
MainActivity.java: when button is pressed, buttonPressed method is called:
private DrawingView drawView;
public void buttonPressed(View v)
{
String shape = v.getTag().toString();
if(shape.equals("circle"))
{
Log.e("button pressed", "circle");
drawView.setValue("circle");
}
else if(shape.equals("rect"))
{
Log.e("button pressed", "rect");
drawView.setValue("rect");
}
}
and in my DrawingView class's onDraw method I am doing:
#Override
protected void onDraw(Canvas canvas)
{
if(testValue.equals("rect"))
{
// draw rectangle
}
else if(testValue.equals("circle"))
{
// draw circle
}
}
// setter method to set value
public void setValue(String val)
{
testValue = val;
}
Any suggestions on how to handle this situation?
Android Views will automatically clear their canvas before onDraw() is called, so you'll have to do one of two things:
Keep a list of items to draw, add to the list when a button is pressed, and iterate over it in onDraw(). This is simple to implement, but might slow down the draw process if your list gets long. This happens on the UI thread, so be careful.
Make your own Canvas from a private Bitmap matching the size of your View, and draw on it when a button is pressed. In onDraw(), use drawBitmap() to copy your buffer onto the View.

how to place an image over another image in android by onclick?

i have an imageview in my xml file and i want to put another image over it where user clicks.
i have used a canvas to place an image where user clicks but i am stuck when it comes to putting the canvas over my imageview.
Or is there any way around when we want to put another image over an imageview while detecting user clicks?
Thanks
Since you're already overriding a View method, I would say have that draw the image rather than an ImageView. Your onDraw() method would essentially be:
#Override
public void onDraw(Canvas canvas) {
canvas.drawBitmap(bottomBitmap, xCorner, yCorner, paintObject);
canvas.drawBitmap(topImage, fingerXCorner, fingerYCorner, paintObject);
}
EDIT:
Alternatively, if you want to keep the ImageView because of the convenient scaletypes, then you can override a FrameLayout. Place the ImageView inside of it, then override the draw() method to draw a bitmap.
#Override
public void draw(Canvas canvas) {
super.draw(canvas); // draws everything in the FrameLayout
canvas.drawBitmap(topImage, fingerXCorner, fingerYCorner, paintObject);
}
Have you tried using an ImageButton?
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="onButtonClick"
android:src="#drawable/yourImageFile" />
Then define your onButtonClick method in your Activity,
public void onButtonClick(View v) {
/* Do something */
}

ImageView and draw image in precision coordinates

I have a problem with drawing image. How I can draw image in precision coorinates. I want to draw image in place where I touch the screen. How I can do this? I implement onTouch method where i get coordinates, but I don;t know how I can draw image in this place.
Do like below to draw the image in touch co-ordinate
class Panel extends View {
public Panel(Context context) {
super(context);
}
#Override
public void onDraw(Canvas canvas) {
Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.calvin_logo_small);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(_scratch, x-point, y-point, null);
}
}

Android drawing button to canvas with custom view?

How can I draw a button on top of the canvas in a custom view? (Preferably on the mid-right side) Is there something I have to call before doing the button.draw(canvas)?
public class MyClass extends View {
public Simulation(Context context) {
super(context);
pauseButton.setText("TestButton");
pauseButton.setClickable(true);
pauseButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.i(TAG, "Button Pressed!");
}
});
public onDraw(Canvas canvas) {
super.onDraw(canvas);
pauseButton.draw(canvas);
}
}
Thanks for your time
You cannot insert a button into canvas. Canvas is an interface for bitmap or a bitmap buffer for a view. You can only draw other bitmap or pixels in it, not insert an object or a widget.
There are some solutions:
as Nikolay suggested, use a FrameLayout and create two layers (views), first your custom view and the second LinerView or RelativeView, which will come on top, where you can have buttons etc
draw an image of a buttun on Canvas then use onTouchEvent in your custom view and test for the coordinates of the touch, then do something... an example for onTouchEvent here: Make certain area of bitmap transparent on touch
Why do you need to draw the button yourself? Use a FrameLayout and simply have the button overlayed on your custom view.
Try this
public onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
pauseButton.draw(canvas);
canvas.restore();
}

Categories

Resources