绘制一个指向地球上特定位置的动态箭头

时间:2011-12-09 05:53:28

标签: android android-layout android-view

我已经实施了一个能够获得他人距离和方向的程序。现在我想通过指向另一个人方向的动态箭头(如针)显示此信息,并且当用户移动他的手机时,箭头将改变其方向仍然指向另一个人的方向。 因此,它将被实现为这样:活动将以(0 <= x <= 360)度数(另一个人)的方向传递给另一个活动,其任务是以图形方式显示箭头。 我知道如何知道方位角值,从而计算特定方向值(x)的箭头旋转度。我知道实现的逻辑,但我不了解图形,动画等。

非常感谢任何快速帮助。

1 个答案:

答案 0 :(得分:0)

这是符合要求的代码。我不知道,但可能存在更好的解决方案: -

        package com.visd.giraffe;

        import android.app.Activity;
        import android.graphics.Bitmap;
        import android.graphics.BitmapFactory;
        import android.graphics.Matrix;
        import android.graphics.drawable.BitmapDrawable;
        import android.hardware.Sensor;
        import android.hardware.SensorEvent;
        import android.hardware.SensorEventListener;
        import android.hardware.SensorManager;
        import android.os.Bundle;
        import android.util.Log;
        import android.widget.ImageView;
        import android.widget.TextView;


        public class arrowhandler extends Activity implements SensorEventListener {


        Matrix mtx = new Matrix();
        ImageView img;
        Bitmap bmp ;
        int w,h;
        float d = 0;
        float oldval = 0,currentrot = 0, rotate = 0,senserotat;

        private SensorManager sensorManager = null;

            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                // Get a reference to a SensorManager
                sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
                setContentView(R.layout.mmmain);
                String[] arrs = getIntent().getExtras().getStringArray("SMSR");
                int degr = arrs[0].indexOf("Dgr:");
                String msgg = "Location of " + arrs[0].substring((arrs[0].indexOf("nosss"))+5) + "\n\n" + arrs[0].substring(0, degr);
                Log.d("AOA",arrs[0].substring(degr+4));
                d = (Float.valueOf(arrs[0].substring(degr+4, arrs[0].indexOf("nosss")))).floatValue();
                Log.d("AOA dv", Float.toString(d));
                if (d>0)
                {
                    d = d+180;
                }
                else if (d<0)
                {
                    d = 180 - (Math.abs(d));
                }
                Log.d("AOA dv", "2" + Float.toString(d));
                Log.d("AOA",Float.toString(d));
                TextView t = (TextView) findViewById(R.id.textView1);
                t.setText(msgg);           

                img=(ImageView)findViewById(R.id.imageView1);
                bmp = BitmapFactory.decodeResource(getResources(), R.drawable.bluearrow);
                // Getting width & height of the given image.
                w = bmp.getWidth();
                h = bmp.getHeight();



              }

            // This method will update the UI on new sensor events
            public void onSensorChanged(SensorEvent sensorEvent) {
            synchronized (this) {
            if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            }

            if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {



                senserotat = d-sensorEvent.values[0];
                if (senserotat < 0)
                {
                    senserotat = 360 - (Math.abs(senserotat));
                }
                    rotate = (360-currentrot)+senserotat;
                    if (rotate>360)
                    {
                        rotate = rotate-360;
                    }
                    mtx.postRotate(rotate);
                    // Rotating Bitmap
                    Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
                    BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);

                    img.setImageDrawable(bmd);
                    currentrot = senserotat;
                    //Log.d("TTTT", Float.toString(sensorEvent.values[0]));


            }
            }

            }


            public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub

        }

            @Override
            protected void onResume() {
            super.onResume();
            // Register this class as a listener for the accelerometer sensor
            sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
            // ...and the orientation sensor
            sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
            }

            @Override
            protected void onStop() {
            // Unregister the listener
            sensorManager.unregisterListener(this);
            super.onStop();
            }

        }