如何根据位置输入移动图像?

时间:2013-02-15 16:41:54

标签: 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.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();
}

1 个答案:

答案 0 :(得分:0)

您应该为每张图片使用ImageView。这会让一切变得简单,因为您只需拨打setX(...)setY(...),您的应用就会自动在正确的位置重绘您的图片。

它还可以大大减少onDraw()方法执行所需的内存和时间 - 因为它每次都在创建新的位图(而不是破坏旧的位图),你的应用程序不会持续太多点击次数。