且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android:如何在方向更改时保存自定义手指绘画视图

更新时间:2023-10-24 20:24:46

您需要确保您的视图为

You need to make sure your View is saveEnabled, try:

setSaveEnabled(true);

您可以从PaintView的构造函数中执行此操作.

You could do this from the constructor of your PaintView.

此外,您可能需要确保您的活动在方向改变时被销毁/重新创建.确保您不要具有:

Also, you may need to make sure that your activities are being destroyed/recreated on orientation change. Make sure you DO NOT have:

<activity name="?"
android:configChanges="keyboardHidden|orientation"
/>

使用此视图为该活动声明了该活动,否则该活动不会在方向更改时被破坏.另外,如果您在这些活动中覆盖了 onSaveInstanceState /* onRestoreInstanceState *,则需要调用相应的超类方法,即super.onSaveInstanceStateState()或super.onRestoreInstanceState()).

declared for the activity using this View or else the activity won't be destroyed on orientation change. Also, if you have overridden onSaveInstanceState/*onRestoreInstanceState* in those activities you need to call the respective superclass method, either super.onSaveInstanceState() or super.onRestoreInstanceState())

关于如何实际重新创建或保存图像以改变方向的操作,如何保存在某种可包裹列表中完成的每个事件的方式.将该列表保存在onSaveInstanceState中,然后将其还原到onRestoreInstanceState中.然后获取事件列表并遍历事件列表,调用相应的绘制函数. 您可能需要根据新的方向对X,Y值进行一些转换.

As far as how to actually recreate or save the image for orientation changes how about you save each event that is done in some kind of parcelable List. Save that list in onSaveInstanceState and restore it in onRestoreInstanceState. THen take the list of events and loop through it, calling the respective draw functions. You may need to do some translation of X,Y values based on the new orientation.

代码看起来像这样:

public class PaintView extends View {

    private static final String EXTRA_EVENT_LIST = "event_list";
private static final String EXTRA_STATE = "instance_state";
private ArrayList<MotionEvent> eventList = new ArrayList<MotionEvent>(100);

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        getParent().requestDisallowInterceptTouchEvent(true);           
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
            performTouchEvent(event);                
        }             
        return true;
    }

    private void performTouchEvent(MotionEvent event) {
        float x = event.getX();
            float y = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                break;
                }
                invalidate();
                eventList.add(MotionEvent.obtain(event));           
        }
    }


    @Override
    public Parcelable onSaveInstanceState() 
    {
        System.out.println("save instance");
        Bundle bundle = new Bundle();
        bundle.putParcelable(EXTRA_STATE, super.onSaveInstanceState());  
        bundle.putParcelableArrayList(EXTRA_EVENT_LIST, eventList);

    return bundle;
    }

    @Override
    public void onRestoreInstanceState(Parcelable state) 
    {
        if (state instanceof Bundle) 
        {
            Bundle bundle = (Bundle) state;
            super.onRestoreInstanceState(bundle.getParcelable(EXTRA_STATE));
            eventList = bundle.getParcelableArrayList(EXTRA_EVENT_LIST);
            if (eventList == null) {
               eventList = new ArrayList<MotionEvent>(100); 
            }
            for (MotionEvent event : eventList) {
               performTouchEvent(event);
            }               
            return;
        }
        super.onRestoreInstanceState(state);
    }
}