我有一個畫布,允許用戶繪製和保存。它還允許用戶更改顏色並在不同顏色的相同畫布上繪製。當保存新的圖像時,它保留舊的畫布
我的油漆類:
package com.test.testing;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Paint.Style;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.test.testing.*;
public class CustomView extends View {
Paint paint;
Path path;
float x = 0;
float y = 0;
private int cWhite = Color.WHITE;
private Button btnS, btnSa;
public CustomView(Context context) {
super(context);
paint = new Paint();
path= new Path();
paint.setAlpha(255);
paint.setColor(cWhite);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(20);
btnS = (Button)((FreeDraw) context).findViewById(R.id.shareButton);
btnSa = (Button)((FreeDraw) context).findViewById(R.id.saveButton);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path,paint);
canvas.drawCircle(x, y, 10, paint);
}
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
if (btnS.getVisibility() == View.VISIBLE && btnS != null) {
//if share button is displaying but I drew something else
//the share button should disappear and I should be presented
//with the save button
btnS.setVisibility(View.GONE);
btnSa.setVisibility(View.VISIBLE);
}
if (btnSa.getVisibility() == View.VISIBLE && btnS != null) {
//do nothing...
}
invalidate();
break;
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
path.lineTo(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
path.lineTo(event.getX(), event.getY());
invalidate();
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
return true;
}
}
在我的活動我所說的塗料類,並使用一個XML佈局是這樣的:
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(FreeDraw.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
而且在同一個活動,我有以下代碼手柄顏色變化:
public void colorHandle() {
// custom dialog
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.colorlayout);
dialog.setTitle("Choose a Drawing Color");
Button btnWH = (Button) dialog.findViewById(R.id.btnWhite);
Button btnBL = (Button) dialog.findViewById(R.id.btnBlack);
Button btnBLU = (Button) dialog.findViewById(R.id.btnBlue);
Button btnCY = (Button) dialog.findViewById(R.id.btnCyan);
Button btnDG = (Button) dialog.findViewById(R.id.btnDkGray);
Button btnGR = (Button) dialog.findViewById(R.id.btnGray);
Button btnGRE = (Button) dialog.findViewById(R.id.btnGreen);
Button btnLG = (Button) dialog.findViewById(R.id.btnLtGray);
Button btnMG = (Button) dialog.findViewById(R.id.btnMagenta);
Button btnRD = (Button) dialog.findViewById(R.id.btnRed);
Button btnYE = (Button) dialog.findViewById(R.id.btnYellow);
if (btnWH != null) {
btnWH.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(FreeDraw.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
view.paint.setColor(Color.WHITE);
dialog.dismiss();
}
});
}
if (btnBL != null) {
btnBL.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(FreeDraw.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
view.paint.setColor(Color.BLACK);
dialog.dismiss();
}
});
}
if (btnBLU != null) {
btnBLU.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(FreeDraw.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
view.paint.setColor(Color.BLUE);
dialog.dismiss();
}
});
}
if (btnCY != null) {
btnCY.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(FreeDraw.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
view.paint.setColor(Color.CYAN);
dialog.dismiss();
}
});
}
if (btnDG != null) {
btnDG.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(FreeDraw.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
view.paint.setColor(Color.DKGRAY);
dialog.dismiss();
}
});
}
if (btnGR != null) {
btnGR.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(FreeDraw.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
view.paint.setColor(Color.GRAY);
dialog.dismiss();
}
});
}
if (btnGRE != null) {
btnGRE.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(FreeDraw.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
view.paint.setColor(Color.GREEN);
dialog.dismiss();
}
});
}
if (btnLG != null) {
btnLG.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(FreeDraw.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
view.paint.setColor(Color.LTGRAY);
dialog.dismiss();
}
});
}
if (btnMG != null) {
btnMG.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(FreeDraw.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
view.paint.setColor(Color.MAGENTA);
dialog.dismiss();
}
});
}
if (btnRD != null) {
btnRD.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(FreeDraw.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
view.paint.setColor(Color.RED);
dialog.dismiss();
}
});
}
if (btnYE != null) {
btnYE.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(FreeDraw.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
view.paint.setColor(Color.YELLOW);
dialog.dismiss();
}
});
}
dialog.show();
}
我必須重複以下代碼爲每種顏色becau SE在我畫的東西,改變顏色和別的畫東西,以前所有的繪製路徑更改爲新的顏色,這個代碼修復它:
layout = (FrameLayout)findViewById(R.id.viewd);
//layout.removeAllViews();
view = new CustomView(FreeDraw.this);
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(view);
我保存在活動代碼的圖像部分是:
View.OnClickListener saveHandle = new View.OnClickListener() {
public void onClick(View v) {
new SaveImageTask().execute(null, null, null);
}
};
public class SaveImageTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) { //Running in background
View content = layout;
content.setDrawingCacheEnabled(true);
content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = content.getDrawingCache();
File folder = new File(Environment.getExternalStorageDirectory() + "/PB");
if (!folder.exists()) {
folder.mkdir();
}
file = new File(folder + "/pb_image_" + Math.random() + ".png");
FileOutputStream ostream;
try {
file.createNewFile();
ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.flush();
ostream.close();
isSaved = true;
} catch (Exception e) {
e.printStackTrace();
isSaved = false;
}
return null;
}
@Override
protected void onPreExecute() { //Activity is on progress
//displayToast("Your image is saving...");
btnSave.setVisibility(View.GONE);
btnShare.setVisibility(View.GONE);
pb.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Void v) { //Activity is done...
if (isSaved == true) {
displayToast("Image was saved.");
btnSave.setVisibility(View.GONE);
btnShare.setVisibility(View.VISIBLE);
pb.setVisibility(View.GONE);
}
if (isSaved == false) {
displayToast("Unable to save image. Try again later.");
btnSave.setVisibility(View.VISIBLE);
btnShare.setVisibility(View.GONE);
pb.setVisibility(View.GONE);
}
}
}
我所希望做的是:
- 當我畫的東西並保存圖像,共享按鈕就會出現,且畫布應該不明確(工作)
- 雖然SHARE按鈕是可見的,但是我在畫布上畫了其他東西,SAVE按鈕應該重新出現(工作)
- 當我再次按下SAVE按鈕時,它應該保存新的畫布圖(不起作用,它保存了以前我保存在第一步)
如何修改我的代碼繪製的帆布做這個:
不要創建爲每種顏色製作新的畫布,而是創造新的路徑。- 一旦保存了畫布上的圖形,並在現有畫布上繪製了一些新圖形並保存。新建畫布應該會與所有新老繪製路徑
清除如果ERASE按鈕被按下
我不能使用'priv在我的Paint類中ate'變量,因爲我將從另一個類訪問這些變量。變量'key'在哪裏被初始化? –
@ZahidH「私人」關鍵字不算,你可以刪除它。關於'鍵'變量是一個錯字,它是來自上一行的'顏色'。 – Luksprog
'//使用下面的路徑.'應該是我的代碼中的其他所有東西吧? –