2

我正在尝试缩小通过相机拍摄的图像并将其放置在我的应用程序的 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

}
}

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

4

1 回答 1

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

这是要添加到您的主类(活动)中的代码:

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();

希望能帮助到你。

于 2012-11-19T14:58:30.367 回答