解决百度地图MapView在ScrollView中的拖动黑影(二)
ort 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
> imageAche
= new HashMap
>(); /** * @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
reference = imageAche.get(place); if(reference != null){ Bitmap bitmap = reference.get(); if(bitmap == null){ new LoadMapImageTask().execute(place,"https://www.cppentry.com/upload_files/article/76/1_x4mjb__.png"); }else { setImageBitmap(bitmap); } }else { new LoadMapImageTask().execute(place,"https://www.cppentry.com/upload_files/article/76/1_x4mjb__.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
{ @Override protected Bitmap doInBackground(String... params) { LogUnit.Log(TAG, "start do back:"+params[0]); InputStream stream = null; try { List
nameva luePairs = new ArrayList
(); nameva luePairs.add(new BasicNameva luePair("center", params[0])); nameva luePairs.add(new BasicNameva luePair("width", GenericUtil.dp2px(380, getContext())+"")); nameva luePairs.add(new BasicNameva luePair("height", GenericUtil.dp2px(200, getContext())+"")); nameva luePairs.add(new BasicNameva luePair("markers", params[0])); nameva luePairs.add(new BasicNameva luePair("markerStyles", "-1,"+params[1]+",-1")); nameva luePairs.add(new BasicNameva luePair("zoom", "12")); stream = HttpClientImp.INSTANCE.getForStream("http://api.map.baidu.com/staticimage", null,nameva luePairs); Bitmap bitmap = BitmapFactory.decodeStream(stream); imageAche.put(params[0], new SoftReference
(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); } } }