0
フォアグラウンドとバックグラウンドイメージを作成しようとしているコードを実装しています。フォアグラウンドイメージを消去したいのですが、間違えたら元に戻すことができます。背景のために、私はイメージ図と前景を使用していますキャンバスビットマップを削除して背景を表示する
は
が機能していない私はここまで働いているキャンバス上にビットマップ、 それはフォアグラウンド(すなわちキャンバス)を消去するが、REDOを元に戻すされています
コードは次のとおりです。
public class MainActivity extends AppCompatActivity {
private Bitmap DrawBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint DrawBitmapPaint;
RelativeLayout Rl;
CustomView View;
DrawView drawView;
private Button undo, redo;
private ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.loadActivity();
}
private Paint mPaint;
public class CustomView extends View {
public CustomView(Context c) {
super(c);
create_image();
setLayerType(android.view.View.LAYER_TYPE_SOFTWARE, mPaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// mCanvas.drawColor(Color.BLUE);
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
for (Path p : paths){
canvas.drawPath(p, mPaint);
}
setDrawingCacheEnabled(true);
canvas.drawBitmap(DrawBitmap, 0, 0, DrawBitmapPaint);
canvas.drawPath(mPath, mPaint);
canvas.drawRect(mY, 0, mY, 0, DrawBitmapPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
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 onTouchEvent(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();
// performClick();
invalidate();
break;
}
return true;
}
public void clear() {
create_image();
// Added later..
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
// mCanvas.drawColor(Color.BLUE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(40);
this.invalidate();
}
public void onClickUndo() {
if (paths.size() > 0) {
undonePaths.add(paths.remove(paths.size() - 1));
invalidate();
} else {
}
//toast the user
}
public void onClickRedo() {
if (undonePaths.size() > 0) {
paths.add(undonePaths.remove(undonePaths.size() - 1));
invalidate();
} else {
}
//toast the user
}
}
public void loadActivity() {
undo = (Button) findViewById(R.id.button1);
redo = (Button) findViewById(R.id.button2);
drawView = new DrawView(this);
View = new CustomView(this);
Rl = (RelativeLayout) findViewById(R.id.linearLayout2);
Rl.addView(View);
Bitmap bitmap = BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.sample);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
// mCanvas.drawColor(Color.BLUE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(40);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mPaint.setStrokeWidth(40);
mPaint.setColor(Color.BLUE);
mPaint.setStrokeWidth(40);
undo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View.onClickUndo();
}
});
redo.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
View.onClickRedo();
}
});
}
public void create_image() {
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
DrawBitmap = Bitmap.createBitmap(screenWidth, screenHeight,
Bitmap.Config.ARGB_4444);
mCanvas = new Canvas(DrawBitmap);
Bitmap bitmap = BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.sample);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
mCanvas.drawBitmap(bitmap, 0, 0, null);
mPath = new Path();
DrawBitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
paths.add(mPath);
}
}
と私のレイアウトは次のとおりです。
とmCanvas
:の<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.approduction.drawing.MainActivity">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Undo" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Redo" />
</LinearLayout>
<RelativeLayout
android:id="@+id/linearLayout2"
android:layout_below="@id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"/>
</RelativeLayout>
任意の提案は、事前に 感謝..
を、私はそれをu [Androidのキャンバスやり直しと元に戻す操作]を助けますね(http://stackoverflow.com/questions/11114625/android-canvas-redo-アンドゥー操作) – Raghavendra
私はそのコードを実装しましたが、画像を描画していないので、画像ビューのみが表示されます。フォアグラウンドはありません。フォアグラウンドで実行されたものを元に戻したいと思います。 – Ashwani