缩小sdcard中的位图以用作google mapview中的标记

时间:2012-11-19 14:16:06

标签: android android-layout android-intent android-mapview

我正在尝试缩小通过相机拍摄的图像,并将其放在我的应用中的mapview中。但是我可以拍摄图像但它似乎没有缩小到缩略图大小的图像。这是我的MainActivity.java代码

AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
            alert.setTitle("Create Marker");
            alert.setButton(DialogInterface.BUTTON_POSITIVE, "Take Photo", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                //TODO Auto=generated method stub

                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photo));
                o = Uri.fromFile(photo);
                startActivityForResult(intent, TAKE_PICTURE);

                BitmapDrawable bd = (BitmapDrawable) Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "pic.jpg").getAbsolutePath());
                Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.8),
                                                                    (int) (bd.getIntrinsicWidth() * 0.8), false);

                OverlayItem overlayItem = new OverlayItem(touchedPoint, "What's Up", "2nd String");
                CustomPinPoint custom = new CustomPinPoint(d, MainActivity.this);
                custom.insertPinpoint(overlayItem);
                overlayList.add(custom);


            }

            });

            alert.show();
            {
        return true;
        }
        }
        return false;

和我的CustomPinPoint.java

public class CustomPinPoint extends ItemizedOverlay<OverlayItem>{

BitmapDrawable bd;
Bitmap b;
private ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();
private Context c;


public CustomPinPoint(Drawable defaultMarker) {
    super(boundCenter(defaultMarker));
    BitmapDrawable bd = (BitmapDrawable) Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "pic.jpg").getAbsolutePath());
    Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.8),
                                                        (int) (bd.getIntrinsicWidth() * 0.8), false);
    // TODO Auto-generated constructor stub
}
public CustomPinPoint(Drawable m, Context context) {
    this(m);
    c = context;
    // TODO Auto-generated constructor stub

}
@Override
protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return pinpoints.get(i);
}
@Override
public int size() {
    // TODO Auto-generated method stub
    return pinpoints.size();
}
public void insertPinpoint(OverlayItem item){
    pinpoints.add(item);
    this.populate();
}
public void add(CustomPinPoint custom) {
    // TODO Auto-generated method stub

}
public void add(OverlayItem i) {
    // TODO Auto-generated method stub

}
}

谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

要调整位图大小,我使用http://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/

中的函数

它运作良好。 试一试!!

否则,只需在地图上的指定位置添加位图,请执行以下操作:

class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.map_pin);   
            int pinPointPositiony = bmp.getWidth();
            int pinPointPositionx = bmp.getHeight() / 2;

            canvas.drawBitmap(bmp, screenPts.x - pinPointPositionx, screenPts.y - pinPointPositiony, null);         
            return true;
        }
    }

你可以在评论“添加标记”下看到,我从我的资源加载一个位图。但是你可以使用从相机中获得的那个。 再次,有一些小的计算,以确保图像将以这种方式放置:图像的中心底部将指示提供的坐标,用于该图像例如:http://fadigeorge.files.wordpress.com/2011/03/pin_6.png

以下是要在主类中添加的代码(Activity):

map = (MapView)findViewById(R.id.map);
        MapController mc = map.getController();
        double lat = 41.39357371045376;
        double lng = 3.888921489715576;

        p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(14); 


        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = map.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        

        map.invalidate();

希望它有所帮助。