Android:更新Gauge Chart onLocationChanged

时间:2017-02-21 07:17:46

标签: android

问题:创建一个可以显示设备位置精度的仪表,但这里的挑战是它像速度计一样运行,然后更改onLocationChanged

使用的工具:

  1. Numetriclabz readymade Gauge Chart
  2. Android Studio
  3. 算法

    获得角度:    在这里,我只设置300米作为最低精度

     float getDistance = location.getAccuracy() / 300;
     int mAngle = Math.round(180 - (getDistance * 180));
    

    代码

    package com.numetriclabz.gaugemap;
    
    import android.content.Context;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import com.numetriclabz.numandroidcharts.ChartData;
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity implements LocationListener{
    
        private static TextView accuracyTxt;
        private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters
        private static final long MINIMUM_TIME_BETWEEN_UPDATES = 0; // in Milliseconds // 5 Seconds
        private LocationManager locationManager;
        private static Context context;
        private static GChart gauge;
        private static LinearLayout gChart;
        private static List values = new ArrayList<>();
        private static Location location = null;
        private static Handler handler = new Handler();
        private static int timerCount = 2;
        private static  float getDistance;
        private static int mAngle;
        private static ArrayList<GChart> graphView = new ArrayList<>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            accuracyTxt = (TextView)findViewById(R.id.accuracyTxt);
            context = this;
            gauge = new GChart(context,null);
            gChart = (LinearLayout)findViewById(R.id.gChart);
            values.add(new ChartData(60f));
            values.add(new ChartData(60f));
            values.add(new ChartData(60f));
            gauge.setData(values);
            locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    MINIMUM_TIME_BETWEEN_UPDATES,
                    MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                    this
            );
        }
    
        @Override
        public void onLocationChanged(Location location) {
            if(location != null && timerCount == 2){
                this.location = location;
                runnable.run();
            }
        }
    
        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {
    
        }
    
        @Override
        public void onProviderEnabled(String s) {
    
        }
    
        @Override
        public void onProviderDisabled(String s) {
    
        }
    
        public static Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try{
                    timerCount--;
                    handler.postDelayed(this,1000);
    
                    if(timerCount == -1){
                        getDistance = location.getAccuracy() / 300;
                        mAngle = Math.round(180 - (getDistance * 180));
    
                        if(graphView.isEmpty()){
                            gauge.setAngle(mAngle);
                            gauge.invalidate();
                            gauge.refreshDrawableState();
                            gChart.addView(gauge);
                            graphView.add(gauge);
                            gChart.invalidate();
                        }else{
                            gauge.setAngle(mAngle);
                            gauge.invalidate();
                        }
    
                        accuracyTxt.setText(String.valueOf(location.getAccuracy()));
                        timerCount = 2;
                        handler.removeCallbacks(runnable);
                    }
    
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
    
    }
    

    问题:代码正在运行,但经常更新视图是指针不会勾选的问题如果我更新GaugeChart值它不会更新我的仪表图表中的角度我的问题。

    使用过的想法我曾经使用gChart.removeAllViews()方法更新了我的图表,但它不是用户要求,因为它应该像速度计一样操作,更新值,只是重新创建视图。

    Here is a link to my android project file

    希望你最重要的考虑。欢呼声。

0 个答案:

没有答案