如何在android中保存Paints

时间:2013-06-17 13:47:20

标签: android android-emulator

你好在一个class.am绘制一些行使用myview,之后如果我点击按钮然后去其他类,然后我再回到上一课,但在这里我画的线不存在,但在我的情况,当我回到以前的活动时,我需要显示画出的线条...你能不能建议我......你好吗

MainActivity

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

2 个答案:

答案 0 :(得分:0)

创建具有以下属性的线类 float startX,float startY,float stopX,float stopY,Paint paint

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

答案 1 :(得分:0)

代码问题在于,只要单击按钮,就会启动新的活动实例。 而不是startActivity(),尝试从Activity2调用finish()。 或者你可以使用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);      
    }