解決百度地圖MapView在ScrollView中的拖動黑影

因爲想在一個Model的屬性下面有一個地址。

想在界面上顯示一個MapView 然後覆寫MapView的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Override
    public boolean onTouchEvent(MotionEvent arg0)
    {
//      return super.onTouchEvent(arg0);
        return false;
    }
 
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev)
    {
//      return super.dispatchTouchEvent(ev);
        return false;
    }
 
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
//      return super.onInterceptTouchEvent(ev);
        return false;
    }

這樣的MapView就不可以接受觸控事件。


但是問題出現了。在ScrollView上,拖動的時候就出現黑影。

我想是不是計算能力不夠。反正就像一張圖片,在第一次加載完成後,變成Bitmap,設置一個圖片進去。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
protected void dispatchDraw(Canvas canvas)
{
    LogUnit.Log(TAG, "dispatchDraw ");
    super.dispatchDraw(canvas);
}
 
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime)
{
    LogUnit.Log(TAG, "drawChild ");
    return super.drawChild(canvas, child, drawingTime);
}

這樣去打印發現,就是地圖第一次加載的時候打印執行。也就是MapView自己本身就是帶有這樣的優化 的。拖動根本不會去調動上面的方法重繪。

所以我就去看了一個靜態地圖的一個api。這個APi設計應該相對是針對web的。

http://developer.baidu.com/map/staticimg.htm這是百度靜態地圖的API文檔。

簡單的說就是給百度一個連接,返回你想要的地圖。

這個連接是GET請求,GET的URL的長度小於300。

Android端就是用ImageView去做的。用 AsyncTask多線程去下載圖片。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    private final class LoadMapImageTask extends AsyncTask<string, void,bitmap="">{
         
    @Override
        protected Bitmap doInBackground(String... params)
        {
        InputStream stream = null;
        try
                {
                List<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>();
                nameValuePairs.add(new BasicNameValuePair("center", params[0]));    //地圖的中心,可以使中文地址,也可以是座標,經緯度用逗號隔開
                nameValuePairs.add(new BasicNameValuePair("width", GenericUtil.dp2px(380, getContext())+""));//返回圖片的寬度
                nameValuePairs.add(new BasicNameValuePair("height", GenericUtil.dp2px(200, getContext())+""));//返回圖片的高度
                nameValuePairs.add(new BasicNameValuePair("markers", params[0]));       //標記的地址,可以使中文地址,或者編碼後的座標
                nameValuePairs.add(new BasicNameValuePair("markerStyles", "-1,"+params[1]+",-1"));//標記地址的圖標,自定義圖標的話,必須用網絡圖標,並                                                     //且小於5KB。
                nameValuePairs.add(new BasicNameValuePair("zoom", "12"));
                //HttpClientImp在http://blog.csdn.net/u012565107/article/details/20561701文章有講解,簡單的使用
                stream = HttpClientImp.INSTANCE.getForStream("http://api.map.baidu.com/staticimage", null,nameValuePairs);
                Bitmap bitmap = BitmapFactory.decodeStream(stream);
                imageAche.put(params[0], new SoftReference<bitmap>(bitmap));//並且把得到的BitMAP儲存到軟引用中,有一定的緩存效果。如果是大量的圖片
                //可以嘗試用 本地的文件做緩存。
                return bitmap;
            }
            catch (Exception e)
            {
                // TODO: handle exception
            }finally{
                try
                {  
                    stream.close();//關閉文件流。
                }
                catch (Exception e2)
                {
                    // TODO: handle exception
                }
            }
            return null;
        }
 
        @Override
        protected void onPostExecute(Bitmap result)
        {
            setImageBitmap(result);
//          result = null;
            super.onPostExecute(result);
        }
         
    }</bitmap></namevaluepair></namevaluepair></string,>


並且用Map<string, softreference> imageAche 去實現一個緩存。

用SoftReference(軟引用)做緩存Google一下吧很多教程。


使用的時候調用SetMapView();把地址設置進去就行了。

效果:

\

下面是這個ImageView的全部代碼。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
 * CopyRight    2014 ZhuYan
 *  @author Zhu Yan
 
 *  All right reserved
 
 *  Created on  2014-3-13  下午1:24:19
 */
package com.mengqi.base.ui.widget;
 
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
 
import com.mengqi.base.util.LogUnit;
import com.mengqi.base.util.rsa.GenericUtil;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.widget.ImageView;
 
/**
 * @author Zhu Yan
 *
 * Created  on  2014-3-13  下午1:24:19
 */
public class LocationImageView extends ImageView
{
    public static final String TAG = "LocationImageView";
 
    private static final Map<string, softreference<bitmap="">> imageAche
                    = new HashMap<string, softreference<bitmap="">>();
     
    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public LocationImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
     
    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public LocationImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public LocationImageView(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub
    }
     
    /**
     * 可以是中文地址。也可以是座標,座標經緯度用逗號隔開。
     * @param place
     */
    public void setMapView(String place){
        LogUnit.Log(TAG, "set map:"+place);
        SoftReference<bitmap> reference = imageAche.get(place);
        if(reference != null){
            Bitmap bitmap = reference.get();
            if(bitmap == null){
                new LoadMapImageTask().execute(place,"http://mengqitech.com/mobilecrm/static/map_pins/company_geo_pin.png");
            }else {
                setImageBitmap(bitmap);
            }
        }else {
            new LoadMapImageTask().execute(place,"http://mengqitech.com/mobilecrm/static/map_pins/company_geo_pin.png");
        }
    }
 
//  @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
//    {
//      int width = MeasureSpec.getSize(widthMeasureSpec);
//      int height = width*708/1317;
//      super.onMeasure(widthMeasureSpec,
//              MeasureSpec.makeMeasureSpec(height,
//              MeasureSpec.getMode(heightMeasureSpec)));
//    }
     
    private final class LoadMapImageTask extends AsyncTask<string, void,bitmap="">{
         
        @Override
        protected Bitmap doInBackground(String... params)
        {
            LogUnit.Log(TAG, "start do back:"+params[0]);
            InputStream stream = null;
            try
            {
                List<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>();
                nameValuePairs.add(new BasicNameValuePair("center", params[0]));
                nameValuePairs.add(new BasicNameValuePair("width", GenericUtil.dp2px(380, getContext())+""));
                nameValuePairs.add(new BasicNameValuePair("height", GenericUtil.dp2px(200, getContext())+""));
                nameValuePairs.add(new BasicNameValuePair("markers", params[0]));
                nameValuePairs.add(new BasicNameValuePair("markerStyles", "-1,"+params[1]+",-1"));
                nameValuePairs.add(new BasicNameValuePair("zoom", "12"));
                 
                stream = HttpClientImp.INSTANCE.getForStream("http://api.map.baidu.com/staticimage", null,nameValuePairs);
                Bitmap bitmap = BitmapFactory.decodeStream(stream);
                imageAche.put(params[0], new SoftReference<bitmap>(bitmap));
                LogUnit.Log(TAG, "ok ");
                 
                return bitmap;
            }
            catch (Exception e)
            {
                // TODO: handle exception
            }finally{
                try
                {
                    stream.close();
                }
                catch (Exception e2)
                {
                    // TODO: handle exception
                }
            }
            return null;
        }
 
        @Override
        protected void onPostExecute(Bitmap result)
        {
            setImageBitmap(result);
//          result = null;
            super.onPostExecute(result);
        }
         
    }
     
 
     
}
</bitmap></namevaluepair></namevaluepair></string,></bitmap></string,></string,>



轉自:http://www.2cto.com/kf/201403/288494.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章