Android如何使用单个数组列表进行所有类和活动?

时间:2012-11-02 05:33:14

标签: android google-maps arraylist overlay

在我的应用程序中,我使用googlemaps进行该主要活动,并在googlemaps上为该一个覆盖类免费手绘草图。对于存储路径的这个自由手绘草图,在覆盖类中使用一个数组列表。接下来在主活动中执行撤消和重做功能,所以我必须在主活动中使用数组列表,因为我将覆盖类中的数组列表声明为公共静态。随着静态的使用我有连续绘图这是我的问题..如何使用数组列表,而不是在我的应用程序中使用静态....

public class HandDrawOverlay extends Overlay {

   private boolean iseditMode = true;
   private boolean isTouched = false;
   private Paint paint = new Paint();
   private Point screenPt1 = new Point();
   private Point screenPt2 = new Point();
   public static ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();    
   int x, y;
   GeoPoint geoP;
   HandDrawOverlay handDrawOverlay;
   private GoogleMapActivity mview = null;
   private boolean isUp = false;     

   public HandDrawOverlay(GoogleMapActivity mapviewdata){

      paint.setStrokeWidth(4.0f);
      paint.setStyle(Style.STROKE);
      paint.setColor(Color.BLUE);
      mview = mapviewdata;       
   }

   public static ArrayList<GeoPoint>  getBitmap(){
          return points;
   }   

   @Override
   public void draw(Canvas canvas, MapView mapView, boolean shadow) {    
    if (points != null && points.size() > 1) {           
        mapView.getProjection().toPixels(points.get(0), screenPt1);

        for (int i = 1; i < points.size(); i++) {            
            mapView.getProjection().toPixels(points.get(i), screenPt2);
            canvas.drawLine(screenPt1.x, screenPt1.y, screenPt2.x, screenPt2.y, paint);
            screenPt1.set(screenPt2.x, screenPt2.y);                 
        }
     }
  }

   @Override
   public boolean onTouchEvent(MotionEvent e, MapView mapView) {
    if(mview.isEditMode() && (iseditMode)){         

        x = (int) e.getX();
        y = (int) e.getY();           
        geoP = mapView.getProjection().fromPixels(x, y);    
        Log.i("", "geoP" +geoP);

        switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:                            
            isTouched = true;        
            points.add(geoP);                               
            break;              

        case MotionEvent.ACTION_MOVE:            
            if (isTouched = true)
                points.add(geoP);               
            break;

        case MotionEvent.ACTION_UP:          
            if (isTouched = true)
                points.add(geoP);                    
            isUp = true;
            handDrawOverlay = new HandDrawOverlay(mview);
            mapView.getOverlays().add(handDrawOverlay);
            break;
        }           
        mapView.postInvalidate();    
        return true;            
      }      
       return false;
   }     
}

1 个答案:

答案 0 :(得分:0)

为此你可以使用它。

public class Hiroshima {

    private static Hiroshima hr = new Hiroshima();


    public ArrayList<String> mImages = new ArrayList<String>();


      private Hiroshima(){}

      public static Hiroshima getInstance(){

          return hr;
      }

}

现在你可以在Anywhere.And中使用这个ArrayList。它会给你相同的内容。

在你的活动中写下如下。

Hiroshima hx = Hiroshima.getInstance();