In my ANDROID application, I am trying to develop an interactive image. The image is made of several interconnected nodes. I would like to tap on two different nodes to get the shortest path between them. When the nodes are tapped, I want to draw an overlay on top of the image which indicates the shortest path between the tapped nodes. In addition to this, I would want to implement the animation of the path from source node to intermediate nodes and finally to the destination node.
I was trying to implement this in Surface View.
Any suggestions on how to get started with this would be really helpful. Any tutorials, sample code which I can use as a guidance would be great.
You can start by creating your own surfaceview class that displays an image.
Make sure you implement SurfaceHolder.Callback in your surfaceview class.
public class MySurfaceView extends SurfaceView implements
SurfaceHolder.Callback {
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mthread.setRunning(false);
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
You can use a thread to call draw methods and render the image. Make sure to use Canvas locks while implementing Threads.
Related
Maybe this is more of an ASP.NET MVC question than an Orchard question but I'm relatively new to both and I don't know the answer in either case.
ASP.NET MVC applications don't technically have a single point of entry, so where am I supposed to put code that I want to always run each and every time someone visits any page, regardless of layer or origin or permissions? Is there a specific Orchard way of doing this?
If it makes a difference, what I'm specifically trying to do at the moment is to restrict the IP range that has access to my website. I want to look at every incoming request and check if the user is either authenticated or has an IP in the allowed range that I configured in my custom settings.
I can think of some quick and dirty ways to achieve this like putting the check in Layout and wrap a condition around all of my zones or implement IThemeSelector to switch to a different Theme but I'd like to do it properly.
All what should you do to achieve this, is implementing new IActionFilter or IAuthorizationFilter like the following:
public class CheckAccessFilter : FilterProvider, IActionFilter, IAuthorizationFilter {
public void OnActionExecuting(ActionExecutingContext filterContext) {
// here you can check the incoming request, and how the system will deal with it,
// before executing the action
}
public void OnActionExecuted(ActionExecutedContext filterContext) {
}
public void OnAuthorization(AuthorizationContext filterContext) {
// here you can authorize any request
}
}
But if you want only to authorize based on content items (like: Widgets, Pages, Projections), you can implement IAuthorizationServiceEventHandler:
public class IPAuthorizationEventHandler : IAuthorizationServiceEventHandler {
public void Checking(CheckAccessContext context) {
}
public void Adjust(CheckAccessContext context) {
}
public void Complete(CheckAccessContext context) {
}
}
The best sample you can follow to implement this approach is SecurableContentItemsAuthorizationEventHandler, you can find it in Orchard.ContentPermissions module.
I'm developing a simple game using eclipse and libgdx.
Currently, I'm using 'Music' instead of 'Sound' for sound effects for my game.
I made a button for muting all of the sound fx but having a problem when it comes to 'sound' instead of music.
Here's my current code:
public static Music jump;
public static void load() {
jump = Gdx.audio.newMusic(Gdx.files.internal("data/jump.wav"));
}
public static void muteFX() {
lesgo.setVolume(0);
public static void normalizeFX() {
jump.setVolume(1f);
//'muteFX' and 'normalizeFX' to be called on different class
I wanted to change it to 'Sound' (the reason is I wanted it to be more responsive on fast clicks,) So it could be like this:
public static Sound jump;
public static void load() {
jump = Gdx.audio.newSound(Gdx.files.internal("data/jump.wav"));
}
/* here comes my problem, didn't know how to set mute and normal
volume for the jump sound. I know there is also a set volume method
to 'Sound' but really confused on the terms (long soundID, float volume)
Can someone make this clear to me on how to implement the long and soundID?
*/
I'm really new to libgdx, as well as in java. I researched many forums regarding this and still can't find a more clearer explanation.
Any help will be greatly appreciated.
Thanks a lot in advance! =)
A quick suggestion would be to use a global variable for sound
public static VOLUME = 1.0f;
The sound api allows you to play a sound at a certain volume, so what you could do is have all the sounds in your game play at that global value when needed.
jump.play(VOLUME);
this way, all your switch will do is change the float value of volume.
public static void muteFX(){
VOLUME = 0.0f;
}
public static void normalizeFX(){
VOLUME = 1.0f;
}
It would be in your best interest to not use the Music class for sound effects due to memory constraints.
I hope this helps
In the documentation it states that the id is returned when play() or loop() methods succeed. This might not be convenient depending what are you trying to achieve. Basicly you can get the id and use it with something similar to this:
long id = sound.play(1.0f); // play new sound and keep handle for further manipulation
sound.stop(id); // stops the sound instance immediately
sound.setPitch(id, 2); // increases the pitch to 2x the original pitch
When I wanted to have a button Volume OFF and Volume ON I had a global variable which I would check before I play the sound somethign similar to this:
In my main Game Class:
public static boolean soundEnabled;
Whenever I wanted to play sound.
if (MyMainClass.soundEnabled)
sound.play();
In case you want any other control of the sound then inevetably you need to get the sound's id.
To communicate between views and objects persistant information, for example the username and choice of font size for display, is it considered good form to put these onto the Application object, or is it more efficient to put them into static singletons?
For example:
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args);
}
public static Username {get;set;}
}
I don't think there's any difference performance-wise between putting static objects into the Application vs singletons vs static classes.
For things like Colors and fonts, I usually prefer to create static classes to hold each type of object, so for example I usually write this:
public static class Colors {
public static Color ToolbarColor = Color.Black;
..
}
This makes it easier to change colors around the entire app without having to be searching around everywhere. I do the same thing for fonts, images, etc.
I have a midlet which has got a static variable. I need to keep the record of all the instances created in this variable. but it does not work like static variable. my code segments look like this. I am running this midlet on sun wireless toolkit 2.5.5. I can create many objects of same midlet from that toolkit but still my counter shows only 1.
public class SMS extends MIDlet implements CommandListener {
private Display display;
private TextField userID, password ;
public static int counter ;
public SMS() {
userID = new TextField("LoginID:", "", 10, TextField.ANY);
password = new TextField("Password:", "", 10, TextField.PASSWORD);
counter++;
}
public void startApp() {
display = Display.getDisplay(this);
loginForm.append(userID);
loginForm.append(password);
loginForm.addCommand(cancel);
loginForm.addCommand(login);
loginForm.setCommandListener(this);
display.setCurrent(loginForm);
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
System.out.println("Total Instances"+counter);
everytime, counter shows only 1 object created.
The only system I've seen that allows static variables to remain between 'invocations' of an application is Android. I've never once seen a J2ME device that maintains static data between invocations of a MIDlet. However, MIDlets within a MIDlet suite can share static data, as described here, while at least one of them is running.
If you want to maintain data between invocations of a MIDlet, you need to use the Record Store APIs in javax.microedition.rms, which provide access to a persistent store.
Your MIDlet is only instantiated once. Kind of.
The MIDP runtime will probably not allow you to launch the same MIDlet twice as long as it is already running.
If you exit the MIDlet, counter goes back to 0 because it is still a in-RAM value and the Java Virtual Machine process is terminated.
On some Nokia series40 phones, the JVM process is never terminated so you could use this to show how many times the MIDlet was created since the last time the phone was switched on.
Static variables are stored in a Class object in the JVM memory. You need to understand class loading (and the usual lack of support for class unloading in J2ME) to figure out what you can store in static variable.
I would suggest moving counter++; to startApp() as it could be called everytime the MIDlet is brought to the foreground.
That would also allow you to store counter in an RMS record for additional accuracy.
I have read in many places that network connection in a j2me app should be done in a separate thread. Is this a necessity or a good to have?
I am asking this because I could not find anywhere written that this must be done in a separate thread. Also, when I wrote a simple app to fetch an image over a network and display it on screen (without using a thread) it did not work. When I changed the same to use a separate thread it worked. I am not sure whether it worked just because I changed it to a separate thread, as I had done many other changes to the code also.
Can someone please confirm?
Edit:
If running in a separate thread is not a necessity, can someone please tell me why the below simple piece of code does not work?
It comes to a stage where the emulator asks "Is it ok to connect to net". Irrespective of whether I press an "yes" or a "no" the screen does not change.
public class Moo extends MIDlet {
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
Display display = Display.getDisplay(this);
MyCanvas myCanvas = new MyCanvas();
display.setCurrent(myCanvas);
myCanvas.repaint();
}
class MyCanvas extends Canvas {
protected void paint(Graphics graphics) {
try {
Image bgImage = Image.createImage(getWidth(), getHeight());
HttpConnection httpConnection = (HttpConnection) Connector
.open("https://stackoverflow.com/content/img/so/logo.png");
Image image = Image.createImage(httpConnection
.openInputStream());
bgImage.getGraphics().drawImage(image, 0, 0, 0);
httpConnection.close();
graphics.drawImage(bgImage, 0, 0, 0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Edit: I got my answer for the code here.
Edit: I spawned off a separate question of this here.
The problem is that you are trying to do work within the thread that is responsible for running the UI. If you do not use a separate thread, then that UI thread is waiting while you do your work and can't process any of your other UI updates! so yes you really should not do any significant work in event handlers since you need to return control quickly there.
I agree with Sean, but it is not required to have your network connection in a separate thread, just best practice. I think that it's probably coincidental that the connection worked properly after moving it to a separate thread. Either way though, if you want to provide any visual feedback to the user while the connection is happening (which you probably do considering the disparity of lag that users can experience on a mobile network), you should have the networked processing in a separate thread.
It is not mandatory that you do network connections in a new thread,however practically you'll find that it almost always a good idea to do so since network activities could block and leave your app in an unresponsive state.
This is an old article but it speaks about some of the issues involved in networking and user experience.