如何将LayerDrawable转换为位图?

问题描述 投票:6回答:2

我正在使用适用于Android的通用图像加载器(这里是link

我需要从URL加载图像并将其覆盖到另一个图像。默认情况下,该库无法执行此操作,因此,我尝试对其进行更改。问题在于,现在我需要将LayerDrawable转换为Bitmap。我该怎么办?

android bitmap android-drawable
2个回答
12
投票

只需在具有位图支持的画布上绘制它。

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
投票
    /* 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>
© www.soinside.com 2019 - 2024. All rights reserved.