-1

我知道这个问题已经被问过其他时间了,但我无法解决我的问题!

我创建自己的视图(RadarView),扩展视图。但是id没有更新!我尝试使用“invalidate()”和“postInvalidate()”,但它们不起作用!

这是雷达视图的代码:

`公共类雷达视图扩展视图{

    private HashMap<Integer, RadarData> allNodes;
    private int myId;
    private double distance=0.0; //in degree
    private double meeterDistance =0.0; // in meeters
    private Paint paint,node,nodeText;

    public RadarView(Context context, HashMap<Integer,RadarData> allNodes, int myId){
        super(context);
        this.allNodes = allNodes;
        this.myId=myId;
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);           
        paint.setColor(Color.GREEN);

        node = new Paint();
        node.setStyle(Paint.Style.FILL);
        node.setAntiAlias(true);            
        node.setColor(Color.GREEN);

        nodeText = new Paint();
        nodeText.setStyle(Paint.Style.STROKE);
        nodeText.setAntiAlias(true);            
        nodeText.setColor(Color.BLACK);
    }

    @Override protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);       

        //here we draw the radar
        canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, canvas.getWidth()/8, paint);
        canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, canvas.getWidth()/4, paint);
        canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, canvas.getWidth()/2.7f, paint);
        canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, canvas.getWidth()/2, paint);
        canvas.drawLine(canvas.getWidth()/2, 0, canvas.getWidth()/2, canvas.getHeight(), paint);
        canvas.drawLine(0, canvas.getHeight()/2, canvas.getWidth(), canvas.getHeight()/2, paint);
        canvas.drawLine(canvas.getWidth() - canvas.getWidth()/4, canvas.getHeight()/10, canvas.getWidth() - canvas.getWidth()/4 +canvas.getWidth()/8, canvas.getHeight()/10, paint);

        //then we add nodes in the view, if at least one exist
        if(allNodes!=null){
            RadarData me = allNodes.get(myId);
            RadarData moreDistant = me;
            Set<Integer> set = allNodes.keySet();
            set.remove(myId);
            for (Integer key : set){
                RadarData element = allNodes.get(key);
                //calculate if it's the more distant, in that case variable "moreDistant" is updated
                if(distance<calculateDistance(element,me)){
                    distance=calculateDistance(element,me);
                    moreDistant=element;
                }
            }
            for (Integer key : set){
                drawNode(canvas,allNodes.get(key), me);
            }
            canvas.drawText("Unit: "+approximateDistance()/4+"degree", canvas.getWidth() - canvas.getWidth()/4, canvas.getHeight()/11,  paint);
        }
}

    private double approximateDistance(){
        int aux=(int) (distance*100);
        return aux/100;
    }
    private void drawNode(Canvas canvas,RadarData element, RadarData me) {
        int radius=canvas.getWidth()/2;
        double x=element.getLongitude()-me.getLongitude();
        double y=element.getLatitude() - me.getLatitude();
        int xInTheView=(int)(radius + x*radius/distance);
        int yInTheView=(int)(canvas.getHeight()/2 -y*radius/distance);

        canvas.drawCircle(xInTheView, yInTheView, radius/10, node);
        canvas.drawText(""+element.getId(), xInTheView, yInTheView, nodeText);
    }

    private double calculateDistance(RadarData element, RadarData me) {
        return Math.hypot(element.getLatitude()-me.getLatitude(), 
                element.getLongitude()-me.getLongitude());
    } 

} `

在主 Activity 中,当 gps 位置发生变化时我调用它:

private void updateRadarView(Location location) {
    if(location!=null){
        RadarData newState= new RadarData(myId, location.getLatitude(), location.getLongitude(), RadarData.RUNNING);
        allNodes = connectionHandler.request(newState);
        radarView = new RadarView(this,allNodes,myId);
        setContentView(radarView);
    }

}

如您所见,我重新创建了radarView,然后使用setContentView 对其进行了更新,但我认为这不是一个好的解决方案。任何想法?

4

1 回答 1

0

当您需要设置radarView的数据时,为什么不创建setNodes(HashMap<Integer,RadarData> allNodes)方法RadarView并每次调用它:我的意思是radarView.setNodes(allNodes)而不是每次都重新创建它?

于 2012-09-09T13:43:44.737 回答