定制视图与触摸事件——挑战练习:设备旋转问题

本文介绍如何在Android应用中通过重写View的onSaveInstanceState和onRestoreInstanceState方法来解决设备旋转后绘制元素丢失的问题。文章提供了BoxDrawingView类的具体实现案例,展示了如何使用Bundle保存和恢复视图状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

此问题是《Android编程权威指南》的第29章的29.5小节挑战练习:设备旋转问题。

从书本提示中可看出,要解决设备旋转后,绘制的矩形框会消失,需要注意的几点:
1.重写View的以下方法,用来保存数据和恢复数据:

protected Parcelable onSaveInstanceState()
protected Parcelable onRestoreInstanceState(Parcelable state)

2.View视图要有ID,只有这样,上面两个方法才会被调用:

<?xml version="1.0" encoding="utf-8"?>
<com.example.administrator.boxdrawingview.BoxDrawingView
    xmlns:android="http://schemas.android.com/apk/res/android"
    **android:id="@+id/box_drawing_view"**
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

</com.example.administrator.boxdrawingview.BoxDrawingView>

3.因为实现Parcelable比较繁琐,用Bundle保存状态数据。

4.需要保存View父视图的状态,在Bundle中保存super.onSaveInstanceState()方法结果,然后调用 super.onRestoreInstanceState(state)把结果发送给超类。

整个实现,BoxDrawingView.java文件为:

public class BoxDrawingView extends View {
    private static final String TAG = "BoxDrawingView";

    private Box mCurrentBox;
    private Paint mBoxPaint;
    private Paint mBackgroundPaint;
    // 定制视图的List
    private List<Box> mBoxen = new ArrayList<>();
    // 保存状态数据,用Bundle
    private Bundle saveStateBundle;


//    1.使用View的以下方法,用来保存数据和恢复数据:
//    protected Parcelable onSaveInstanceState()
//    protected Parcelable onRestoreInstanceState(Parcelable state)
    @Override
    protected Parcelable onSaveInstanceState() {
        saveStateBundle = new Bundle();
        // 在Bundle中保存父视图状态,恢复状态时会将此发送给超类
        saveStateBundle.putParcelable("onSaveInstanceState", super.onSaveInstanceState());
        // 保存当前的状态数据,bundle保存List<Object>,强转Serializable
        saveStateBundle.putSerializable("mBoxen", (Serializable) mBoxen);
        return saveStateBundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        Bundle bundle = (Bundle) state;

        // 将父视图状态取出,发送给超类
        Parcelable restoreState = bundle.getParcelable("onSaveInstanceState");
        super.onRestoreInstanceState(restoreState);

        // 恢复状态数据
        mBoxen = (List<Box>) bundle.getSerializable("mBoxen");
    }

    public BoxDrawingView(Context context) {
        super(context);
    }

    public BoxDrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mBoxPaint = new Paint();
        mBoxPaint.setColor(0x22ff0000);

        mBackgroundPaint = new Paint();
        mBackgroundPaint.setColor(0xfff8efe0);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        PointF current = new PointF(event.getX(), event.getY());
        String action = "";

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                action = "ACTION_DOWN";
                mCurrentBox = new Box(current);
                mBoxen.add(mCurrentBox);
                break;
            case MotionEvent.ACTION_MOVE:
                action = "ACTION_MOVE";
                if (mCurrentBox != null) {
                    mCurrentBox.setCurrent(current);
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                action = "ACTION_UP";
                mCurrentBox = null;
                break;
            case MotionEvent.ACTION_CANCEL:
                action = "ACTION_CANCEL";
                mCurrentBox = null;
                break;
        }

        Log.i(TAG, action + "at x=" + current.x + ", y=" + current.y);

        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPaint(mBackgroundPaint);

        for (Box box : mBoxen) {
            float left = Math.min(box.getOrigin().x, box.getCurrent().x);
            float right = Math.max(box.getOrigin().x, box.getCurrent().x);
            float top = Math.min(box.getOrigin().y, box.getCurrent().y);
            float bottom = Math.max(box.getOrigin().y, box.getCurrent().y);

            canvas.drawRect(left, top, right, bottom, mBoxPaint);
        }
    }
}

这里写图片描述这里写图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值