Java 如何根据位置输入移动图像?
试图使图像表示人的位置,但不确定如何将变量x和y传递到onDraw 我有它检索我的位置,然后画符号,它以前工作时,我只有数字内的Java 如何根据位置输入移动图像?,java,android,Java,Android,试图使图像表示人的位置,但不确定如何将变量x和y传递到onDraw 我有它检索我的位置,然后画符号,它以前工作时,我只有数字内的 package com.corey.navigationtest; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.gra
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 to draw on
LinearLayout myLayout; //The layout that holds the 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 a surfaceholder to the 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()
);
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS failed, please try 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();
以下是我想要制作图像坐标的变量:
double x;
double y;
x = ((newlong - (-81.366553))/.003803)*width;
y = ((newlat - 41.273816)/.001709)*height;
下面是代码的其余部分
if ((newlat < 41.274871 & newlong > -81.3659) & (oldlong < -81.3659 || oldlat > 41.274871)) {
String phoneNo = "8473027766";
count++;
String sms = String.format(
"You are behind enemy lines \n Longitude: %1$s \n Latitude: %2$s \n Count: %3$s",
location.getLongitude(), location.getLatitude(), count
);
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS failed, please try 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 by the user. GPS turned off",
Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(MainActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_SHORT).show();
}
}
public void onDraw() {
//Starts a thread
new Thread(new Runnable() {
public void run() {
while(true) {
//Loops until surfaceHolder is valid to use
if (surfaceHolder.getSurface().isValid()) {
Log.i("Drawing","Drawing");
//Always lock the canvas if you want to 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 the canvas
canvas.drawBitmap(image2,0,0, null);
canvas.drawBitmap(image1, x, y, null);
//Don't forget to unlock it after you draw in the surfaceview
surfaceHolder.unlockCanvasAndPost(canvas);
//breaks the while and end the thread.
break;
}
}
}
}).start();
}
if((newlat<41.274871&newlong>-81.3659)和(oldlong<81.3659 | | oldlat>41.274871)){
字符串phoneNo=“8473027766”;
计数++;
String sms=String.format(
“您在敌后\n经度:%1$s\n纬度:%2$s\n计数:%3$s”,
location.getLength(),location.getLatitude(),count
);
试一试{
SmsManager SmsManager=smsmsmanager.getDefault();
sendTextMessage(电话号码,空,短信,空,空);
Toast.makeText(getApplicationContext(),“SMS已发送!”,
吐司。长度(短)。show();
}捕获(例外e){
Toast.makeText(getApplicationContext(),
“短信失败,请稍后再试!”,
吐司。长度(短)。show();
e、 printStackTrace();
}
}
onDraw();
oldlat=newlat;
oldlong=newlong;
}
状态已更改的公共void(字符串s、整数i、束b){
Toast.makeText(MainActivity.this,“提供者状态已更改”,
吐司。长度(短)。show();
}
公共无效onProviderDisabled(字符串s){
Toast.makeText(MainActivity.this,
“用户禁用了提供程序。GPS已关闭”,
吐司。长度(短)。show();
}
已提供已启用的公共void(字符串s){
Toast.makeText(MainActivity.this,
“由用户启用提供程序。GPS已打开”,
吐司。长度(短)。show();
}
}
公共资源{
//开始一个线程
新线程(newrunnable()){
公开募捐{
while(true){
//循环,直到surfaceHolder有效使用
if(surfaceHolder.getSurface().isValid()){
日志i(“图纸”、“图纸”);
//如果要在surfaceview中绘制,请始终锁定画布
canvas=surfaceHolder.lockCanvas();
位图image2=BitmapFactory.decodeResource(getResources(),R.drawable.vertical);
位图image1=BitmapFactory.decodeResource(getResources(),R.drawable.ic_启动器);
canvas.drawColor(Color.WHITE);//画布的背景色
drawBitmap(image2,0,0,null);
drawBitmap(image1,x,y,null);
//在surfaceview中绘制后,不要忘记将其解锁
surfaceHolder.unlockCanvasAndPost(画布);
//打断线,结束线。
打破
}
}
}
}).start();
}
您应该为每个图像使用一个。这将使一切变得简单,因为你只需调用setX(…)
和setY(…)
,你的应用程序就会自动在正确的位置重新绘制你的图像
它还将大大减少执行
onDraw()
方法所需的内存和时间,因为它当前每次都在创建新位图(而不是销毁旧位图),因此应用程序不会持续太多的点击。将SurfaceView替换为FrameLayout
,并在其中添加两个ImageView
(图1和图2)。所以根本不使用曲面?我怎么做?我对这些不太熟悉。这应该会让你走上正确的方向:但是,你怎么使用框架布局,以便它删除旧的并转到新的位置