更改一个对象的值会影响其他对象
Changing the value of one Object affects other
我遇到了一个奇怪的问题。
我有一个 class:
public class HeatMap {
public View view;
public RelativeLayout.LayoutParams orignalParam;
public RelativeLayout.LayoutParams centerParam;
public int originalPos[]=new int[2];
public boolean Position;
public float Positions[]=new float[2];
public int centerPos[]=new int[2];
public HeatMap(View view){
this.view=view;
this.Position=false;
orignalParam= (RelativeLayout.LayoutParams) view.getLayoutParams();
centerParam =
(RelativeLayout.LayoutParams) view.getLayoutParams();
}
在另一个 class 中,我正在做以下事情
static HeatMap staticView;
public static void TranslateViewAtCenter1(Context context, int pop){
HeatMap hMap=Constants.heatMapList.get(pop);
staticView=hMap;
// staticView.centerParam.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.CENTER_VERTICAL);
// staticView.view.setLayoutParams(hMap.centerParam);
View rootChild = hMap.view;
int originalPos[] = new int[2];
rootChild.getLocationOnScreen(originalPos);
int mainViewWidth=HomeScreenActivity.rl_heatmap_view.getMeasuredWidth();
int mainViewHeight=HomeScreenActivity.rl_heatmap_view.getMeasuredHeight();
RelativeLayout.LayoutParams lp= (RelativeLayout.LayoutParams)rootChild.getLayoutParams();
int left=lp.leftMargin;
int top=lp.topMargin;
Log.e("ORGPOs_center", hMap.originalPos[0] + " , " + hMap.originalPos[1] + " ," + hMap.view.getId());
Log.e("XXWidhtHeight",rootChild.getMeasuredWidth()+" , "+rootChild.getMeasuredHeight());
int xDest = mainViewWidth/2;
xDest -= (rootChild.getMeasuredWidth()/2);
int yDest = mainViewHeight/2 - (rootChild.getMeasuredHeight()/2)/*-statusBarOffset*/;
int xD= CommonMethods.pxToDp(xDest-left,context);
int yD= CommonMethods.pxToDp(yDest - top, context);
hMap.centerPos[0]=xD;
hMap.centerPos[1]=yD;
TranslateIntoCenter intoCenter=new TranslateIntoCenter();
intoCenter.setView(hMap);
TranslateAnimation anim = new TranslateAnimation( xD, 0 , yD, 0 );
anim.setAnimationListener(intoCenter);
anim.setDuration(500);
anim.setFillAfter(true);
hMap.view.startAnimation(anim);
}
问题出在这两行
staticView.centerParam.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.CENTER_VERTICAL);
staticView.view.setLayoutParams(hMap.centerParam);
如果我评论这些行,每件事对 hMap
来说都是完美的,但是在更改 staticView
属性时,hMap
也发生了变化。我已经检查了我没有使用 staticView
或在其他任何地方使用 staticView
做任何事情的整个代码。
谢谢。
通过添加行
staticView=hMap;
您正在获取对 hMap 对象的引用。您不是在创建新对象。这意味着 staticView 和 hMap 都指向同一个对象,如果您对其中任何一个进行更改,它们都会反映在 staticView 和 hMap 中。
您必须创建一个新的 HeatMap 对象
我遇到了一个奇怪的问题。
我有一个 class:
public class HeatMap {
public View view;
public RelativeLayout.LayoutParams orignalParam;
public RelativeLayout.LayoutParams centerParam;
public int originalPos[]=new int[2];
public boolean Position;
public float Positions[]=new float[2];
public int centerPos[]=new int[2];
public HeatMap(View view){
this.view=view;
this.Position=false;
orignalParam= (RelativeLayout.LayoutParams) view.getLayoutParams();
centerParam =
(RelativeLayout.LayoutParams) view.getLayoutParams();
}
在另一个 class 中,我正在做以下事情
static HeatMap staticView;
public static void TranslateViewAtCenter1(Context context, int pop){
HeatMap hMap=Constants.heatMapList.get(pop);
staticView=hMap;
// staticView.centerParam.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.CENTER_VERTICAL);
// staticView.view.setLayoutParams(hMap.centerParam);
View rootChild = hMap.view;
int originalPos[] = new int[2];
rootChild.getLocationOnScreen(originalPos);
int mainViewWidth=HomeScreenActivity.rl_heatmap_view.getMeasuredWidth();
int mainViewHeight=HomeScreenActivity.rl_heatmap_view.getMeasuredHeight();
RelativeLayout.LayoutParams lp= (RelativeLayout.LayoutParams)rootChild.getLayoutParams();
int left=lp.leftMargin;
int top=lp.topMargin;
Log.e("ORGPOs_center", hMap.originalPos[0] + " , " + hMap.originalPos[1] + " ," + hMap.view.getId());
Log.e("XXWidhtHeight",rootChild.getMeasuredWidth()+" , "+rootChild.getMeasuredHeight());
int xDest = mainViewWidth/2;
xDest -= (rootChild.getMeasuredWidth()/2);
int yDest = mainViewHeight/2 - (rootChild.getMeasuredHeight()/2)/*-statusBarOffset*/;
int xD= CommonMethods.pxToDp(xDest-left,context);
int yD= CommonMethods.pxToDp(yDest - top, context);
hMap.centerPos[0]=xD;
hMap.centerPos[1]=yD;
TranslateIntoCenter intoCenter=new TranslateIntoCenter();
intoCenter.setView(hMap);
TranslateAnimation anim = new TranslateAnimation( xD, 0 , yD, 0 );
anim.setAnimationListener(intoCenter);
anim.setDuration(500);
anim.setFillAfter(true);
hMap.view.startAnimation(anim);
}
问题出在这两行
staticView.centerParam.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.CENTER_VERTICAL);
staticView.view.setLayoutParams(hMap.centerParam);
如果我评论这些行,每件事对 hMap
来说都是完美的,但是在更改 staticView
属性时,hMap
也发生了变化。我已经检查了我没有使用 staticView
或在其他任何地方使用 staticView
做任何事情的整个代码。
谢谢。
通过添加行
staticView=hMap;
您正在获取对 hMap 对象的引用。您不是在创建新对象。这意味着 staticView 和 hMap 都指向同一个对象,如果您对其中任何一个进行更改,它们都会反映在 staticView 和 hMap 中。
您必须创建一个新的 HeatMap 对象