mAppWidget - 5. 为地图物品添加警示圈

我家淘宝店,主要协助同学做毕业设计:https://shop104550034.taobao.com/?spm=2013.1.1000126.d21.pPCzDZ

首先看效果

实现方法:

  1. 建立一个新类OtherCar,继承自AnimationMapObject。
    (AnimationMapObject 类的代码在http://blog.csdn.net/u012812482/article/details/51337110中可以找到)
  2. 添加一个线程不断刷新圆圈的半径。
  3. 重写draw方法,动态绘制4个圆圈。

2. 实现代码:

OtherCar 类

package com.qinxiaoyu.mAppwidget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

/** * 其它车辆,指除了本车自己以外的车辆 * @author 秦晓宇 * @date 2016年5月5日 上午10:01:19 */
public class OtherCar extends AnimationMapObject{


    /**报警圈数据刷新线程*/
    WarringCircle warringCircle;

    private boolean isShowWarringCircle = false;

    public OtherCar(Context context, int id, Drawable drawable, Point initPoint) {
        super(context, id, drawable, initPoint);
        // TODO Auto-generated constructor stub
        warringCircle = new WarringCircle();
        warringCircle.start();
    }
    /** * 重写了父类的绘画方法,在其上添加了4个警示圈动画 * * @param canvas * * @date 2016年5月5日 上午10:02:24 * * @see com.qinxiaoyu.mAppwidget.AnimationMapObject#draw(android.graphics.Canvas) */
    @Override
    public void draw(Canvas canvas)
    {       
        super.draw(canvas);
        if(this.isShowWarringCircle == true)
        {
            Paint p = new Paint();
            p.setAntiAlias(true);
            p.setColor(Color.RED);
            p.setStyle(Paint.Style.STROKE);
            p.setStrokeWidth(2);
            canvas.drawCircle(move_xstep, move_ystep, warringCircle.getRange4(), p);// 大圆
            canvas.drawCircle(move_xstep, move_ystep, warringCircle.getRange3(), p);// 大圆
            canvas.drawCircle(move_xstep, move_ystep, warringCircle.getRange2(), p);// 大圆
            canvas.drawCircle(move_xstep, move_ystep, warringCircle.getRange1(), p);// 大圆 
        }
    }

    /** * 刷新警示圈线程,目前1个警示圈绘制4个圆圈 * @author 秦晓宇 * @date 2016年5月5日 上午10:03:27 */
    private class WarringCircle extends Thread {
        private boolean runFlag = true;
        private final int duration = 10;
        private int range;
        private int range1;
        private int range2;
        private int range3;
        private int range4;
        public int getRange1() {
            return range1;
        }
        public int getRange2() {
            return range2;
        }
        public int getRange3() {
            return range3;
        }
        public int getRange4() {
            return range4;
        }
        public void run()
        {
            while(runFlag)
            {                                                                   
                try 
                {
                    Thread.sleep(duration);
                } 
                catch (InterruptedException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                range += 1;
                range1 = range;
                range2 = range-35*1;
                range3 = range-35*2;
                range4 = range-35*3;


                if(range1 > 200) 
                {
                    range1 = 0; 
                }   
                if(range2 > 200) 
                {
                    range2 = 0; 
                }   
                if(range3 > 200) 
                {
                    range3 = 0; 
                }   
                if(range4 > 200) 
                {
                    range4 = 0; 
                    range = 0;
                }   
            }
        }

    }

    public boolean isWarringCircleVisable() {
        return isShowWarringCircle;
    }
    public void setWarringCircleVisable(boolean isShowWarringCircle) {
        this.isShowWarringCircle = isShowWarringCircle;
    }

}

MainActivity

public class MainActivity extends Activity {

    private void debug(String str){
        Log.d("MainActivity",str);
    }

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        requestWindowFeature(Window.FEATURE_NO_TITLE); //设置无标题
        setContentView(R.layout.activity_main); 

        final RoadWayMapWidget map = new RoadWayMapWidget(this, "map",15);
        LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);     
        map.getConfig().setZoomBtnsVisible(false);
        layout.addView(map);

        final OtherCar myCar = new OtherCar(getApplicationContext(),1,getResources().getDrawable(R.drawable.car_arror),new Point(100,100));
        myCar.setWarringCircleVisable(true);
        map.addCar(myCar);
        myCar.setMove(new Point(1500, 500), 10000);
        myCar.setRotation(-360, 3000);
    }
}

你可能感兴趣的:(android,安卓,地图,mAppwidget)