J
J
jusalex2014-11-07 22:36:36
Android
jusalex, 2014-11-07 22:36:36

Where does the Custom View disappear after turning off/on the screen?

Made a simple DressView class that extends View. In the onDraw method, I drew two rectangles and cut one from the other:

paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);

        path.moveTo(0, 0);
        path.lineTo(metrics.widthPixels, 0);
        path.lineTo(metrics.widthPixels, metrics.heightPixels);
        path.lineTo(0, metrics.heightPixels);
        path.close();

        path.moveTo(100, 100);
        path.lineTo(metrics.widthPixels - 100, 100);
        path.lineTo(metrics.widthPixels - 100, metrics.heightPixels - 200);
        path.lineTo(100, metrics.heightPixels - 200);
        path.close();

        path.setFillType(Path.FillType.EVEN_ODD);
        canvas.drawPath(path, paint);

Placed the created element on the layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:background="@color/white"
                android:layout_height="match_parent">
    <ImageView android:layout_width="match_parent"
               android:src="@drawable/foto"
               android:id="@+id/foto"
               android:layout_height="match_parent"/>
    <ImageView android:layout_width="0dp"
               android:id="@+id/loading"
               android:src="@color/loading"
               android:layout_height="match_parent"/>
    <ru.memores.dressdrawer.DressView
        android:layout_height="match_parent"
        android:id="@+id/dress_view"
        android:layout_width="match_parent"/>

</RelativeLayout>

Everything works as intended, however, after turning the screen on and off, only the ImageView is visible. Partially solved the problem by calling DressView.invalidate() from Activity.onResume(), so after the second screen on/off, everything is drawn. But the first shutdown iteration erases the rectangles. How can you solve the problem and somehow cache the state and restore it if necessary, and is it possible to understand the nature of this problem at all?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jusalex, 2014-11-18
@jusalex

Yes, after overriding the onMeasure method and setting the required dimensions for the View, the problem is gone!
Thank you!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question