3

I'm new to android programming (writing my first app now) and I need some help.

I've set up my Framelayout with a linearlayout inside of it with a button, ImageView, and TextView.

I want to be able to draw a bitmap I have ONTO the ImageView where and when the user touches onto the ImageView. I have my ontouchlistener set up (and i know it's working, i've tested) but I can't figure out how to make the bitmap (a 20px by 20px ball) show up where the user touches it. Is there a way I can do this, or add a canvas on top of the ImageView?

Sorry if my question wasn't very detailed or too noobish but im really having some trouble with this and I need an explanation from someone. Thanks in advance!

Jarnuman
  • 48
  • 1
  • 8

1 Answers1

2

I thought there was an example of this in the android sdk, I'm often forgetful about these things but I definitely found exactly what you are looking for on the web. But my search-fu is all out of funk today.

Here's something to get you started, you might need to modify so that you do the draw on touch.

http://android-er.blogspot.com/2010/05/draw-bitmap-on-view.html

Also use the search on this site ... just type [android] +draw +bitmap +touch into the search box to specify that you want to filter on the android tag and force include the search terms.

Also take a look at android drawing on touch event

Community
  • 1
  • 1
Moog
  • 10,193
  • 2
  • 40
  • 66
  • Thanks merlin! There is a little bit of a problem that I don't understand. I already have an xml file with my layouts all created that I want to use (and i don't really know how to use custom views :/ ). It says "setContentView(new myView(this)) and I don't know how to implement my already made xml file into this. It would be a huge pain if I had to redo my entire project (which im starting to get a feeling that i have to do). Thanks :) – Jarnuman Oct 16 '11 at 01:36
  • that's easy ... just create your extended view, once you build the project you should be able to find it in your layout designer in eclipse. Just drag and drop ;) – Moog Oct 16 '11 at 01:39
  • Sorry, but im really new and i'm not quite sure i got what you just said. What do you mean when you say "find it in my layout designer in eclipse and drag and drop?". Do i setContentView(R.layout.main) in my extended view? – Jarnuman Oct 16 '11 at 01:42
  • No not in the view, you should already have this in your activity. If you want to include your custom view in your layout, then create your customview as a class in it's own file. e.g myCustomView.java When you compile it, your new view should be selectable in the layout designer (in the toolbox of controls). You should be able to drag your customview into your layout. You should not have to change your activity at all. – Moog Oct 16 '11 at 01:53
  • Wow, im having a really hard time understanding this.....so if I create my custom view in a seperate java class and move it to the main activity, how do I set my original layout to be showing? And likewise how do I set my extended view to be working? – Jarnuman Oct 16 '11 at 02:00
  • you don't move it into the activity you include it in your layout xml file. – Moog Oct 16 '11 at 02:04
  • You can then use findViewById in your activity to setOnClickListener() or whatever you want to do with it – Moog Oct 16 '11 at 02:05
  • Ok that makes a little bit more sense...except i don't know how to include my custom view into the xml layout. Im really knew! Care to explain? I'm sure i can find it online, if you're busy. (I really appreciate this! Sorry, i haven't really expressed that enough) – Jarnuman Oct 16 '11 at 02:11
  • 1
    When you open up your xml file, you can open it by right-clicking and selecting **Open With -> Other -> Android Layout Editor** this will allow you to drag and drop your layout in a visual designer. In the toolbox of controls you will find the **Custom and Library Views** folder which should have your custom view in it. – Moog Oct 16 '11 at 02:16
  • alright i'll try all that out. If I have any questions, i'll comment on this thread so look out :) I really appreciate this man. You're a life saver. You saved me hours of searching the web. Thanks! – Jarnuman Oct 16 '11 at 02:37
  • Merlin, I've gone through a huge process of creating a custom Surface View and my onTouchEvent works perfectly fine. I am, however, having trouble integrating my custom view into my XML. Is there any way you could possibly help me with this problem? I've spend about a week on this and I've really hit a roadblock on this one. Thanks! – Jarnuman Nov 06 '11 at 23:27