Can I inflate a view replacing the icon in mapbox android? - java

When adding markers in mapbox, there is a provision to add custom icon to the marker. I wonder whether we can inflate a view(R.layout file) instead of assigning a drawable icon.
Here is the code:-
public void onMapReady(MapboxMap mapboxMap) {
IconFactory iconFactory=IconFactory.getInstance(context);
for(int i=0;i<coordinates.size();i++){
mapboxMap.addMarker(new MarkerOptions()
.position(new LatLng(lat,longt))
.icon(iconFactory.fromResource(R.drawable.ic_location_green))
//can we inflate a view here instead of assigning a drawable image?
}
}

I don't think that it is possible
What you can do is draw custom icon at runtime:
Draw it on Canvas
Generate Drawable at runtime (instead of creating xml, you can create an object. So if you were to type <shape>, you can replace it with new Shape(); in Java)
Generate a view and copy its bitmap (How to convert Views to bitmaps?) this option would provive only looks of it - things like click listeners will not work, thus I don't see a reason for choosing this option

This possible using the following utility class:
/**
* Utility class to generate Bitmaps for Symbol.
* <p>
* Bitmaps can be added to the map with {#link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}
* </p>
*/
private static class SymbolGenerator {
/**
* Generate a Bitmap from an Android SDK View.
*
* #param view the View to be drawn to a Bitmap
* #return the generated bitmap
*/
public static Bitmap generate(#NonNull View view) {
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(measureSpec, measureSpec);
int measuredWidth = view.getMeasuredWidth();
int measuredHeight = view.getMeasuredHeight();
view.layout(0, 0, measuredWidth, measuredHeight);
Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
bitmap.eraseColor(Color.TRANSPARENT);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
}
A full example integrating this code can be found here. Note that this code can be executed on a background thread, so you don't need to block the main thread for it. While we don't expose a binary atm we are looking into creating a little plugin around this code. The feature request for it can be found here.

Related

Android RotateDrawable on SVG produces jagged edges

I'm trying to visualize a rotating object on an Android 9 device by programmatically rotating a vector drawable, created from a .svg file using the New->Vector Asset method. The rotation of the object should follow the changes of some external property, instead of a predefined animation.
When a vector image is drawn on an ImageView as a VectorDrawable, the image produced has smooth edges as it should, but when the image is rotated programmatically using a RotateDrawable, the edges become jagged, as if the image was treated as a bitmap, and not redrawn as vector graphics.
The image below illustrates this problem:
According to the VectorDrawable documentation, a bitmap cache is created for each vector asset when it is first loaded, but is there a possibility to force the re-rendering when the image is rotated?
Below is some sample code used to create the effect.
Drawable class:
public class MyDrawable extends Drawable implements Drawable.Callback, Runnable {
#Override
public void draw(Canvas canvas)
{
VectorDrawable vectorDrawable = (VectorDrawable)mainActivity.getResources().getDrawable(R.drawable.test_drawable, mainActivity.getTheme());
vectorDrawable.setBounds(canvas.getClipBounds());
vectorDrawable.draw(canvas); // This looks OK
RotateDrawable rotator = new RotateDrawable();
rotator.setBounds(canvas.getClipBounds());
rotator.setLevel(1000);
rotator.setDrawable(vectorDrawable.mutate());
rotator.draw(canvas); // Jagged edges
}
}
Used in an ImageView in a Fragment:
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
final ImageView testView = (ImageView)getView().findViewById(R.id.test_view);
testView.setImageDrawable(new MyDrawable());
}
There seems to be no built-in support currently in Android for what I'm trying to achieve, but I found at least one working solution, which is to use the third-party AndroidSVG library and modify the .svg upper-most group <g> element directly to include a transformation:
#Override
public void draw(Canvas canvas)
{
try {
String svgString = // Your .svg file as plaintext
svgString = svgString.replaceFirst("<g>", "<g transform=\"rotate(" + angle + ", " + pivotX + " , " + pivotY + ")\">");
SVG svg = SVG.getFromString(svgString);
svg.renderToCanvas(canvas);
// ...
}
catch (SVGParseException svgpe){}
}

How obtain the Context object into a class extenging Fragment?

I am pretty new new in Android and I am finding some dificulties to do the following thing related to the Context.
So I have an utility class that contain an utility method that create and return a Bitmap image, this is the code of my class:
public class ImgUtility {
/**
* Method that create the images related to the difficulty of a recepy
* #param context
* #param difficulty that represent the number of chef_hat_ok into the final image
* #return a Bitmap representing the difficult of a recepy
*/
public static Bitmap createRankingImg(Context context, int difficulty) {
// Create a Bitmap image starting from the star.png into the "/res/drawable/" directory:
Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.chef_hat_ok);
// Create a new image bitmap having width to hold 5 star.png image:
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth() * 5, myBitmap.getHeight(), Bitmap.Config.RGB_565);
/* Attach a brand new canvas to this new Bitmap.
The Canvas class holds the "draw" calls. To draw something, you need 4 basic components:
1) a Bitmap to hold the pixels.
2) a Canvas to host the draw calls (writing into the bitmap).
3) a drawing primitive (e.g. Rect, Path, text, Bitmap).
4) a paint (to describe the colors and styles for the drawing).
*/
Canvas tempCanvas = new Canvas(tempBitmap);
// Draw the image bitmap into the cavas:
tempCanvas.drawBitmap(myBitmap, 0, 0, null);
tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth(), 0, null);
tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 2, 0, null);
tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 3, 0, null);
tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 4, 0, null);
return tempBitmap;
}
}
As you can see this class contains the createRankingImg() that take the Context object as parameter and use it to create an image. This object is used to retrieve an image from the resources (into the BitmapFactory.decodeResource() method). What exactly represent the Context object into an Android application?
I know that to obtain the context into an activity class I can use the getResources() method.
My problem is that I have to obtain the context into a class that exetends Fragment.
I have something like this:
public class ScreenSlidePageFragment extends Fragment {
.......................................................................
.......................................................................
.......................................................................
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
.....................................................................
.....................................................................
.....................................................................
switch (mPageNumber + 1) {
case 1:
imgSlideView.setImageResource(R.drawable.carbonara);
((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.carbonara));
ImageView difficultyContainerImageView1 = (ImageView) rootView.findViewById(R.id.difficultyContainer);
difficultyContainerImageView1.setImageDrawable(new BitmapDrawable(getResources(), ImgUtility.createRankingImg(getApplicationContext(), 3)));
break;
.....................................................................
.....................................................................
.....................................................................
}
return rootView;
}
My problem is that when in the previous fragment class I call the method:
ImgUtility.createRankingImg(getResources(), 3)
passing to it the getResources() output (that I thinked give me the Context, the IDE give me the following error message:
Wrong 1st argument type. Found: 'android.content.res.Resources',
required: 'android.content.Context'
So it seems to me that into a class that extends a Fragment and not an Activity the getResources() method return a Resources object instead a Context object (as done into an Activity class). Is it true? Why?
How can I obtain the Context inside a class that extends Fragment? And what exactly represent the Context in an Android app? What am I missing? How can I sole this issue?
Create an initial function that takes Context as a parameter so you can pass the context object from the activity class.

Android creating BitmapDescriptor exception

I'm writing an application that works alot with google map and markers on it. My task is to create and display some amount of markers on google map. Markers have custom image and text in it. Data is loading from server and i need to display new amount of data every time user moves google map camera. So i'm using android-maps-utils:0.4.3 library for creating custom Bitmap (using IconGenerator) and then create BitmapDescriptor from it. Here is part of the code:
googleMap.clear()
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
IconGenerator clusterIconGenerator = new IconGenerator(getActivity());
View clusterView = layoutInflater.inflate(R.layout.marker_cluster_view, null, false);
clusterIconGenerator.setContentView(clusterView);
clusterIconGenerator.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.mark_normal_grey));
List<Cluster> clusters = result.getResult(); // server data
for (Cluster cluster : clusters) {
Bitmap bitmap = clusterIconGenerator.makeIcon(String.valueOf(cluster.getOffersCount()));
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(cluster.getLocation().getLatitude(), cluster.getLocation().getLongitude()))
.icon(BitmapDescriptorFactory.fromBitmap(bitmap)) // crash here
.anchor(0.5f, 0.5f));
markerClusterMap.put(marker, cluster);
}
Everything is ok except application is crashes sometimes (not very often) with 2 different exceptions:
java.lang.RuntimeException: Could not copy bitmap to parcel blob.
at android.graphics.Bitmap.nativeWriteToParcel(Native Method)
at android.graphics.Bitmap.writeToParcel(Bitmap.java:1541)
at com.google.android.gms.maps.model.a.c.a(:com.google.android.gms:237)
at com.google.android.gms.maps.internal.h.a(:com.google.android.gms:227)
at com.google.android.gms.maps.internal.j.a(:com.google.android.gms:183)
at com.google.android.gms.maps.internal.CreatorImpl.a(:com.google.android.gms:32)
at com.google.android.gms.maps.internal.b.a(:com.google.android.gms:227)
at com.google.android.gms.maps.model.a.b.onTransact(:com.google.android.gms:106)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.maps.model.internal.zza$zza$zza.zzc(Unknown Source)
at com.google.android.gms.maps.model.BitmapDescriptorFactory.fromBitmap(Unknown Source)
at com.cheapsta.cheapsta.fragments.GoogleMapFragment.clustersLoadingFinished(GoogleMapFragment.java:187)
and sometimes
java.lang.RuntimeException: Could not allocate dup blob fd.
at android.graphics.Bitmap.nativeCreateFromParcel(Native Method)
at android.graphics.Bitmap.-wrap0(Bitmap.java)
at android.graphics.Bitmap$1.createFromParcel(Bitmap.java:1516)
at android.graphics.Bitmap$1.createFromParcel(Bitmap.java:1515)
at maps.bx.a$a.onTransact(:com.google.android.gms.alldynamite:101)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.maps.model.a.c.a(:com.google.android.gms:242)
at com.google.android.gms.maps.internal.h.a(:com.google.android.gms:227)
at com.google.android.gms.maps.internal.j.a(:com.google.android.gms:183)
at com.google.android.gms.maps.internal.CreatorImpl.a(:com.google.android.gms:32)
at com.google.android.gms.maps.internal.b.a(:com.google.android.gms:227)
at com.google.android.gms.maps.model.a.b.onTransact(:com.google.android.gms:106)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.maps.model.internal.zza$zza$zza.zzc(Unknown Source)
at com.google.android.gms.maps.model.BitmapDescriptorFactory.fromBitmap(Unknown Source)
at com.cheapsta.cheapsta.fragments.GoogleMapFragment.clustersLoadingFinished(GoogleMapFragment.java:187)
What can i do with this? I guess i'm using my memory to much to create BitmapDescriptors. It's nearly 20 BitmapDescriptos every 3 seconds if user moving camera too much. Should i cache it somehow? Thx a lot for your answers and time!
Well here is what i got. Looks like BitmapFactory can't create Bitmap if it don't have enought memory. So if GC didn't do job and u don't have enough memory u'll get this exception. In my case that was pretty often because i need to generate about 10-20 markers every time user moves google map camera.
First of all don't be stupid like me and don't use android-maps-utils just for IconGenerator :) I wrote my own class that generate's BitmapDescriptor from Bitmap and caches it in LruCache. Here's good tutorial for caching Bitmaps. You can do almost the same for BitmapDescriptor. Pay attention to LruCache size. You can't get BitmapDescriptor size in bytes, so you need to think about amount of these objects in LruCache. Just look at your bitmap size and do some calculations.
If you need text in your image do something like this:
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mark_active_grey).copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(offersCount,
canvas.getWidth()/2,
canvas.getHeight()/2 - ((clustersPaint.getFontMetrics().ascent + clustersPaint.getFontMetrics().descent) / 2) ,
clustersPaint);
Sorry for bad english and i hope this information will be useful to some one.
I had same problem. With Kuva's answer I make a new class like this:
public class MapBmpContainter
{
private int mBmpSize;
public BitmapDescriptor mBmpDescriptor;
public MapBmpContainter(Bitmap bmp)
{
mBmpSize=bmp.getByteCount()/1014;
mBmpDescriptor= BitmapDescriptorFactory.fromBitmap(bmp);
}
public int getSize()
{
return mBmpSize;
}
}
I cache new class object in LruCache instead of Bitmap.
Same with Kuva I think Bitmap and BitmapDescriptor almost same size.
And It worked
For me the problem was related to the size of the icon. I just change dynamically the size of the Bitmap and works fine.
Bitmap image2 = Bitmap.createScaledBitmap(iconBitmap, 120, 120, false);
Use Picasso , Glide or Fresco Literary to cache bitmaps efficiently.
Picasso.with(getContext())
.load(R.drawable.marker)
.resize(width, width)
.into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
markerOptionsHome = new MarkerOptions();
markerOptionsHome.title("Home location");
markerOptionsHome.snippet("");
markerOptionsHome.position(latlng);
markerOptionsHome.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
homeLocationMarker = map.addMarker(markerOptionsHome);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) { }
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) { }
});

Picasso image downloading & caching behavior

So I recently wanted to try out the caching feature of the Picasso library, & I got into this confusing situation:
I retrieve the images' file names & paths from my web server (using Retrofit2), & I store them into ImageComponent objects (model):
public class ImageComponent {
private int id; // 'id' in database
private String filename; // image name
private String path; // image path in server storage
private Bitmap bitmap;
// Overloaded constructor
// Getters & setters
}
So now that the loading is successful, I populate a RecyclerView with these images using Picasso. The loading and inflation process is successful, but it gets a little tricky when caching the images.
Case1: using android.util.LruCache
(For convenience, I will post the entire code of the Recyclerview's adapter. I will try to be concise)
// imports
import android.util.LruCache;
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
private Context mContext; // Activity's context
private List<ImageComponent> mImages; // The imageComponents to display
// The contreversial, infamous cache
private LruCache<Integer, Bitmap> mImageCache;
public ImageAdapter(Context context, List<ImageComponent> images) {
mContext = context;
mImages = images;
// Provide 1/8 of available memory to the cache
final int maxMemory = (int)(Runtime.getRuntime().maxMemory() /1024);
final int cacheSize = maxMemory / 8;
mImageCache = new LruCache<>(cacheSize);
}
#Override
public ImageAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Nothing special
}
#Override
public void onBindViewHolder(final ImageAdapter.ViewHolder holder, final int position) {
// Current ImageComponent
ImageComponent imageComponent = mImages.get(position);
// Full image path in server storage
String imagePath = Constants.SERVER_IP_ADDRESS + Constants.UPLOADS_DIRECTORY
+ imageComponent.getPath();
// Display the file's name
holder.text.setText(imageComponent.getFilename());
final ImageView imageView = holder.image;
// Get bitmap from cache, check if it exists or not
Bitmap bitmap = mImageCache.get(imageComponent.getId());
if (bitmap != null) {
Log.i("ADAPTER", "BITMAP IS NOT NULL - ID = " + imageComponent.getId());
// Image does exist in cache
holder.image.setImageBitmap(imageComponent.getBitmap());
}
else {
Log.i("ADAPTER", "BITMAP IS NULL");
// Callback to retrieve image, cache it & display it
final Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
ImageComponent img = mImages.get(position);
// Display image
holder.image.setImageBitmap(bitmap);
// Cache the image
img.setBitmap(bitmap);
mImages.set(position, img);
mImageCache.put(img.getId(), bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
// Tag the target to the view, to keep a strong reference to it
imageView.setTag(target);
// Magic
Picasso.with(mContext)
.load(imagePath)
.into(target);
}
}
#Override
public int getItemCount() {
return mImages.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView text;
// Constructor & view binding, not that special
}
}
RESULT1
(Notice those 2 last images, & how they show other previous images before displaying the correct one)
A few notes:
I ran across a problem, where the images weren't displayed at all. After some research, I found this answer which suggested binding the target to the ImageView. (worked)
I didn't quite understand how Picasso caches the images. Is it an automatic or manual process ? This answer states that Picasso handles this task for you. But when I actually tried it out (without the android Lrucache), no caching seemed to be done : The images were getting reloaded every time I scroll back & forth.
Actually I was going to post a second use case where things went even more wrong, using the Picasso's Lrucache (images were being shown randomly , & change with every scroll), but I think this post is already long enough.
My questions are:
Why do I get that weird behavior ? (as shown in the attached GIF)
How does this whole caching process work ? Should I (or could I) use a Lrucache when making use of Picasso ?
What's the difference between the Lrucache that comes with the SDK & Picasso's ? (Performance, best use case scenarios, etc...)
I think using both LRU cache and Picasso is causing the weird behaviour. I have used Picasso to cache Image to an Adapter, which works completely fine. you can check in here
Picasso cache Image automatically when used with adapter, it will cache like this, if the child item of list/Recycler view is not visible it will stop caching the image for the respective child.So it's better to use Picasso alone with Adapter.
The main usage of Picasso over LRU cache is that, Picasso is easy to use.
ex : specifying Memory cache Size in Picasso.
Picasso picasso = new Picasso.Builder(context)
.memoryCache(new LruCache(250))
.build();
Picasso also allow you to notify user with an Image when there is an error in downloading, a default holder for Imageview before loading the complete image.
Hope it helps.

When in the Activity life cycle are UI elements available to be measured?

I am defining some animations based on the inflated dimensions of some UI controls. What is the earliest point in the Activity life cycle I can tap into to know when the UI elements have been sized and I can query them for their dimensions?
Right after you set the Content of you Activity via the setContentView() method is the earliest I've been able to grab information from my widgets (size, text and others).
Per Rich's request:
You can determine when the width and height by using the GlobalLayoutListener like so:
final View myView = findViewById(R.id.id_of_view);
ViewTreeObserver vto = myView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int viewHeight = myView.getHeight();
int viewWidth = myView.getWidth();
// Do what you want with the width and height
ViewTreeObserver obs = myView.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
Full (better) answer: How to retrieve the dimensions of a view?
If you want to drill down to the point that widget have JUST been placed you have to extend each widget you want to monitor.
Then override onDraw method and capture if that view has been drawn one time
private boolean imVisible=false;
public boolean imVisible() {
return imVisible;
}
#Override
protected void onDraw(Canvas canvas) {
if(!imVisible){
imVisible=true;
}
super.onDraw(canvas);
}
Then you can do a for loop to the widgets of interest and you know they are drawn at their position with dimentions.
A far better solution is when the onDraw gets called the first time fire a listener that is drawn. You have to set an array of listeners that watch the progress of the widgets. That way the exact moment that the last widget is on the screen... you know it.

Categories

Resources