从SD卡缩小位图作为在谷歌图形页面的标记使用 [英] Scale down bitmap from sdcard to use as marker in google mapview

查看:215
本文介绍了从SD卡缩小位图作为在谷歌图形页面的标记使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图缩小通过照相机拍摄的图像,并将其放置在图形页面在我的应用程序。不过,我可以把图像,但它似乎没有它的规模下降到缩略图大小的图片。这里是我的code为MainActivity.java

I am trying to scale down a image taken via camera and place it in mapview in my app. However i can take the image but it doesn't seem to scale it down to a thumbnail size image. Here is my code for 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

and my 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

}
}

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

Can anyone tell me what I am doing wrong?

推荐答案

要调整位图,我使用的 HTTP://thinkandroid.word$p$pss.com/2009/12/25/resizing-a-bitmap/

To resize bitmaps, I use a function from http://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/

它的工作非常出色。
试试看!

It's working very well. Give it a try !!

否则,简单地在指定位置添加一个位图在地图上,做如下所示:

Otherwise, to simply add a bitmap on a specified location on a map, do as shown:

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;
        }
    }

您可以在注释请参见添加标记,我从我的ressources加载一个位图。但是你可以使用你从相机获得的。
下一次,还有一些小的计算,以确保图像将被放置这样:图像的中心下方会显示使用所提供的坐标与图像为例:的 HTTP://fadigeorge.files.word$p$pss.com/2011/03/pin_6.png

You can see under the comment "add the marker", I load a bitmap from my ressources. But you can use the one you get from the camera. Under again, there is some little calculation to ensure that the image will be placed this way: The center bottom of the image will indicate the provided coordinates for use with that image for exemple : http://fadigeorge.files.wordpress.com/2011/03/pin_6.png

这里是code在你的主类(活动)地址:

And here is the code to add in your main class (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();

希望它帮助。

这篇关于从SD卡缩小位图作为在谷歌图形页面的标记使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆