How does the onDraw(Canvas) function of the View class work? - android

I need to implement an interactive graph in Android
For a start I am trying to see how this code works
There, in a class called LineChartView Which extends the user generated class ChartView which extends the View class, there is an #overrideed function called onDraw(Canvas canvas). How and when does this function get called? the output of that code is a bunch of graphs on full screen, but my interactive graph should only take up a part of the screen. Does the onDraw() function get called automatically? If so, when? And what is the size of the canvas? Is it always the full screen occupied by the current activity's window?

in this link View Android Developer there is a section on implementing a custom view
onDraw(Canvas canvas) is called whenever a view should render its content
if you define to use this view in your layout xml or even in code you can specify the attributes
android:layout_width=
android:layout_height=
and those attributes will be applied to the size that the view will use
in your layout.xml
<your.package.name.extendedviewclass
android:layout_width="100dp"
android:layout_height="100dp"/>

Related

How to draw animation over a webview in android

I want to draw animations over a webview. My activity's xml structure looks like the structure below:
<FrameLayout>
<LinearLayout>
</LinearLayout>
<WebView/>
<LinearLayout>
</LinearLayout>
</FrameLayout>
Is it possible somehow to create a transparent canvas in order to draw my animation over the webview?
Thanks in advance!
If you use a RelativeLayout as a parent of the WebView you would be able to add some other view above it, order of declaration in the XML file determines the way it is drawn, so if the WebView is drawn first then the next view will be on top of it, make the background of this view transparent and you should be good to go, you may have some problems with interacting with the WebView however, I would make the visibility of this view View.GONE while it is not needed.
Ok... Actually done it by creating a CustomWebView object that extends WebView and add it to the layout programmaticaly. The CustomWebView objects overrides onDraw(Canvas canvas) method, and there I do my animation...

How to show canvas in my layout?

Do you know, how to show canvas here:
What should I put in here?
If you want to draw directly on the canvas using the onDraw method, just create a new class extending View and do your drawing there.
On the xml you put your class as a item on the layout, using the full name of the class including the package.
Example:
<com.example.MyCanvas
android:layoutWidth="match_parent"
android:layoutHeight="match_parent"
/>
Then on your project you have the class MyCanvas on the package com.example that extends View and override the onDraw() where you have a canvas and can draw anything you want on it.

Add draggable shape on a picture in Android

Can anyone explain how can I add shape (for example a rectangle or an arrow) on to a image (in ImageView)? Once added, the shape will need to be draggable to anywhere in the image. And finally need to save the image edited (with the shape on top of that).
What informations do you really need?
This can be some steps to do:
Maybe create a new object to handle this:
Create a new class and extend from ImageView or use LayerDrawable for that.
Write setters/getters for the main-background-image/bitmap.
Add your own Vector shape (create a new class with definable color,thickness... or hard-code it)
Do all of your drawings in onDraw-Methods
Implement onTouch interfaces to handle your selection and dragging.
Create new method (render to jpeg/png/..) to save your resulting image
Create a Custom view from View that completely overlaps your ImageView. This view will have a transparent background and contain the draggging view. Initially set visibility to invisible.
In Custom view class override the onTouch event where you update the dragging views margins.
On save, get drawing cache of the parent layout(that contains both the ImageView and Custom view) and use as needed.
Sample xml code:
Relative layout parent whose drawing cache has to be grabbed using rel.getDrawingCache()
<RelativeLayout .......>
<ImageView ......./>
<CustomView ......./>
</RelativeLayout>

Adding Buttons to FingerPaint Android API Sample1

Im kind of new to Android.
I am playing around with the Android FingerPaint API Sample. I have figured out how to add buttons and functions to the Menu, but how do I get buttons on the actual screen?
Is it possible to place buttons over the drawing surface? Or would I need a linear (Vertical) layout on the left, and place buttons in there. Either would be fine.
Help? Thanks.
The Android FingerPaint sample code does not use a layout; it instead just has a subclass of View called MyView, and then the Activity sets its content view to be an instance of MyView.
If you want to have more than one View on the screen, you'll need to use some sort of layout. You could use a LinearLayout if you want the MyView for painting to be above, below, or to the side of the buttons; if you want the buttons to be on top of the MyView, take a look at using a FrameLayout or RelativeLayout.
You can either then define the layout in XML or create it manually in code. The former is more flexible and maintainable, but there will be a few hiccups.
First, create a layout XML showing how you want your components to be laid out. For this example, we'll call it finger_paint.xml. Make sure you have a MyView in there somewhere, something like:
<view class="com.example.android.apis.graphics.FingerPaint$MyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
Then, replace the line that looks like
setContentView(new MyView(this));
with
setContentView(R.layout.finger_paint);
Note that because MyView does not (yet) have the proper constructor for being instantiated by the LayoutInflater, this will not work yet, so let's fix that. Add an additional import near the top of the file:
import android.util.AttributeSet;
and then add an AttributeSet parameter to the constructor of MyView:
public MyView(Context c, AttributeSet as) {
super(c, as);
// rest of constructor is same as in the sample
}
You will also have to change MyView to be a static inner class of FingerPaint.
You may find the Building Custom Components document and the NotePad sample useful as you figure this out.
Good luck!
Are you trying to dynamically position your buttons?
If so, you can use setLayoutParams to set the layout properties of the button.

Android: draw on a view inside an inflated xml (another view)

I have a small xml file called 'xmlview.xml' which is something like:
<tablelayout>
<tablerow>
<view id="view1" />
I created a class which extends view, and I inflated that xml file and now i have a view obtained due to inflation.
xmlView = layoutInflater.inflate(R.layout.xmlview, this);
I am wondering is there a way to draw on the view 'view1' inside the view 'xmlview'
I tried something like:
View view1 = (View)xmlview.findById(R.Id.view1);
and then I tried in the overriden onDraw to draw on view1 but nothing worked,
any ideas ??
Seems to me maybe you should try something like this:
<TableLayout>
<TableRow>
<com.yourdomain.yourproject.ViewClassName>
Then you can create a class ViewClassName that extends View and, on that class, override the onDraw method. This is explained in detail here: http://developer.android.com/guide/topics/ui/custom-components.html. You can go directly the topic Modifying an Existing View Type
See, you don't need to inflate the view if the only reason to do this is to try and manipulate the view's drawing. And if it is the only reason, you really shouldn't.
hope I helped
I'm not sure what exactly you mean by "draw on the view", but it sounds like you should be looking into the Canvas. You should not arbitrarily override the onDraw method, this is what causes your widgets to appear on the screen.

Categories

Resources