完成活动不起作用

时间:2018-01-28 18:48:42

标签: android android-activity android-view activity-finish

我正在尝试实现绘图工具。 因此,我有一个活动AddCanvasActivity和一个自定义的视图类DrawingView 应该有一个按钮来确认绘图过程,然后我想回到我的第一个活动。

我无法弄清楚它为什么不起作用。 这是我发现我的代码正在做什么

D/ADDCANVAS- DrawingView: onTouchEvent
D/TouchEvent true: 442.4297
D/TouchEvent true: 575.8801
D/DrawingView: TouchUp
D/State: pencil

所以这里是我画完一条线的地方,之后我点击了相应的按钮确认了画布

D/iscontent Pathsarray: true 
D/getPaint pathinfo: -16777216
D/Add Path first content: -16777216
D/DrawingView: OnDraw
D/iscontent Pathsarray: true
D/AddCanvasActivityíy: Content true pathsarray
D/Finished: true
D/DrawingView: OnDraw
D/EGL_emulation: eglMakeCurrent: 0xa303da20: ver 3 0 (tinfo 0xa4f9bd40)
D/EGL_emulation: eglMakeCurrent: 0xa303da20: ver 3 0 (tinfo 0xa4f9bd40)
D/EGL_emulation: eglMakeCurrent: 0xa303da20: ver 3 0 (tinfo 0xa4f9bd40)
D/EGL_emulation: eglMakeCurrent: 0xa303da20: ver 3 0 (tinfo 0xa4f9bd40)
D/EGL_emulation: eglMakeCurrent: 0xa303da20: ver 3 0 (tinfo 0xa4f9bd40)
D/OpenGLRenderer: endAllActiveAnimators on 0x958f3e00 (RippleDrawable) with handle 0x959bf910
D/EGL_emulation: eglMakeCurrent: 0xa303da20: ver 3 0 (tinfo 0xa4f9bd40)

然后我第二次确认了,但我的数据已经丢失了......

D/iscontent Pathsarray: false
D/AddCanvasActivityíy: Content false pathsarray
D/pa: isnull
D/onActivityResult: added canvas to canvases
D/EGL_emulation: eglMakeCurrent: 0xa303da20: ver 3 0 (tinfo 0xa4f9bd40)
D/EGL_emulation: eglMakeCurrent: 0xa303da20: ver 3 0 (tinfo 0xa4f9bd40)
D/EGL_emulation: eglMakeCurrent: 0xa303da20: ver 3 0 (tinfo 0xa4f9bd40)
D/EGL_emulation: eglMakeCurrent: 0xa303da20: ver 3 0 (tinfo 0xa4f9bd40)
D/EGL_emulation: eglMakeCurrent: 0xa303da20: ver 3 0 (tinfo 0xa4f9bd40)
D/OpenGLRenderer: endAllActiveAnimators on 0x95810580 (RippleDrawable) with handle 0xa2edb8c0
D/EGL_emulation: eglMakeCurrent: 0xa303da20: ver 3 0 (tinfo 0xa4f9bd40)

应用程序之后仍在运行,但我不知道如何处理这些OpenGL信息。

我班级的代码是

public class AddCanvasActivity extends AppCompatActivity{

    DrawingView dv ;
    private String mDrawOption;
    private Paint mPaint;
    private Boolean mFinished;
    private PathsArray mPathsArray;
    private PathInformation mPathInformation;

    @Override
    protected void onCreate(Bundle saveInstanceState){
        if(getIntent().getBooleanExtra("Finished",false)) {
            finish();
        }

        super.onCreate(saveInstanceState);

        dv = new DrawingView(this);

        mDrawOption = new String("pencil");
        mPaint = new Paint();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(12);
        mPaint.setTextSize(50);
        mDrawOption = new String();
        mDrawOption = "pencil";
        mFinished=false;
        mPathsArray = new PathsArray();

        setContentView(R.layout.view_add_canvas);

        ActionBar actionbar = getSupportActionBar();
        actionbar.setDisplayHomeAsUpEnabled(true);
        actionbar.setDisplayShowTitleEnabled(false);

        Log.d("Activit onCreate", "AddCanvasActivity");

    }


    protected void onStart(Bundle SavedInstances){
        super.onStart();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_add_canvas, menu);

        MenuItem item = menu.findItem(R.id.menuColor);
        Spinner spinner = (Spinner) menu.findItem(R.id.menuColor).getActionView();

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.array_choose_color, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        AdapterView.OnItemSelectedListener ListenerColorChoose = new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                Log.d("Onitemse pos:", Integer.toString(position));
                Log.d("Onitemse id:", Long.toString(id));
                for(int c = 0; c<= mPathsArray.getLength(); c++){
                    Log.d("colorchange id before ", Integer.toString(c))   ;
                    Log.d("colorchange color befor", Integer.toString(mPathsArray.getPathInfo(c).getPaint().getColor()))  ;
                }

                switch (position) {
                    case 0:
                        //clear before?
                        mPaint.setColor(Color.BLACK);
                        break;
                    case 1:
                        mPaint.setColor(Color.BLUE);
                        break;
                    case 2:
                        mPaint.setColor(Color.GREEN);
                        break;
                    case 3:
                        mPaint.setColor(Color.YELLOW);
                        break;
                    case 4:
                        mPaint.setColor(Color.RED);
                        break;
                    case 5:
                        mPaint.setColor(Color.WHITE);
                        break;

                }

                for(int c = 0; c<= mPathsArray.getLength(); c++){
                    Log.d("colorchange id after ", Integer.toString(c))   ;
                    Log.d("colorchange color after", Integer.toString(mPathsArray.getPathInfo(c).getPaint().getColor()))  ;
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                Log.d("Onitemse pos:", "nothing");

            }
        };

        spinner.setOnItemSelectedListener(ListenerColorChoose);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
            case R.id.action_scrollback:
                dv.undoLastPath();
                break;
            case R.id.action_confirm:
                Intent intent = new Intent(this, ShowAllActivity.class);
                if(mPathsArray.isContent()){
                    Log.d("AddCanvasActivityíy", "Content true pathsarray");
                    intent.putExtra("pathsArray", mPathsArray);
                    intent.putExtra("Finished",true);
                    setResult(RESULT_OK,intent);
                    mFinished = true;
                    Log.d("Finished", "true");
                    finish();
                    return true;
                }
                else{
                    Log.d("AddCanvasActivityíy", "Content false pathsarray");
                    intent.putExtra("pathsArray", mPathsArray);
                    setResult(RESULT_CANCELED, intent);
                    mFinished = true;
                    finish();
                    return true;
                }
            case R.id.circle:
                mDrawOption = "circle";
                Log.d("SUCCESS", "chose Circle");
                break;
            case R.id.pencil:
                mDrawOption="pencil";
                Log.d("SUCCESS", "chose Pencil");
                break;
            case R.id.ellipse:
                mDrawOption = "ellipse";
                Log.d("SUCCESS", "chose Ellipse");
                break;
            case R.id.line:
                mDrawOption = "line";
                Log.d("SUCCESS", "chose Linie");
                break;
            case R.id.rectangle:
                mDrawOption = "rectangle";
                Log.d("SUCCESS", "chose Rectangle");
                break;
            case R.id.text:
                mDrawOption = "text";
                Log.d("SUCCESS", "chose Text");
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public Paint getPaint(){
        return this.mPaint;
    }

    public String getDrawOption(){
        return this.mDrawOption;
    }

    public PathsArray getPathsArray(){
        return this.mPathsArray;
    }

    public Boolean getFinished(){
        return this.mFinished;
    }

    public boolean undoLast(){
        return mPathsArray.undoLast();
    }

    public void addPath(PathInformation pi){
        mPathsArray.addPath(pi);
    }

}

和DrawingView

public class DrawingView extends View {

    public int width;
    public int height;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    private Paint mBitmapPaint;
    Context context;
    private Paint circlePaint;
    private Path circlePath;
    private Path mPreviewPath;
    private ShapeDrawable mDrawable;
    private float mStartX;
    private float mStartY;
    private float mDistanceX;
    private float mDistanceY;
    private float mDistance;
    private float radiusPreview;
    private Paint clearPaint;
    private AddCanvasActivity mActivity;

    //constructors
    public DrawingView(Context c) {
            super(c);
            Log.d("DrawingView", "OnCreate1");
            this.setUp(c);

    }

    public DrawingView(Context c, AttributeSet attributeSet) {
            super(c, attributeSet);
            Log.d("DrawingView", "OnCreate2");
            this.setUp(c);

    }


    public DrawingView(Context c, AttributeSet attributeSet, int defStyle) {
        super(c,attributeSet,defStyle);
        Log.d("ADDCANVAS - DrawingView", "OnCreate3");
        this.setUp(c);
    }

    public void setUp(Context c){
        context = c;
        mPath = new Path();
        mPreviewPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        clearPaint = new Paint();
        circlePaint = new Paint();
        circlePath = new Path();
        circlePaint.setAntiAlias(true);
        circlePaint.setColor(Color.BLUE);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStrokeJoin(Paint.Join.MITER);
        circlePaint.setStrokeWidth(4f);
        clearPaint.setColor(Color.WHITE);
        mActivity = (AddCanvasActivity) c;

    }




    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        Log.d("DrawingView", "OnSizeChanged");
        super.onSizeChanged(w, h, oldw, oldh);

        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);

    }

    @Override
    protected void onDraw(Canvas canvas) {
            if(!mActivity.getFinished()) {
                Log.d("DrawingView", "OnDraw");
                super.onDraw(canvas);
                mActivity.getDrawOption();
                canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
                if (mActivity.getDrawOption() == "pencil" || mActivity.getDrawOption() == "line")
                    canvas.drawPath(mPath, mActivity.getPaint());
                canvas.drawPath(circlePath, circlePaint);
                canvas.drawPath(mPreviewPath, mActivity.getPaint());
            }
    }

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

    private void touch_start(float x, float y) {
        Log.d("DrawingView", "TouchStart");

        mStartX = x;
        mStartY = y;

        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    private void touch_move(float x, float y) {
        Log.d("DrawingView", "TouchMove");


        float dx = abs(x - mX);
        float dy = abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mDistanceX = abs(x - mStartX);
            mDistanceY = abs(y - mStartY);
            mDistance = (float) sqrt((mDistanceX * mDistanceX) + (mDistanceY * mDistanceY));
            mPreviewPath.reset();

            switch (mActivity.getDrawOption()) {
                case "circle":
                    mPreviewPath.addCircle(mStartX, mStartY, mDistance, Path.Direction.CW);
                    break;
                case "pencil":
                    mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
                    break;
                case "ellipse":
                    mPreviewPath.addOval(mStartX, mStartY, x, y, Path.Direction.CW);
                    break;
                case "line":
                    mPreviewPath.moveTo(mStartX, mStartY);
                    mPreviewPath.quadTo(mStartX, mStartY, x, y);
                    break;
                case "rectangle":
                    if (mStartX > x) {
                        if (mStartY > y) {
                            mPreviewPath.addRect(x, y, mStartX, mStartY, Path.Direction.CW);
                        } else mPreviewPath.addRect(x, mStartY, mStartX, y, Path.Direction.CW);
                    }
                    if (mStartY > y) {
                        mPreviewPath.addRect(mStartX, y, x, mStartY, Path.Direction.CW);
                    }
                    mPreviewPath.addRect(mStartX, mStartY, x, y, Path.Direction.CW);
                    break;
                case "text":
                    //to be done
                    break;
                default:
                    break;

            }
            mX = x;
            mY = y;


            circlePath.reset();
            circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
        }
    }


    private void touch_up(float x, float y) {
        Log.d("DrawingView", "TouchUp");
        mPath.lineTo(mX, mY);
        mDistanceX = abs(x - mStartX);
        mDistanceY = abs(y - mStartY);
        mDistance = (float) sqrt((mDistanceX * mDistanceX) + (mDistanceY * mDistanceY));
        Path pa = new Path();

        Log.d("State", mActivity.getDrawOption());

        Paint mPaint = mActivity.getPaint();

        switch (mActivity.getDrawOption()) {
            case "circle":
                mCanvas.drawCircle(mStartX, mStartY, mDistance, mPaint);
                pa.addCircle(mStartX,mStartY,mDistance,Path.Direction.CW);
                Log.d("DrawOption", "Circle, Touch up");
                Log.d("DrawOption: Dist", Float.toString(mDistance));
                Log.d("DrawOption: DistX", Float.toString(mDistanceX));
                Log.d("DrawOption: DistY", Float.toString(mDistanceY));
                Log.d("DrawOption: mX", Float.toString(mX));
                Log.d("DrawOption: mX", Float.toString(mY));
                break;
            case "rectangle":
                mCanvas.drawRect(mStartX, mStartY, x, y, mPaint);
                if (mStartX > x) {
                    if (mStartY > y) {
                        pa.addRect(x, y, mStartX, mStartY, Path.Direction.CW);
                    } else pa.addRect(x, mStartY, mStartX, y, Path.Direction.CW);
                }
                if (mStartY > y) {
                    pa.addRect(mStartX, y, x, mStartY, Path.Direction.CW);
                }
                pa.addRect(mStartX, mStartY, x, y, Path.Direction.CW);
                break;
            case "ellipse":
                mCanvas.drawOval(mStartX, mStartY, x, y, mPaint);
                pa.addOval(mStartX,mStartY,x,y,Path.Direction.CW);
                break;
            case "line":
                mCanvas.drawLine(mStartX, mStartY, x, y, mPaint);
                break;

            case "text":
                mCanvas.drawText("Text", mX, mY, mPaint);
                break;

            case "pencil":
                mCanvas.drawPath(mPath, mPaint);
                pa.addPath(mPath);
                break;
            default:
                Log.d("DrawOption: default", mActivity.getDrawOption());

        }
        PathInformation pi = new PathInformation(pa, true, mPaint);
        Log.d("DV add path b color", Integer.toString(mPaint.getColor()));
        mActivity.addPath(pi);
        Log.d("DV is content ", Boolean.toString(mActivity.getPathsArray().isContent()));
        Log.d("Add Path first content", Integer.toString(mActivity.getPathsArray().getPathInfo(0).getPaint().getColor()));
        circlePath.reset();
        mPreviewPath.reset();
        // 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) {
        if(!mActivity.getFinished()) {

            Log.d("ADDCANVAS- DrawingView", "onTouchEvent");

            Log.d("TouchEvent true", Float.toString(event.getX()));
            Log.d("TouchEvent true", Float.toString(event.getY()));

            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(x, y);
                    invalidate();
                    break;
            }
            return true;
        }
        else {
            Log.d("TouchEvent false", Float.toString(event.getX()));
            Log.d("TouchEvent false", Float.toString(event.getY()));
            return false;
        }
    }


    public void undoLastPath() {
        boolean b = mActivity.undoLast();
        if(b) {
            mCanvas.drawRect(0, 0, mCanvas.getWidth(), mCanvas.getHeight(), clearPaint);
            invalidate();
            for (int i = 0; i <= mActivity.getPathsArray().getLength(); i++) {
                if (mActivity.getPathsArray().getPathInfo(i).getValid() == true) {
                    mCanvas.drawPath(mActivity.getPathsArray().getPathInfo(i).getPath(), mActivity.getPathsArray().getPathInfo(i).getPaint());
                    Log.d("Undolastpath paint", Integer.toString(mActivity.getPathsArray().getPathInfo(i).getPaint().getColor()));
                    Log.d("Undolastpath id",Integer.toString(i));
                }
            }
            invalidate();
        }
    }
}

更新:添加ShowAllActivity

public class ShowAllActivity extends AppCompatActivity implements OnTouchListener{

    private ArrayList<PathsArray> mCanvases;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_show_all);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floatingActionButtonShowAll);

        mCanvases = new ArrayList<PathsArray>();
        fab.setOnTouchListener(this);

        GridView gridview = (GridView) findViewById(R.id.gridview_canvas);

        gridview.setAdapter(new ImageAdapter(this, mCanvases));

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Toast.makeText(ShowAllActivity.this, "" + position,
                        Toast.LENGTH_SHORT).show();
            }
        });

        Log.d("Activity onCreate", "ShowAllActivity");

    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {

                    PathsArray pa = (PathsArray) data.getParcelableExtra("pathArray");
                    if(pa == null) Log.d("pa", "isnull");
                    mCanvases.add(pa);
                    //Log.d("onactivityresult", Integer.toString(mCanvases.get(0).getPathInfo(0).getPaint().getColor()));
                    Log.d("onActivityResult", "added canvas to canvases");

            }
        }
    }

    public ArrayList<PathsArray> getCanvases(){return mCanvases;}




    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        Intent intent = new Intent(this.getBaseContext(), AddCanvasActivity.class);
        intent.putExtra("message", "Message from First Screen");
        startActivityForResult(intent,1);
        intent.putExtra("Finished",false);
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

可能您试图通过Intent传递更多关于活页夹交易所允许的最大数据量,这就是您的Activity无法finish();的原因。 Intent可以传递 1MB 的数据。 mPathsArray的大小可能大于 1MB 。对于大文件,您有三个选项之一:

  • 将数据存储在(临时)文件中并传递文件URI。如果您想将大量数据传输到完全不同的应用程序,则此选项可能是您唯一的选择。
  • 将数据存储在Application实例中;
  • 创建一个包含您传递的数据的单例容器。

在Google的帮助下,我发现这篇文章可能对您有所帮助: it.bewares JSON