其他分享
首页 > 其他分享> > Android:如何在方向更改时保存自定义手指绘画视图

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

作者:互联网

我正在尝试创建一个自定义视图,用户可以在其中绘制其签名.您可以在其中以编程方式将其放置到视图中并设置大小等.我之前没有做过此操作,因此我将其基于android api演示的fingerpaint类.现在的问题是,它会在方向改变时擦除对象,我不确定如何使它不这样做.

PaintView的代码

private Bitmap  mBitmap;
private Canvas  mCanvas;
private Path    mPath;
private Paint   mBitmapPaint;
private boolean mDrawPoint;

private int stateToSave;

private Paint mPaint;


public PaintView(Context context) {
    super(context);
    System.out.println("init");
    mPath = new Path();
    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    setupPaint();
}

private void setupPaint()
{
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(0xFF000000);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(4);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    System.out.println("w "+w+" h "+h+" oldw "+oldw+" oldh "+oldh);
    mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
}

@Override
protected void onDraw(Canvas canvas) {
    System.out.println("on draw");
    canvas.drawColor(0xFFAAAAAA);

    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

    canvas.drawPath(mPath, mPaint);
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
    mDrawPoint=true;
}
private void touch_move(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
        mDrawPoint=false;
        mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
        mX = x;
        mY = y;
    }
}
private void touch_up() {
    if(mDrawPoint == true) {
        mCanvas.drawPoint(mX, mY, mPaint);
    } else {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    getParent().requestDisallowInterceptTouchEvent(true);
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
    }
    return true;
}

作为一项迷你测试,我加入了

@Override
public Parcelable onSaveInstanceState() 
{
    System.out.println("save instance");
    stateToSave++;
    Bundle bundle = new Bundle();
    bundle.putParcelable("instanceState", super.onSaveInstanceState());  
    bundle.putInt("stateToSave", this.stateToSave);

    return bundle;
}

@Override
public void onRestoreInstanceState(Parcelable state) 
{
    System.out.println("on restore");
    if (state instanceof Bundle) 
    {
        System.out.println("on restore");
        Bundle bundle = (Bundle) state;
        this.stateToSave = bundle.getInt("stateToSave");
        super.onRestoreInstanceState(bundle.getParcelable("instanceState"));
        System.out.println(stateToSave);
        return;
    }
    super.onRestoreInstanceState(state);
}

但是onRestoreInstance()和onSaveInstance()永远不会在方向更改时被调用.这是我进行paintview活动的oncreate中的代码.

RelativeLayout inner= (RelativeLayout) findViewById(R.id.inner);

    int width=600;
    int height=360;
    float X= 100;
    float Y=60;

    LayoutParams wrapped = new LayoutParams(width,height);

    PaintView painting=new PaintView(this);

    painting.setX(X);
    painting.setY(Y);
    painting.setLayoutParams(wrapped);
    inner.addView(painting);

编辑:我想出了为什么saveinstancestate没有调用,我必须给View提供一个ID,然后它才能工作.

painting.setId(4);

现在,我需要弄清楚如何使其保存图形.

解决方法:

您需要确保您的视图为saveEnabled,请尝试:

setSaveEnabled(true);

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

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

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

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

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

代码看起来像这样:

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);
    }
}

标签:android-orientation,orientation,android-canvas,android
来源: https://codeday.me/bug/20191030/1966054.html