2016-12-21 13 views
-1

私の質問は私のアプリで駐車場予約システムを表示しようとしています。空きスペースをすべて緑色で表示したい場合、ユーザーのブックスペースで同じ領域が赤色に変わるようにする必要があります。下の画像は、次のとおりです。画像内の特定の領域の背景色を変更し、ユーザーが同じ領域をクリックしたときに色を変える方法

enter image description here

私は非常に多くの方法を試してみましたが、任意の成功を取得できませんでした。今

public class CarParkingActivity extends AppCompatActivity { 

private ImageMap mImageMap; 
private RelativeLayout stage; 
private RelativeLayout.LayoutParams lparams; 
private SparseArray<ImageMap.Bubble> spaces; 
private String title; 
Tracker mytracker; 

private List<String> blocks; //list of 'occupied' spaces 

@Override 
protected void onCreate(Bundle savedInstanceCarParking) { 
    super.onCreate(savedInstanceCarParking); 
    setContentView(R.layout.activity_car_parking); 

    blocks = new ArrayList<>(); 
    blocks.add("p1"); 
    blocks.add("p3"); 
    blocks.add("p6"); 
    blocks.add("p13"); 
    blocks.add("p9"); 
    blocks.add("p200"); 


    // find the image map in the view 
    mImageMap = (ImageMap) findViewById(R.id.map); 
    stage = (RelativeLayout) findViewById(R.id.stage); 
    mImageMap.setImageResource(R.drawable.carpark2); 

    title = getIntent().getStringExtra("option"); 
    getSupportActionBar().setTitle(title); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    // add a click handler to react when areas are tapped 
    mImageMap.addOnImageMapClickedHandler(new ImageMap.OnImageMapClickedHandler() { 
     @Override 
     public void onImageMapClicked(int id, ImageMap imageMap) { 

      mImageMap.showBubble(id); 

      if (blocks.contains(imageMap.getArea(id).getName())) { 

       MySingleton.getInstance().showToast(getApplicationContext(), "Sorry, this car space is occupied"); 

       spaces = mImageMap.getParkings(); 
       drawParkSpace(spaces.get(id)._x, spaces.get(id)._y, false); 



      } else { 
       //open booking view 
       Intent it = new Intent(getApplicationContext(), BookingCarParkingActivity.class); 
       it.putExtra("parkId", imageMap.getArea(id).getName().toUpperCase()); 
       startActivity(it); 
      } 


     } 

     @Override 
     public void onBubbleClicked(int id) { 
      Log.i("app", "onBubbleClicked: id: " + id); 
     } 
    }); 


} 

/** 
* Draw red or green rect on the view. 
* 
* @param x 
* @param y 
* @param isOk: true equals green 
*/ 
private void drawParkSpace(float x, float y, Boolean isOk) { 
    Drawable imageView = null; 
    imageView = ContextCompat.getDrawable(CarParkingActivity.this, isOk ? R.drawable.carok : R.drawable.carnook); 

    lparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    ImageView imageView1 = new ImageView(CarParkingActivity.this); 
    imageView1.setImageDrawable(imageView); 
    imageView1.setLayoutParams(lparams); 
    imageView1.setX(x); 
    imageView1.setY(y); 
    stage.addView(imageView1); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    mImageMap.loadMap("menu"); //load menu mapped image 
    //List<ImageMap.PolyArea> list = mImageMap.getPolys(); 
    //ImageMap.PolyArea poly = list.get(0); 

} 


@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    switch (item.getItemId()) { 

     case android.R.id.home: 
      this.finish(); 
      return true; 

    } 


    return false; 



    } 
} 

各車がクリック可能であるが、私は、画像に示されており、同地域が必要とする必要があるユーザー帳スペースを赤くしたときのように色を変更する必要があります。以下は、私のコードです。この仕事を整理するのを手伝ってください。私はアンドロイドでは新しいですが、この問題を解決するために努力しています。私の質問が十分に強くなければ、私の謝罪を受け入れてください。また、事前に多くのおかげです。これは私があなたの必要性のためのスペース

ImageMapClass

+0

投稿可能ImageMapクラス – Nas

+0

返信いただきありがとうございますimageMapクラス –

+0

お返事ありがとうございます –

答えて

1

を予約したときに、第2の活動で、あなたは予約しないで予約したスペースを追跡するために2つのリストと2つのペイントオブジェクトを維持する必要があります。

- 利用可能なスペースの一覧。

-予約されたスペースのリスト。

-bookedスペース塗料以下の

void onDraw(Canvas canvas, boolean isBooked) { 
      if (_a != null) { 
       // Draw a shadow of the bubble 
       float l = _left + mScrollLeft + 4; 
       float t = _top + mScrollTop + 4; 
       canvas.drawRoundRect(new RectF(l, t, l + _w, t + _h), 20.0f, 20.0f, bubbleShadowPaint); 
       Path path = new Path(); 
       float ox = _x + mScrollLeft + 1; 
       float oy = _y + mScrollTop + 1; 
       int yoffset = -35; 
       if (_top > _y) { 
        yoffset = 35; 
       } 
       // draw shadow of pointer to origin 
       path.moveTo(ox, oy); 
       path.lineTo(ox - 5, oy + yoffset); 
       path.lineTo(ox + 5 + 4, oy + yoffset); 
       path.lineTo(ox, oy); 
       path.close(); 
       canvas.drawPath(path, bubbleShadowPaint); 

       // draw the bubble 
       l = _left + mScrollLeft; 
       t = _top + mScrollTop; 
      if (isBooked) { 
       canvas.drawRoundRect(new RectF(l, t, l + _w, t + _h), 20.0f, 20.0f, bookedBubblePaint); 
      } else { 
       canvas.drawRoundRect(new RectF(l, t, l + _w, t + _h), 20.0f, 20.0f, bubblePaint); 
      } 

       path = new Path(); 
       ox = _x + mScrollLeft; 
       oy = _y + mScrollTop; 
       yoffset = -35; 
       if (_top > _y) { 
        yoffset = 35; 
       } 
       // draw pointer to origin 
       path.moveTo(ox, oy); 
       path.lineTo(ox - 5, oy + yoffset); 
       path.lineTo(ox + 5, oy + yoffset); 
       path.lineTo(ox, oy); 
       path.close(); 
      if (isBooked) { 
       canvas.drawPath(path, bookedBubblePaint); 
      } else { 
       canvas.drawPath(path, bubblePaint); 
      } 


       // draw the message 
       canvas.drawText(_text, l + (_w/2), t + _baseline - 10, textPaint); 
      } 
     } 

変更initDrawingToolsなどの方法が予約状況に基づいて、ペイントを描くonDrawスペースがあなたのバブルクラスで、その後

Paint bubblePaint; 
Paint bookedBubblePaint; 

    //list of bubbles available 
    SparseArray<Bubble> mBubbleMap = new SparseArray<Bubble>(); 

    //list of bubbles already booked 
    SparseArray<Bubble> bookedBubble = new SparseArray<Bubble>(); 

protected void drawBubbles(Canvas canvas) { 
     for (int i = 0; i < mBubbleMap.size(); i++) { 
      int key = mBubbleMap.keyAt(i); 
      Bubble b = mBubbleMap.get(key); 
      if (b != null) { 
       b.onDraw(canvas, false);//send info of booked status 

      } 
     } 

     for (int i = 0; i < bookedBubble.size(); i++) { 
      int key = bookedBubble.keyAt(i); 
      Bubble b = bookedBubble.get(key); 
      if (b != null) { 
       b.onDraw(canvas, true);//send info of booked status 

      } 
     } 
    } 

を描く予約する

-yet

private void initDrawingTools() { 
     textPaint = new Paint(); 
     textPaint.setColor(0xFF000000); 
     textPaint.setTextSize(30); 
     textPaint.setTypeface(Typeface.SERIF); 
     textPaint.setTextAlign(Paint.Align.CENTER); 
     textPaint.setAntiAlias(true); 

     textOutlinePaint = new Paint(); 
     textOutlinePaint.setColor(0xFF000000); 
     textOutlinePaint.setTextSize(18); 
     textOutlinePaint.setTypeface(Typeface.SERIF); 
     textOutlinePaint.setTextAlign(Paint.Align.CENTER); 
     textOutlinePaint.setStyle(Paint.Style.STROKE); 
     textOutlinePaint.setStrokeWidth(2); 

     bubblePaint = new Paint(); 
     bubblePaint.setColor(Color.GREEN); 
     bubbleShadowPaint = new Paint(); 
     bubbleShadowPaint.setColor(0xFF000000); 
     bookedBubblePaint= new Paint(); 
     bookedBubblePaint.setColor(Color.RED); 
    } 

    public void addBubble(String text, int areaId) { 
     if (mBubbleMap.get(areaId) == null) { 
      Bubble b = new Bubble(text, areaId); 
      mBubbleMap.put(areaId, b); 
     } 
    } 

    public void addBookedBubble(String text, int areaId) { 
     if (bookedBubble.get(areaId) == null) { 
      Bubble b = new Bubble(text, areaId); 
      bookedBubble.put(areaId, b); 
     } 
    } 

希望すると、これ以上進めるのに役立ちます。

+0

あなたはあなたの期待した結果を得た? – Nas

関連する問題