I am making a 2d game with Canvas in Android.
I have read the documentation of Canvas and I find out that we can create a class that extends View class, like this:
Class MyDraw extends View{
}
and use it as setContentView() in another class, like this:
class HomeActivity extends AppCompatActivity{
void onCreate(Bundle savedInstanceState) {
setContentView(new MyDraw());
}
So my question is: What is the difference between upper code and use MyDraw class as an object without setContentView(new MyDraw()), like this:
class HomeActivity extends AppCompatActivity{
void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_home));
MyDraw myDraw = new MyDraw(context);
myDraw.draw()
}
Which one is better performance?
And when we should use which one?
Performance will be the same here, but there is a very important difference.
If you want to show your canvas, then you need to get it into the content view, so either as the top level view or as a child of it. But in your two examples, only the first one would be shown.
Game engine will be better for performance :)
But, If you want to make it by youself consider using at least SurfaceView https://developer.android.com/reference/android/view/SurfaceView because It provides async drawing to Canvas.
Or you may want to draw it using OpenGL via GLSurfaceView, but this is completely different story
Related
I am creating a finger paint touch mechanism, and I am looking for ways to increase and enhance the performance of the drawing (make it fast and responsive).
In android to draw using canvas we basically create a custom view, we extend the (View) class, and finally we override the most important method to draw and that is the (onDraw) method. We could visualize the usual code for drawing on canvas to be like that:
Usual Drawing
public class UsualDraw extends View{
//constructor (usually 3)
//........
//the onDraw function
#override
protected void onDraw(Canvas canvas){
//here we draw what ever we want
}
}
First Notice
I noticed that others use another technique to draw on canvas, and that involves multi-threading, and instead of extending (View) they extend (SurfaceView). Could be like that:
public class AnotherDraw extends SurfaceView implements SurfaceHolder.Callback{
//constructor (usually 3)
//........
//the onDraw function
#override
protected void onDraw(Canvas canvas){
//here we draw what ever we want
}
//in addition to 3 methods
#override
protected void surfaceChanged(......){
//...
}
#override
protected void surfaceCreated(......){
//here background thread starts.
}
#override
protected void surfaceDestroyed(......){
//here back ground thread should stop.
}
}
basically its a drawing on the background thread.
Second Notice
I noticed that while touch and drawing on the screen, there is missing points which means when you draw very fast then the screen won't keep up with your finger speed (so there is missing touch points or maybe ignored touch points).
My question
1) Why some documentation extend View and others extend SurfaceView? What should I use in my case for responsive drawing? Does it make any difference?
2) What should I do to make the screen keep up with very fast drawing, and make sure that every path is drawn?
I am having trouble grasping a certain concept in Android UI design. The book I am referring to first uses the usual technique that Java programmers use to create UIs and that is to to create containers and add UI components to them and nest them as necessary.
Now, the book introduces a new concept where the entire UI was created using an XML file. The code is pasted below:
package com.oreilly.android.intro;
import android.app.Activity;
import android.os.Bundle;
/**
* Android UI demo program
*/
public class AndroidDemo extends Activity {
private LinearLayout root;
#Override public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
root = (LinearLayout) findViewById(R.id.root);
}
}
so basically, I can use any of them ?
Simple answer,yes, you can use either approach. However, there are some limitations to each such as there are layout properties that must be set in xml if you want to use them. I can't think of what any are off-hand but I can look them up.
For the most part, creating the layouts is much simpler to do in xml but you do have the option of setting Views and layouts in Java if you need to such as creating an unknown number of Buttons depending on some user-defined variable.
When you create your UI in xml then you inflate it in your Java code. This is normally done in onCreate() using
setContentView(R.layout.main);
as you see in your example. But it can also be done with an inflater.
The thing to remember here is to inflate your layout, using either method, before trying to initialize any views in the layout or you will get a NPE when trying to call a method on a View defined before inflating the layout it is contained in.
A correct way
**Examples of inflating views/layouts correctly**
Button mBtn;
public class AndroidDemo extends Activity {
private LinearLayout root;
#Override public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
root = (LinearLayout) findViewById(R.id.root);
btn = (Button) findViewById(R.id.buttonId); // Button is initialized after inflating the layout
}
}
Incorrect way
public class AndroidDemo extends Activity {
private LinearLayout root;
#Override public void onCreate(Bundle state) {
super.onCreate(state);
Button mBtn = (Button) findViewById(R.id.buttonId); // Button is initialized before inflating layout which will return null
setContentView(R.layout.main);
root = (LinearLayout) findViewById(R.id.root);
}
}
I added the above example because I have seen a lot of people make that mistake. So don't do it...you've been warned! :)
Not entirely sure what you're asking, but the two are interchangeable.
Most of the time your UI will be done via xml. In some cases though, the ui is heavily dependent of the data, so you may need to dynamically generate it.
It basically comes down to whichever is easiest for you at the time.
Yes.
But is preferable to use xml, it is more powerful, easier and will separate layout from your code.
Take a look at the docs:
http://developer.android.com/guide/topics/ui/declaring-layout.html
I am trying to create a simple game based on the 2D game framework as described in the 2011 Apress book, "Beginning Android Games" by Mario Zechner. Maybe that helps, maybe it doesn't, but either way:
I only have one activity for the whole game. Everything is rendered onto a bitmap, which is then sent to a SurfaceView, and what is sent to the SurfaceView will change based on what screen the game is on.
I want to implement a simple highscore system that lets users enter their name into an EditView on the main menu screen. I've been able to combine the SurfaceView and the EditView with a RelativeLayout, but the problem is getting rid of the EditView once the user moves past the main menu screen.
There is much more to this class, but here's a snipet of what I feel is important:
public abstract class AndroidGame extends Activity implements Game{
public EditText et;
public RelativeLayout rl;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
et = new EditText(this);
renderView = new AndroidFastRenderView(this, frameBuffer);
rl = new RelativeLayout(this);
rl.addView(renderView);
rl.addView(et);
setContentView(rl);
}
}
The above is the only activity for the game. The AndroidFastRenderView class extends SurfaceView. The problem I'm having is that while I need the SurfaceView to persist through the whole game, I need EditText to only persist for a short while, then disappear based on touch events.
I try to call RelativeLayout.removeView(et) from my class MainMenuScreen, which is responsible for constructing the bitmap sent to the SurfaceView as well as listening for touch events. But I get an uncaught exception that says I can't edit views that are defined in one thread from another.
I'm sorry for the novel, but I've been trying to solve this myself for hours with no luck. I feel like this is a very simple thing I'm just not getting. Any help is much appreciated!
Hm... I think only UI Thread can Add and delete views. Try to call the Thread like this:
runOnUiThread(new Runnable(){
public void run() {
/* STUFF To do in Thread */
//Delete the view...
rl.removeView(et);
}});
Hope it helps :)
I'm fairly new to Java & Android, so I have little idea what I am doing.
My test program successfully creates view objects, so now I am trying to organize my code.
I want to make a separate class to manage my GUI, but it always fails.
Basic info about my gui class:
package horc.gui;
...
public class GUI{
private Context context;
From my main activity, I constructed that gui class with the app context.
gui=new GUI(getApplicationContext()); // gui is a var of type GUI, & this sets the context of the class
The problem is when I make/modify a view object that is in the class from outside, it throws an exception.
My main activity...
package horc.test;
...
GUI gui;
LinearLayout test=gui.newLinear("ff", "v"); // <-- this sets fill parent for width/height
// & vertical orientation of the vertical layout.
// Doesnt work for the reason stated above.
// I cannot manage any view objects from a separate class.
gui.lastText.setText("##########"); // <-- a variable in the class to hold the view object I am manipulating
setContentView(t);
...calls this class function:
public TextView newText(String text){
TextView test=new TextView(context);
lastLinear.addView(test);
return test;
}
I tested this similar body within the main activity & it worked fine. It only fails when I do anything from outside that gui class.
Is there a common issue that people run into when managing view objects in separate classes? I have absolutely no idea what I am doing at this point. Coming from C++, java seems like a nutty language. I cannot plan things the way I would in C++.
instead of
gui=new GUI(getApplicationContext());
try
gui=new GUI(MyActivity.this);
Please put your activity name instead of MyActivity
I'm writing an app for android and I have a class setup that extends View. Inside that class I'm overwriting onDraw.
I have another class where I'm doing a lot of the processing. I was wondering if there was a way I can use the onDraw method inside the 2nd class too? The 2nd class isn't extending anything.
You can instantiate the 2nd class from the first, then pass the Canvas you receive as parameter to the method of the 2nd witch need it.
Something like.
public void onDraw(Canvas canvas){
super.onDraw(canvas);
MyDrawer drawer = new MyDrawer(); //The 2nd class
canvas.store(); //Use it to store the actual matrix and any further change you can do in the paint method will not take effect over the original matrix
drawer.drawOn(canvas);
canvas.restore();//Return the original matrix, any new paint will use the original matrix
}