有关java中的 浅复制和深复制
,这里不详细讨论。相关知识请参考以下帖子:
浅复制和深复制http://blog.csdn.net/yang_hui1986527/article/details/7029777
浅析Java中的深拷贝与浅拷贝http://blog.csdn.net/yang_hui1986527/article/details/7012428
android中的 浅复制和深复制,具体可能于java中的有一点不同。下面,我们来探讨下在这个问题。
主要思路:
1、创建TextBox,然后根据TextBox复制出一个对象TextBox2(点击屏幕,进行复制);
2、改变TextBox2的位置,方便观察比较差异。
3、改变TextBox2内部文本的位置。如果是浅复制,那么TextBox2内部文本改变位置的同时,TextBox内部文本的位置会随着改变。否则,如果是深度复制,TextBox内部文本的位置不会改变。
效果如下:
注:请注意文本“test”在文本框内的位置。
浅复制:
深复制:
源程序:
(点击下载:http://download.csdn.net/detail/yang_hui1986527/3874905)
CloneActivity.java
package com.clone; import android.app.Activity; import android.os.Bundle; public class CloneActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); CloneView mCloneView = new CloneView(this); setContentView(mCloneView); } }
CloneView.java
package com.clone; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PointF; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class CloneView extends View { private Document mDocument = null; public Paint mPaint= null; public TextBox mTextBox = null; public TextBox mTextBox2 = null; public CloneView(Context context) { super(context); mDocument = new Document(); mPaint = new Paint(); initData(); } public CloneView(Context context, AttributeSet attrs) { super(context, attrs); mDocument = new Document(); mPaint = new Paint(); initData(); } public CloneView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mDocument = new Document(); } public void initData() { PointF textpos = new PointF(30,30); String text = new String("test"); mTextBox = new TextBox(50, 50, 100, 100, textpos, text); mDocument.add(mTextBox); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.WHITE); mDocument.draw(canvas, mPaint); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { try { mTextBox2 = (TextBox)mTextBox.clone(); mTextBox2.moveBound(); mTextBox2.changeData(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } if(mTextBox2 != null) mDocument.add(mTextBox2); } invalidate(); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; default: break; } return super.onTouchEvent(event); } }
Document.java
package com.clone; import java.util.Vector; import android.graphics.Canvas; import android.graphics.Paint; public class Document { private Vector<Object> objs = null; public Document() { objs = new Vector<Object>(); } public void add(Object obj){ objs.add(obj); } void draw(Canvas canvas, Paint paint) { canvas.save(); for (int i = 0; i < objs.size(); i++) { Object obj = objs.get(i); obj.draw(canvas, paint); } canvas.restore(); } }
Object.java
package com.clone; import android.graphics.Canvas; import android.graphics.Paint; public abstract class Object implements Cloneable{ protected float posx = 0; protected float posy =0; protected float width = 0; protected float height = 0; public Object() { posx = 0; posy =0; width = 0; height = 0; } public Object(float posx,float posy,float width,float height){ this.posx = posx; this.posy = posy; this.width = width; this.height = height; } public void moveBound() { this.posx += 100; this.posy += 100; } abstract void draw(Canvas canvas,Paint paint); abstract void changeData(); public Object clone() throws CloneNotSupportedException { return (Object) super.clone(); } }
TextBox.java
package com.clone; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.PointF; public class TextBox extends Object implements Cloneable{ private PointF mtextpos = null; private String mtextString = null; public TextBox() { posx = 0; posy =0; width = 0; height = 0; mtextpos = new PointF(); mtextString = new String(); } public TextBox(float posx, float posy, float width, float height,PointF textpos,String text) { this.posx = posx; this.posy = posy; this.width = width; this.height = height; mtextpos = new PointF(textpos.x,textpos.y); mtextString = new String(text); } @Override void draw(Canvas canvas, Paint paint) { canvas.save(); paint.setColor(Color.BLUE); paint.setStyle(Style.STROKE); canvas.drawRect(posx, posy, posx+width, posy+height, paint); canvas.translate(posx, posy); paint.setColor(Color.BLACK); paint.setTextSize(20); canvas.drawText(mtextString, mtextpos.x, mtextpos.y, paint); canvas.restore(); } // // //浅克隆 // public TextBox clone() throws CloneNotSupportedException { // return (TextBox) super.clone(); // } //深克隆 public TextBox clone() throws CloneNotSupportedException { TextBox o = (TextBox) super.clone(); //对于string,int等基本类型,用上面一句就可以 //对于复杂类型,例如PointF等还需要手动实现拷贝 if(mtextpos != null) o.mtextpos = new PointF(mtextpos.x,mtextpos.y); return o; } @Override void changeData() { if(mtextpos != null) mtextpos.offset(20, 50); } }
时间: 2024-11-03 03:53:45