0

我在drawable-hdpi中有所有图像。我需要将它们放在其他可绘制文件中吗?如果它与覆盖有关,有人可以给我一个例子,说明如何用覆盖处理每个语句?

package com.CS3040.Places;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import com.CS3040.Coursework.R;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.OverlayItem;

public class PlaceOverlayItem extends OverlayItem {
    private final GeoPoint point;
    private final Place place;
    private final Drawable marker;
    //private final Context context;

    public PlaceOverlayItem(Context context, Place p, String type) {
        super(p.getGeoPoint(), p.getName(), p.getFormatted_address());

        if(type.equals("restaurant"))
        { 
            //this.marker = 

            Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.restaurant);
            this.marker = new BitmapDrawable(context.getResources(), bmp);

            //Drawable d = context.getResources().getDrawable(R.drawable.restaurant);
            //d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            //this.marker = d;

//          Resources resources = context.getResources();
//          Bitmap bmp = BitmapFactory.decodeResource(resources, R.drawable.restaurant);
//          this.marker = new BitmapDrawable(resources, bmp);

        }else if(type.equals("bar||club")){
            Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.bars);
            this.marker = new BitmapDrawable(context.getResources(), bmp);
        }else if(type.equals("amusement_park||aquarium||park||zoo")){
            Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.attractions);
            this.marker = new BitmapDrawable(context.getResources(), bmp);
        }else if(type.equals("hotel||motel||bread_and_breakfast")){
            Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.hotels);
            this.marker = new BitmapDrawable(context.getResources(), bmp);
        }else if(type.equals("shopping")){
            Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.shopping);
            this.marker = new BitmapDrawable(context.getResources(), bmp);
        }else if(type.equals("food||meal_takeaway")){
            Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.fastfood);
            this.marker = new BitmapDrawable(context.getResources(), bmp);
        }else{
            this.marker = null;
        }

        super.setMarker(this.marker);
        this.point = p.getGeoPoint();
        this.place = p;
    }

    /**
     * @return the point
     */
    public GeoPoint getPoint() {
        return point;
    }

    /**
     * @return the place
     */
    public Place getPlace() {
        return place;
    }

}
4

1 回答 1

1

我认为这可能是您测试 OverlayItem 类型的方式的问题。

仅当类型字符串为 时,条件type.equals("bar||club")才会为真"bar||club"。这意味着如果 type 是“bar”,它将没有任何标记。

我建议的是创建一个静态地图:

private static Map<String, Integer> types = new HashMap<String, Integer>();

在静态块上初始化它:

static {
    types.put("bar", R.drawable.bars);
    types.put("club", R.drawable.bars);
    // ... the rest of mappings
}

然后在叠加层的构造函数上简单地执行以下操作:

public PlaceOverlayItem(Context context, Place p, String type) {
    super(p.getGeoPoint(), p.getName(), p.getFormatted_address());
    int resId = types.get(type);
    Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resId);
    this.marker = new BitmapDrawable(context.getResources(), bmp);
    super.setMarker(this.marker);
    this.point = p.getGeoPoint();
    this.place = p;
}

或者,您也可以在 raw 文件夹中创建一个 json 文件并使用org.json对其进行解析以获取映射。这样您就可以更改图标,而无需转到代码并能够在其他类中重用它。

另一种选择是,如果您愿意实施它,可以使用注释来创建地图。

祝你好运

于 2012-03-16T11:16:14.833 回答