I'm using the Universal Image Loader for Android (here is the link)
I need to to load an Image from a URL and overlay it to another image. The library can not do it by default, so I'm try to change it. The problem is that now I need to convert a LayerDrawable to Bitmap. How can I do that?
Just draw it on a Canvas which is backed by a Bitmap.
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
/* After receiving error: cannot convert LayerBitmap to BitmapDrawable
This is Java code I used in my android app for my business.
Android Studio version 3.6.2
I have multiple versions of an image that I need to be displayed randomly as icons. I have tried using an xml file to choose random images, but it didn't work for me. The code below only showed the same color truck for two separate truck icons, and I ran the app ten times. I guess it's possible, but doesn't seem highly likely. The code below chooses random images from my specific drawables and displays multiple icons with my random, specified drawables. Sometimes the icons are the same, as I don't specify within the code to not choose the same images. I'm good with this until I decide to fetch icon from firestore or cloud storage of employees.
The following code worked for me:
below "public class" (below your package name and imports)
*/
private ImageView truckPlaceholder;
private int[] images;
//in your onCreate method:
//below shows "rootView.", because I'm using fragments. If you're not using //fragments, just remove "rootView.".
truckPlaceholder = rootView.findViewById(R.id.image_truck_placeholder);
images = new int[]{R.drawable.smoothie_truck_blue, R.drawable.smoothie_truck_light_blue, R.drawable.smoothie_truck_lime,
R.drawable.smoothie_truck_orange, R.drawable.smoothie_truck_red};
//below code is my setMarker method. I display the random image into imageView //first, I get drawable, create Bitmap, draw on canvas, draw the drawable on //canvas, and then have the map add my marker and create icon with //BitmapDescriptorFactory and display the random image to my icon(s).
private void setMarker(DocumentSnapshot documentSnapshot){
Random random = new Random();
truckPlaceholder.setImageResource(images[random.nextInt(images.length)]);
Drawable drawable = truckPlaceholder.getDrawable();
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
String key = documentSnapshot.getId();
HashMap<String, Object> value = (HashMap<String, Object>) documentSnapshot.getData();
double lat = Double.parseDouble(value.get("latitude").toString());
double lng = Double.parseDouble(value.get("longitude").toString());
LatLng location = new LatLng(lat, lng);
if (!mMarkers.containsKey(key)) {
mMarkers.put(key,
mMap.addMarker(new MarkerOptions().title(key).position(location).anchor(.5f, .5f)
.icon(BitmapDescriptorFactory.fromBitmap(bitmap))));
} else {
mMarkers.get(key).setPosition(location);
}
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : mMarkers.values()) {
builder.include(marker.getPosition());
}
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 300));
}
//Here is the xml of my fragment - This doesn't contain the xml of the truck //variations, as I listed them individually (this worked, not the xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_track_us"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/image_truck_placeholder"
android:visibility="invisible"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="90dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp"
android:textAlignment="center"
android:layout_gravity="center_horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:scaleType="matrix" />
</androidx.constraintlayout.widget.ConstraintLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With