0

I'm using XML drawables and overlaying them ontop of eachother dynamically using layers. Everytime the user hits a button it overlays another image. The issue is that once I have five or ten layers of this particular XML drawable then the UI wants to just lock up. Here is the drawable:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <rotate
            android:fromDegrees="247.5"
            android:toDegrees="247.5"
            android:pivotX="50%"
            android:pivotY="50%">
            <shape android:shape="oval">
                <size
                    android:width="800dp"
                    android:height="800dp"/>
                <stroke
                    android:dashWidth="235.6dp"
                    android:dashGap="5000.01dp"
                    android:width="200dp"
                    android:color="#c2272d" />
            </shape>
        </rotate>
    </item>
    <item android:top="0dp" android:bottom="0dp" android:left="0dp" android:right="0dp">
        <rotate
            android:fromDegrees="247.5"
            android:toDegrees="247.5"
            android:pivotX="50%"
            android:pivotY="50%">
            <shape android:shape="oval">
                <size
                    android:width="800dp"
                    android:height="800dp"/>
                <stroke
                    android:dashWidth="290.6dp"
                    android:dashGap="5000.01dp"
                    android:width="60dp"
                    android:color="#767171" />
            </shape>
        </rotate>
    </item>
</layer-list>

And here is the code I am using to display and update:

public class ShotMagFragment extends Fragment implements DataDelegate, View.OnClickListener{

    private ImageView mainImage;
    private ArrayList<Drawable> layers = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_shot_mag, container, false);
        View simulateBtn = view.findViewById(R.id.simulateBtn);
        simulateBtn.setOnClickListener(this);
        mainImage = (ImageView)view.findViewById(R.id.mainImage);
        return view;
    }


    public void displayNewImage() {
        Random r = new Random();
        int number = 1 + (20 - 20) * r.nextInt();
        Drawable d = getResources().getDrawable(getResources().getIdentifier("mag_shade" + number, "drawable", getContext().getPackageName()));
        layers.add(d);

        final LayerDrawable layerDrawable = new LayerDrawable(layers.toArray(new Drawable[layers.size()]));
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mainImage.setImageDrawable(layerDrawable);
            }
        });
    }
    @Override
    public void onClick(View v) {
        displayNewImage();
    }
}

But when I use a different XML drawable I don't get the same UI lock up problem:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="130dp" android:bottom="130dp" android:left="130dp" android:right="130dp">
        <rotate
            android:fromDegrees="247.5"
            android:toDegrees="247.5"
            android:pivotX="50%"
            android:pivotY="50%">
            <shape android:shape="oval">
                <size
                    android:width="540dp"
                    android:height="540dp"/>
                <stroke
                    android:dashWidth="212.0dp"
                    android:dashGap="5000.01dp"
                    android:width="4dp"
                    android:color="#fff" />
            </shape>
        </rotate>
    </item>
</layer-list>

EDIT I also get a weird error icon next to my reference to my drawable. When I click on it, it just takes me to the drawable. I have no idea if that is related to my problem, if it is a real error, or if it is just a notice.

error image

Chase Roberts
  • 9,082
  • 13
  • 73
  • 131

0 Answers0