I need to add icon to my map, something like image below :
this is my code :
MarkerOptions marker = new MarkerOptions().position(location).title("resturan")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.a2));
I manage to add icon to my map but it's just an icon with no border and it doesn't look nice , in the above image , there is a border around each image , this is what i need
How can I add this border to my icons ?
thank you
Take a look at this blog post I wrote on how to put images inside the InfoWindow in an asynchronous way:
Guide: Google Maps V2 for Android : Update InfoWindow with an Image Asynchronously
You can see there how to specify a layout for your InfoWindow as well, in the layout you could add the borders to your image using the margins attribute.
This way you can customize your map icon before apply in the map.
public Bitmap createDrawableFromView(Context context) {
View marker = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).
inflate(R.layout.map_popup, your_parent_layout,false);
TextView tv_location_name = (TextView) marker.findViewById(R.id.tv_location_name);
TextView tv_event_status = (TextView) marker.findViewById(R.id.tv_event_status);
TextView tv_event_name = (TextView) marker.findViewById(R.id.tv_event_name);
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
marker.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
marker.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
marker.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(marker.getMeasuredWidth(), marker.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
marker.draw(canvas);
return bitmap;
}
Related
I been using google map utils for a couple of days now and it seemed so fine.
This is code im using to add a marker with a custom layout file to the map.
IconGenerator mIconGenerator = new IconGenerator(getApplicationContext());
View mView = getLayoutInflater().inflate(R.layout.map_location_label, null);
TextView mInfoWindowText = (TextView) mView.findViewById(R.id.location);
mInfoWindowText.setText(Takzy.pickupLocationText);
mIconGenerator.setContentView(mView);
mIconGenerator.setRotation(-90);
mIconGenerator.setContentRotation(90);
mIconGenerator.setContentPadding(0, 0, 0, 0);
view.Bitmap mBitmapPickupLocation = mIconGenerator.makeIcon();
Bitmap mBitmapDestinationLocation = mIconGenerator.makeIcon(Takzy.destinationLocationText + " >");
// add the pickup marker to map
mMap.addMarker(
new MarkerOptions().title(Takzy.pickupLocationText)
.position(Takzy.pickupLatLng)
.icon(BitmapDescriptorFactory
.fromBitmap(mBitmapPickupLocation))
.anchor(mIconGenerator.getAnchorU(), mIconGenerator.getAnchorV()));
Everything is okay but as you can see, my text gets cut from the marker label. I have tried googling, read the docs over and over, and none of them worked.
it will work fine if I do not rotate the marker. but I need my marker rotated.
Can someone point out what might be the issue is?
I'm trying to use a custom view as the icon next to the up button. I searched for a solution online and read something about using a drawing cache and tried implementing it in my code but no icon is showing.
ActionBar bar = getSupportActionBar();
bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.scheme_dark_blue)));
bar.setTitle(i.getStringExtra("schoolDay"));
CalendarIcon icon = (CalendarIcon) getLayoutInflater().inflate(R.layout.icon_calendar, null);
icon.setDrawingCacheEnabled(true);
day = Integer.parseInt(i.getStringExtra("date"));
icon.setData(day + "");
icon.buildDrawingCache();
bar.setIcon(new BitmapDrawable(getResources(), icon.getDrawingCache()));
bar.setDisplayHomeAsUpEnabled(true);
Thanks for any help :)
Your code is probably not working because the view does not have a size yet. I also suggest you don't use the drawing cache but instead draw the view on a canvas.
I have written a little example where the view is given a size when it doesn't have one yet. You may want to tune to code a bit to fit your needs.
Example:
public static Drawable createDrawableFromView(View v, int requestedWidth, int requestedHeight) {
// Because the view is never shown it does not have a size, but if shown don't resize it.
if (v.getMeasuredHeight() <= 0) {
// You may want to change these lines according to the behavior you need.
int specWidth = MeasureSpec.makeMeasureSpec(requestedWidth, MeasureSpec.AT_MOST);
int specHeight = MeasureSpec.makeMeasureSpec(requestedHeight, MeasureSpec.AT_MOST);
v.measure(specWidth, specHeight);
}
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
return new BitmapDrawable(v.getContext().getResources(), b);
}
Usage:
getActionBar().setTitle("View to icon test");
TextView view = new TextView(this);
view.setText("AI");
view.setGravity(Gravity.CENTER);
view.setTextColor(Color.RED);
getActionBar().setIcon(createDrawableFromView(view, 250, 250));
Result:
I'm trying to create a dynamic icon menu for my android application.
The icon is made from 2 drawable, a 9-patch background image with the icon image overlay on it.
With reference to overlay two images in android to set an imageview, I've the following code which have the overlay nicely.
Resources res = parent.getResources();
Drawable icon_bg = res.getDrawable(R.drawable.menu_icon_bg);
Drawable icon = res.getDrawable(R.drawable.menu_icon);
// Not working
int int_icon_height = icon.getIntrinsicHeight();
int int_icon_width = icon.getIntrinsicWidth();
Rect rect_icon_bg_bound = new Rect();
rect_icon_bg_bound.left = 0;//(int) Math.round(int_icon_width*-1.5);
rect_icon_bg_bound.right = (int) Math.round(int_icon_width*1.5);
rect_icon_bg_bound.top = 0;//(int)Math.round(int_icon_height*-1.5);
rect_icon_bg_bound.bottom = (int)Math.round(int_icon_height*1.5);
icon_bg.setBounds(rect_icon_bg_bound);
Drawable[] layers = new Drawable[2];
layers[0] = icon_bg;
layers[1] = icon;
LayerDrawable layerDrawable = new LayerDrawable(layers);
ImageView iv_icon_combined = new ImageView(getApplicationContext());
iv_icon_combined.setImageDrawable(layerDrawable);
The problem I'm facing right now is adding a padding so that the icon will have some spacing within the background.
Hope to get some enlightenment here, thanks in advance.
I just ran into this same issue. Take a look at the setLayerInset method on LayerDrawable. It takes the layer level as an argument and then the modifiers for the left, top, right, bottom bounds of the drawable.
I'm building a widget which displays some text. By widget I mean the kind which lies on the desktop.
The problem is that I want to change text's font at runtime. There is several textview I would like, at runtime, to set the first as bold, the second blue and italic for example, etc.
I came up with this :
TextView tv = new TextView(context);
tv.setText(stringToDisplay);
tv.setTextColor(0xa00050ff); // example
tv.setTextSize(30); // example
Bitmap b = loadBitmapFromView(tv);
updateViews.setImageViewBitmap(R.id.id_of_the_imageview, b);
with
private static Bitmap loadBitmapFromView(View v)
{
Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
but it wont work (NullPointerException on first line of loadBitmap), until I replace
v.getLayoutParams().width, v.getLayoutParams().height by fixed sizes like 250, 50
Bitmap b = Bitmap.createBitmap(250, 50, Bitmap.Config.ARGB_8888);
// ...
v.layout(0, 0, 250, 50);
But that's not a good solution ...
so I tried this :
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = li.inflate(R.layout.widget_text, null);
TextView tv = (TextView) row.findViewById(R.id.id_of_the_textview);
widget_text being a layout similar to the displayed one but with TextViews instead of ImageViews, in the hope to get some size information out of it ..
but it's not working and I get this exception :
01-02 17:35:06.001: ERROR/AndroidRuntime(11025): Caused by: java.lang.IllegalArgumentException: width and height must be > 0
on the call to Bitmap.createBitmap()
so, someone could point me in the right direction?
Through RemoteViews you can still hide and show elements, so why not duplicate the text views for all font styles you might encounter?
something like:
widget.setViewVisibility(R.id.textview_italic,View.GONE);
widget.setViewVisibility(R.id.textview_bold,View.VISIBLE);
using images to display text should always be the last resort.
You can set the size dynamically:
float mySize = 10f;
updateViews.setFloat(R.id.textview, "setTextSize", mySize);
For some reason, when I create a bitmap from an image in asset, it does not display fullscreen. When I take an image from drawable, resId, the image shows fullscreen. Why is this?
//the image is not fullscreen
ImageView iv = new ImageView(getBaseContext());
iv.setImageBitmap(bitmapFileFromAssetorSD);
//this makes the image fullscreen
//iv.setBackgroundResource(resId);
iv.setLayoutParams(
new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT
)
);
Try setting the appropriate ImageView.ScaleType
ImageView.setScaleType(ImageView.ScaleType.CENTER_CROP)