java - How do i make an image move based on location input? -
trying create image represent person's location, not sure how pass variables x , y ondraw...
i have retrieving location drawing symbol , worked when had numbers within the
package com.corey.navigationtest; import android.app.activity; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.color; import android.location.criteria; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.telephony.smsmanager; import android.util.displaymetrics; import android.util.log; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.linearlayout; import android.widget.framelayout.layoutparams; import android.widget.toast; public class mainactivity extends activity { private static final long minimum_distance_change_for_updates = 5; // in meters private static final long minimum_time_between_updates = 1000; // in milliseconds protected locationmanager locationmanager; protected button retrievelocationbutton; protected button buttonsend; canvas canvas; //your canvas draw on linearlayout mylayout; //the layout holds surfaceview surfaceview surface; surfaceholder surfaceholder; public double oldlat = 0; public double oldlong = 0; public double newlat; public double newlong; public int count = 0; displaymetrics metrics; int width; int height; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); retrievelocationbutton = (button) findviewbyid(r.id.retrieve_location_button); locationmanager = (locationmanager) getsystemservice(context.location_service); criteria criteria = new criteria(); criteria.setaccuracy(criteria.accuracy_fine); criteria.setaltituderequired(false); criteria.setbearingrequired(false); criteria.setcostallowed(true); criteria.setpowerrequirement(criteria.power_low); string provider = locationmanager.getbestprovider(criteria, true); locationmanager.requestlocationupdates( provider, minimum_time_between_updates, minimum_distance_change_for_updates, new mylocationlistener() ); buttonsend = (button) findviewbyid(r.id.buttonsend); retrievelocationbutton.setonclicklistener(onclicklistener); buttonsend.setonclicklistener(onclicklistener); mylayout = (linearlayout) findviewbyid(r.id.mylayout); linearlayout.layoutparams params_surfacecanvas = new linearlayout.layoutparams(layoutparams.match_parent, layoutparams.match_parent); surface = new surfaceview(this); surface.setlayoutparams(params_surfacecanvas); //assign surfaceholder surface surfaceholder = surface.getholder(); mylayout.addview(surface); canvas = new canvas(); metrics = this.getresources().getdisplaymetrics(); width = metrics.widthpixels; height = metrics.heightpixels; } private onclicklistener onclicklistener = new onclicklistener () { @override public void onclick(final view v) { switch(v.getid()) { case r.id.buttonsend: location location = locationmanager.getlastknownlocation(locationmanager.passive_provider); string phoneno = "8473027766"; string sms = string.format( "current location \n longitude: %1$s \n latitude: %2$s", location.getlongitude(), location.getlatitude() ); seek { smsmanager smsmanager = smsmanager.getdefault(); smsmanager.sendtextmessage(phoneno, null, sms, null, null); toast.maketext(getapplicationcontext(), "sms sent!", toast.length_short).show(); } grab (exception e) { toast.maketext(getapplicationcontext(), "sms failed, please seek 1 time again later!", toast.length_short).show(); e.printstacktrace(); } break; case r.id.retrieve_location_button: showcurrentlocation(); break; } } }; protected void showcurrentlocation() { location location = locationmanager.getlastknownlocation(locationmanager.passive_provider); if (location != null) { string message = string.format( "current location \n longitude: %1$s \n latitude: %2$s", location.getlongitude(), location.getlatitude() ); toast.maketext(mainactivity.this, message, toast.length_short).show(); } } private class mylocationlistener implements locationlistener { public void onlocationchanged(location location) { newlat = location.getlatitude(); newlong = location.getlatitude(); string message1 = string.format( "new location \n longitude: %1$s \n latitude: %2$s", location.getlongitude(), location.getlatitude() ); toast.maketext(mainactivity.this, message1, toast.length_short).show();
these variables want create coordinates of image:
double x; double y; x = ((newlong - (-81.366553))/.003803)*width; y = ((newlat - 41.273816)/.001709)*height;
and heres rest of code
if ((newlat < 41.274871 & newlong > -81.3659) & (oldlong < -81.3659 || oldlat > 41.274871)) { string phoneno = "8473027766"; count++; string sms = string.format( "you behind enemy lines \n longitude: %1$s \n latitude: %2$s \n count: %3$s", location.getlongitude(), location.getlatitude(), count ); seek { smsmanager smsmanager = smsmanager.getdefault(); smsmanager.sendtextmessage(phoneno, null, sms, null, null); toast.maketext(getapplicationcontext(), "sms sent!", toast.length_short).show(); } grab (exception e) { toast.maketext(getapplicationcontext(), "sms failed, please seek 1 time again later!", toast.length_short).show(); e.printstacktrace(); } } ondraw(); oldlat = newlat; oldlong = newlong; } public void onstatuschanged(string s, int i, bundle b) { toast.maketext(mainactivity.this, "provider status changed", toast.length_short).show(); } public void onproviderdisabled(string s) { toast.maketext(mainactivity.this, "provider disabled user. gps turned off", toast.length_short).show(); } public void onproviderenabled(string s) { toast.maketext(mainactivity.this, "provider enabled user. gps turned on", toast.length_short).show(); } } public void ondraw() { //starts thread new thread(new runnable() { public void run() { while(true) { //loops until surfaceholder valid utilize if (surfaceholder.getsurface().isvalid()) { log.i("drawing","drawing"); //always lock canvas if want draw in surfaceview canvas = surfaceholder.lockcanvas(); bitmap image2 = bitmapfactory.decoderesource(getresources(), r.drawable.vertical); bitmap image1 = bitmapfactory.decoderesource(getresources(), r.drawable.ic_launcher); canvas.drawcolor(color.white); //the background color of canvas canvas.drawbitmap(image2,0,0, null); canvas.drawbitmap(image1, x, y, null); //don't forget unlock after draw in surfaceview surfaceholder.unlockcanvasandpost(canvas); //breaks while , end thread. break; } } } }).start(); }
you should utilize imageview each of images. create easy, since phone call setx(...)
, sety(...)
, , app automatically redraw image in right location.
it cut down memory , time needed ondraw()
method perform - since creating new bitmaps every time (and not destroying old ones), app not lastly many clicks.
java android
No comments:
Post a Comment