i have the following problem.
I am working with a library of maps which paints the icons on the map using drawables and canvas.
Now, i'm trying to modify it in order to the user can click on icons. So i want to attach drawables into different ImageView with a onClickListener.
However, i don't know how i can paint the ImageView using canvas from method onDraw.
I've tried with:
ImageView iv = new ImageView(context);
iv.setDrawableResource(drawable);
iv.draw(c)
But it doesn't appears in screen.
Any idea? Thanks
You might have better luck if you provided more context -- what's "c" represent here? But in any case, you can't just create new imageviews, you need to attach them to your layout, either by inflating them with a parent view argument from XML, or calling your parent layout's addView() programatically.
Generally, you'll rarely call any draw() methods by hand (unless you're implementing a custom view of some sort); you'll inflate your views from XML into your layout, or else instantiate your views, set whatever LayoutParams you need, and add them to a layout. The Android UI libraries handle figuring out when standard views are invalidated and need to be redrawn, for performance reasons and your own sanity.
Related
I was trying to understand what's the meaning of inflating a view and found this topic:
What does it mean to inflate a view from and xml file?
But I was not satisfied by the accepted answer, as it mention that inflating a view causes its rendering, while I think it is only responsible of the conversion of the XML layouts into the corresponding code by building the View objects, and I'm wondering if it's the case or not, and if not, what can initiate the drawing of the view on screen?
Can anyone clarify my questioning by providing trusted or official documentation?
Every subclass of View overrides the onDraw() method from the View class. This is where the View actually specifies how it will be drawn on the screen. All drawing takes place on the View's Canvas, and the dimensions of the Canvas are specified by the width and height attributes you set in your layout file for that View.
For instance, here's the onDraw() method for TextView:
https://android.googlesource.com/platform/frameworks/base/+/jb-mr0-release/core/java/android/widget/TextView.java#4738
I'm trying to programatically position a TextView above a custom View in a Relative Layout that has also been placed programatically (lets call this CustomView).
I've scoured these forums for similar problems, and I seem to have done all the steps, including setting ids, making sure there are no conflicting rules, etc. But yet it doesn't seem to work.
I am wondering if this has to do with the fact that my CustomView class overrides the onDraw() method (it needs to eventually be animated). In fact, testing the CustomView with it's own set of LayoutParams shows that it does not obey the rules laid out in it - in fact the way that I place this CustomView is by calling canvas.drawBitmap() with the relevant positions in the onDraw() method of CustomView.
Any idea how I can get around this problem? Is it possible to somehow place the the TextView above the CustomView?
I am new to android and trying to make something like the fingerlock security system of the android. The problem is that I have found a lot of reference on drawing onto a plain background (that sets different content view and allows drawing onto that), but not regarding drawing onto the same activity layout.
To draw manually on a View, you need to override its onDraw method. For this you need a custom View (often extending from an existing View type).
If you are using an xml to set the Activity's layout in setContentView() then you need to add the above custom view to this xml. Then, you can draw onto the same activity layout.
setContentView is noting but the activity view. once the view is set in main activity (max in oncreate method) it can update from multiple classes according to your logics.. i think before trying this you have to understand the view and its hierarchic properties properly
I have a layout which contains a ScrollView which contains various components including an ImageView. I've found that I can add a TouchListener to the ImageView and the x,y values I get for the event are offset from the corner of the ImageView (including padding). Thus I can easily handle touch events without worrying about the state of the ScrollView.
The problem is that I don't see a way to add a view on top of the ImageView so that I can make a visual change to the ImageView when it's touched in certain locations.
Specifically, there are various boxes in the image which I'd like to highlight when they're tapped. I can add a view to the activity, but I'd have to take the state of the ScrollView into account in order to display things in the right spot.
What's the best way to approach this? Can I derive a new version of ImageView, overload the draw method, and use that somehow? (not sure how I'd specify it in the xml, I'd probably need to set it in the onCreate of the activity)
You should be able to override the onDraw method of the ImageView as you suggested and draw stuff on top of the ImageView. The onDraw method gives you the Canvas that you could draw on. In the XML, you could just use the name of the new class you created instead of "ImageView" (unless you want to add parameters in addition to the ones offered by ImageView, this should be pretty straight forward).
<your.new.awesome.class.with.the.full.namespace
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/awesome_image_view"
/>
The problem is that I don't see a way to add a view on top of the ImageView so that I can make a visual change to the ImageView when it's touched in certain locations.
It seems that you are more comfortable with having a separate view that you could work on. You could do that by encapsulating the ImageView with a FrameLayout and then adding another view of any kind as a sibling of that ImageView. This view would be drawn on top of the ImageView if it was mentioned in the FrameLayout after the ImageView. This solution may not be the most efficient one, but it might get you started. Overriding the onDraw method of the ImageView would definitely be the best option.
you can make a relativelayout or something with a background as your desired image instead of having just an imageview. then you can have child views within that relativelayout.
I am creating a android app using LunarLander as a example.
Now I need to create a few buttons which are drawn over the view.
I do not want them as a seperate layout above or below the view but in the custom view.
Is this possible or am I going to have to programmatically show the button images then detect the touch. The buttons I create using new never show on the app. I assume this is because I have overwritten the onDraw and the buttons are never drawn even though I call
super.onDraw(canvas);
I think you could use FrameLayout to show two layers - first would be your surface from lunar, and second is the layout with buttons etc. You could define everything in layout.xml file. Probably that is enough.
Regards!
If your view extends Linear/Relative/TableLayout, you can use view.bringChildToFront(child).
Use AbsoluteLayout & then in your ui thread set button.bringTioFront() & you are done!
watch this post Problem when using more elements in the ListView