我在ANdroid中创建了一个关于undo-redo paint的示例项目。
单击每个按钮更改绘画颜色时,我会初始化一个新对象,例如:
dp = new DrawingPanel(MainActivity.this, Color.RED);
使用DrawingPanel dp和颜色进行设置。
但是所有的油漆都变成了新的颜色,尽管之前的油漆是其他颜色。
我该如何解决这个问题。
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#A52A2A"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/Undo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Undo" />
<Button
android:id="@+id/Redo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Redo" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btnRed"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RED" />
<Button
android:id="@+id/btnBlue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="BLUE" />
<Button
android:id="@+id/btnYELLOW"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="YELLOW" />
<Button
android:id="@+id/btnGreen"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="GREEN" />
</LinearLayout>
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CCC" >
</FrameLayout>
MainActivity.java
public class MainActivity extends Activity {
private ArrayList<Path> undonePaths = new ArrayList<Path>();
private ArrayList<Path> paths = new ArrayList<Path>();
FrameLayout frmLayout;
DrawingPanel dp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frmLayout = (FrameLayout) findViewById(R.id.frameLayout);
dp = new DrawingPanel(this, Color.BLACK);
frmLayout.addView(dp);
initChangeColor();
initUndoRedo();
}
private void initUndoRedo() {
((Button) findViewById(R.id.Undo))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (paths.size() > 0) {
undonePaths.add(paths.remove(paths.size() - 1));
dp.invalidate();
}
}
});
((Button) findViewById(R.id.Redo))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (undonePaths.size() > 0) {
paths.add(undonePaths.remove(undonePaths.size() - 1));
dp.invalidate();
}
}
});
}
private void initChangeColor() {
findViewById(R.id.btnRed).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dp = new DrawingPanel(MainActivity.this, Color.RED);
frmLayout.addView(dp);
}
});
findViewById(R.id.btnBlue).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dp = new DrawingPanel(MainActivity.this, Color.BLUE);
frmLayout.addView(dp);
}
});
findViewById(R.id.btnYELLOW).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dp = new DrawingPanel(MainActivity.this, Color.YELLOW);
frmLayout.addView(dp);
}
});
findViewById(R.id.btnGreen).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dp = new DrawingPanel(MainActivity.this, Color.GREEN);
frmLayout.addView(dp);
}
});
}
public class DrawingPanel extends View implements OnTouchListener {
private Canvas mCanvas;
private Path mPath;
private Paint mPaint, circlePaint, outercirclePaint;
private float xleft, xright, xtop, xbottom;
public DrawingPanel(Context context, int colorPaint) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);
this.setOnTouchListener(this);
circlePaint = new Paint();
mPaint = new Paint();
outercirclePaint = new Paint();
outercirclePaint.setAntiAlias(true);
circlePaint.setAntiAlias(true);
mPaint.setAntiAlias(true);
mPaint.setColor(colorPaint);
outercirclePaint.setColor(0x44FFF000);
circlePaint.setColor(0xAADD5522);
outercirclePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStyle(Paint.Style.FILL);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(20);
outercirclePaint.setStrokeWidth(6);
mCanvas = new Canvas();
mPath = new Path();
paths.add(mPath);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
for (Path p : paths) {
canvas.drawPath(p, mPaint);
}
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 0;
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);
mCanvas.drawPath(mPath, mPaint);
mPath = new Path();
paths.add(mPath);
}
@Override
public boolean onTouch(View arg0, 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;
}
}
}
答案 0 :(得分:1)
您还必须将颜色保存到撤消历史记录中。我想最好的方法是创建一个包含路径和颜色并使用它们的类。我想它可能看起来像这样(但我没有测试过,所以如果需要稍微调整的话就不要开枪了)。
class MyPath extends Path { public int color; }
这使得Path类的扩展也具有保存颜色的公共变量。然后用于存储您使用的历史记录:
private ArrayList<MyPath> undonePaths = new ArrayList<MyPath>();
private ArrayList<MyPath> paths = new ArrayList<MyPath>();
对于每个类,您只需在将其添加到ArrayList之前设置颜色。