【发布时间】:2016-12-21 10:26:16
【问题描述】:
我的问题是我想在我的应用中显示一个停车场预订系统。我想以绿色显示所有可用空间,当用户预订空间时,同一区域应变为红色。下图是:
我尝试了很多方法,但都没有成功。以下是我的代码:
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;
}
}
现在每辆车都是可点击的,但我需要改变颜色,如图所示,当用户预订空间时,同一区域应该变成红色。请帮我解决这个任务。我是 android 新手,但正在努力解决这个问题。如果我的问题不够强烈,请接受我的道歉。也提前非常感谢。这是我预订空间时的第二个活动
【问题讨论】:
-
你能发布 ImageMap 类吗
-
感谢您的回复让我发布 imageMap 类
-
你能看看吗谢谢
-
我想你理解错了。我在询问“ImageMap”类,但它发布了一些活动。
-
有些我无法将我的 imageMap 类放在这里,但我把它作为链接你可以检查一下吗?谢谢