我是 android 和 java 的新手。这可能很简单,但我被卡住了......我正在创建一个应用程序,通过单击“添加”按钮在画布上的某个默认位置添加一个正方形。然后,您可以移动正方形并调整其大小。完成移动/重塑后,我的目的是让方块保持其新的大小/位置,并且让用户能够单击添加按钮,新方块将出现在原始默认位置。然后您可以再次移动/重塑第二个方块。并最终添加任意数量的方块(每个都是独一无二的)。
我已经设法添加了第一个方块,并且我还能够移动/重塑。我还创建了一个数组列表来存储方块。我的问题是,一旦我添加了第二个正方形,第一个正方形的形状和位置就会重置为默认位置和大小。如果我移动第二个方块,第一个也会移动。如此有效,就好像我最终得到了一堆同时移动和重塑的正方形。
问题是,在将正方形添加到数组之前,如何将位置/大小的变量信息转换为常量? 我的主要活动有以下内容
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(addTimer != null) {
addTimer.cancel();
}
if (addclickcount>0) {
addBoxtoList();
}
addBox();
addclickcount++;
}
});
public void addBox(){ mCustomView.drawRoundRect();}
public void addBoxtoList(){mCustomView.addBoxtoList();}
然后在自定义视图中
public class CustomView extends View {
private ArrayList<Box> boxesArray = new ArrayList<>();
private RectF mRectSquare;
private Paint mPaintSquare;
private static RectF aRectSquare;
private static Paint aPaintSquare;
private int mSquareLeft, mSquareTop, dx, dy;
int colorSelected;
public CustomView(Context context) {
super(context);
init(null);
}
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(attrs);
}
private void init (@Nullable AttributeSet set){
mRectSquare = new RectF();
mPaintSquare = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintSquare.setColor(Color.BLUE);
mPaintSquare.setStyle(Paint.Style.FILL_AND_STROKE);
mPaintSquare.setAlpha(85);
}
public void drawRoundRect(){
if (General.addBoxCheck == true) {
dx = 200;
dy = 200;
mSquareLeft = scrwd - dx - 50;
mSquareTop = 50;
General.addBoxCheck = false;
}
mRectSquare.left = mSquareLeft;
mRectSquare.top = mSquareTop;
mRectSquare.right = mRectSquare.left + dx;
mRectSquare.bottom = mRectSquare.top + dy;
postInvalidate();
}
public void addBoxtoList(){
boxid = boxesArray.size() + 1;
aRectSquare = mRectSquare;
aPaintSquare = mPaintSquare;
Box boxa = new Box(boxid, aRectSquare, aPaintSquare);
boxesArray.add(boxa);
//postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawRoundRect(mRectSquare, 15, 15, mPaintSquare);
if(boxesArray != null) {
RectF rectF = new RectF();
Paint paint = new Paint();
for (int i = 0; i < boxesArray.size(); i++) {
paint = boxesArray.get(i).getPaint();
rectF = boxesArray.get(i).getRect();
canvas.drawRoundRect(rectF, 15, 15, paint);
}
}
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean value = super.onTouchEvent(event);
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:{
return true;
}
case MotionEvent.ACTION_MOVE:{
int x = (int) event.getX();
int y = (int) event.getY();
mSquareLeft = x;
mSquareTop = y;
postInvalidate();
return value;
}
}
return value;
}
谢谢
答案 0 :(得分:-1)
问题似乎很模糊,如果值发生变化,则意味着您更改了值。如果你不改变数组的值,那么它本质上应该是一个“常量”。我的假设是您正在覆盖数组,因此值正在发生变化。查看您的代码会有所帮助。
检查存储的正方形的索引并确认您没有覆盖任何以前的索引。