我正在使用Android的通用图像加载器(这里是link)
我需要从网址加载图片并将其叠加到另一张图片上。默认情况下,库无法执行此操作,因此我尝试更改它。 问题是现在我需要将LayerDrawable转换为Bitmap。我怎么能这样做?
答案 0 :(得分:11)
只需在由Bitmap支持的Canvas上绘制。
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);
答案 1 :(得分:1)
/* 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>