0

嗨,在一个班级中使用 myview。我画了一些线条,之后如果单击按钮,然后我会去其他班级,然后我又回到上一堂课,但这里我画的线条不存在,但在我的情况下,我当我回到以前的活动时需要显示那些画线......你能推荐我吗..thnkyou

主要活动

public class MainActivity extends Activity {
MyView myview;
RelativeLayout rl;
Button b1;
public boolean action = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b1=(Button)findViewById(R.id.button1);
    b1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i=new Intent(MainActivity.this,Activity2.class);
            startActivity(i);
        }
    });
    rl = (RelativeLayout) findViewById(R.id.layout);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    // mPaint.setColor(0xFFFF0000);
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(6);
    myview = new MyView(this);
    myview.setId(004);
    RelativeLayout.LayoutParams lp6 = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    myview.setLayoutParams(lp6);
    // myview.setBackgroundResource(R.drawable.booklet);
    lp6.setMargins(10, 100, 10, 10);
    // lp6.setMargins(25, 50, 8,70);
    rl.addView(myview, lp6);

}

private Paint mPaint;
private Bitmap mBitmap;

public void colorChanged(int color) {
    mPaint.setColor(color);
}

public class MyView extends View {

    private Canvas mCanvas;
    private Path mPath;
    private Paint mBitmapPaint;

    public MyView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        // clearAllResources();

    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (action) {
            invalidate();
        }
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
       canvas.drawPath(mPath, mPaint);

    }

    private float mX, mY;
    private final float TOUCH_TOLERANCE = 2;

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    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) {
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;

        }
    }

    private void touch_up() {
        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) {
        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;
    }

}
    }

活动2

public class Activity2 extends Activity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.acticity2);
    b1=(Button)findViewById(R.id.button1);
    b1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i=new Intent(Activity2.this,MainActivity.class);
            startActivityForResult(i, 0);
        }
    });
}
}
4

2 回答 2

0

创建一个具有以下属性的线类:float startX、float startY、float stopX、float stopY、Paint paint

然后创建 List 以包含您的行实例。每次创建线时,创建一个新的线实例,设置其数据并将其添加到列表中。在 onDraw 中,遍历列表并使用您存储的数据绘制每一行。

于 2013-06-17T14:31:15.497 回答
0

您的代码的问题是,每当单击按钮时,您都会启动一个新的活动实例。尝试从 Activity2 调用 finish() 而不是 startActivity() 。或者你可以使用 setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 将 MainActivity 的前一个实例带到前面。

public void onClick(View v) {

    Intent intent = new Intent(getApplicationContext(),MainActivity.class);
              //finish(); OR
              intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
              startActivity(intent);      
    }
于 2013-06-17T14:54:45.833 回答