0

我想在我的地图叠加层中添加一个标记(项目)。我的问题是我的标记设置为 ImageView 布局。它有一个 9 补丁 png 作为背景,用户的 Facebook 个人资料图片作为 src。

我的问题是我懒惰下载个人资料图片。如果还没有加载,我会在 src 中放置一个临时图像,然后抛出 AsyncTask 我正在从 Facebook 下载个人资料图片。当图片下载后,我想把它放在标记中..

我该怎么做 ?在所有方法中,标记都将 Drawable 作为对象...

也许与 LayerDrawable 有关?

4

1 回答 1

0

覆盖类应该像下面

public class MapItemOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context mContext;
    private JSONArray marrJson;
    private JSONObject mjsonObj;

    public MapItemOverlay(Drawable defaultMarker, JSONArray arrJson,
            Context context) {
        super(boundCenterBottom(defaultMarker));
        marrJson = arrJson;
        mContext = context;
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(final int i) {

        OverlayItem result = mOverlays.get(i);

        LayerDrawable marker = (LayerDrawable) mContext.getResources()
                .getDrawable(R.drawable.marker);

        GetFBImageTask task = new GetFBImageTask(i, marker, result);
        task.execute(new Void[] {});



        result.setMarker(boundCenter(marker));

        return result;
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    public MapItemOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }

    @Override
    protected boolean onTap(int index) {
        return true;
    }

    public class GetFBImageTask extends AsyncTask<Void, Void, LayerDrawable> {
        private LayerDrawable marker;
        private OverlayItem result;
        private int index;

        public GetFBImageTask(int i, LayerDrawable Mmarker, OverlayItem result1) {
            marker = Mmarker;
            result = result1;
            index = i;
        }

        @Override
        protected void onPostExecute(LayerDrawable bmp) {
            super.onPostExecute(bmp);
            if (bmp != null) {
                result.setMarker(boundCenter(bmp));
            }

        }

        @Override
        protected LayerDrawable doInBackground(Void... p) {


try {


Bitmap bmp = null;
                        String[] params = new String[2];
                params[0] = "http://your image url";
                params[1] = your image name;
                URL ulrn = new URL(params[0]);
                HttpURLConnection con = (HttpURLConnection) ulrn
                                    .openConnection();
                InputStream is = con.getInputStream();
                bmp = BitmapFactory.decodeStream(is);


if (null != bmp) {


    if (params[1] != null) {
                    OutputStream fOut = null;
                    File file = new File(Preference.ImagePath,                                              params[1] + ".jpg");


        if (!file.exists()) {
                            fOut = new FileOutputStream(file);
                                 bmp.compress(Bitmap.CompressFormat.PNG,90, fOut);
                                             fOut.flush();
                                             fOut.close();
                        }
                    }

                }




if (bmp != null) {

                        Drawable photo = new BitmapDrawable(
                                mContext.getResources(), bmp);
                        marker.setDrawableByLayerId(
                                R.id.frame,
                                mContext.getResources().getDrawable(
                                        R.drawable.pa3_map_people_img1));
                        marker.setDrawableByLayerId(R.id.photo, photo);
                        marker.setLayerInset(1, 4, 4, 4, 11);
                        return marker;
                }



            } catch (Exception e) {

            }

            return null;
        }
    }


}

drawble/marker.xml 应该像下面

<item
    android:id="@+id/frame"
    android:drawable="@drawable/pa3_map_people_img1"/>
<item
    android:id="@+id/photo"
    android:drawable="@drawable/pa3_map_people_img1"/>

于 2012-09-27T14:10:37.380 回答