絵を描いたり、マス目を描画したりすることは完成しました。お絵描きアプリなので描いた絵を保存したいのですが、色々と調べてみて実装しようとしたのですが、うまくいきませんでした。
import android.content.Context;
import android.content.ReceiverCallNotAllowedException;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class DrawingView extends View {
// 履歴
private List<DrawLine> lines;
// 現在、描いている線の情報
private Paint paint;
private Path path;
private Rect rect;
private Paint paint1;
// 線の履歴(座標+色)
class DrawLine {
private Paint paint;
private Path path;
private Rect rect;
private Paint paint1;
DrawLine(Path path, Paint paint, Rect rect) {
this.paint = new Paint(paint);
this.path = new Path(path);
this.rect = new Rect(rect);
//this.paint1 = new Paint(paint);
}
void draw(Canvas canvas) {
canvas.drawPath(this.path, this.paint);
}
}
public DrawingView(Context context) {
super(context);
}
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
this.path = new Path();
this.paint = new Paint();
this.rect = new Rect();
this.paint.setStyle(Paint.Style.STROKE);
// this.paint.setStrokeJoin(Paint.Join.MITER);//これがあると角が丸くなる
this.paint.setStrokeCap(Paint.Cap.SQUARE);
this.paint.setAntiAlias(true);
this.paint.setStrokeWidth(40);
this.lines = new ArrayList<DrawLine>();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// キャンバスをクリア
canvas.drawColor(Color.WHITE);
// 履歴から線を描画
for (DrawLine line : this.lines) {
line.draw(canvas);
}
// 現在、描いている線を描画
canvas.drawPath(this.path, this.paint);
Paint paint1 = new Paint(Color.WHITE);
paint1.setColor(Color.BLACK);
paint1.setStyle(Paint.Style.STROKE);//ペン先に合わせて色が一緒に代わってしまうので保留
for (int i = 0; i < 120; i++) {
for (int j = 0; j < 120; j++) {
int a = 40;
Rect rect = new Rect(a * i, a * j, a * (i + j) - 1, a * (i + j) - 1);
canvas.drawRect(rect, paint1);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float dx = event.getX();
float dy = event.getY();
int x = (int) (dx - dx % 40) +20;
int y = (int) (dy - dy % 40) +20;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.path.moveTo(x, y);
this.path.lineTo(x, y);
break;
case MotionEvent.ACTION_MOVE:
this.path.lineTo(x, y);
break;
case MotionEvent.ACTION_UP:
this.path.lineTo(x , y );
// 指を離したので、履歴に追加する
this.lines.add(new DrawLine(this.path, this.paint, this.rect));
// パスをリセットする
// これを忘れると、全ての線の色が変わってしまう
this.path.reset();
break;
}
invalidate();
return true;
}
public void delete() {
// 履歴をクリア
this.lines.clear();
// 現在の線をクリア
this.path.reset();
invalidate();
}
public void setPen(int color){
this.paint.setColor(color);
}
public void setStrokeWidth(float width){
this.paint.setStrokeWidth(width);
}
}
MainActivity
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CpuUsageInfo;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ImageButton;
import android.provider.MediaStore;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private DrawingView drawingView;
ImageButton red_button,blue_button,yellow_button,black_button,kesigomu_button,cyan_button,gray_button,green_button;
ImageButton orange_button,pink_button,purple_button,kimidori_button,mizuiro_button,hadairo_button;
Button hutosa1_button,hutosa2_button,kukei_button;
Button savebutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//線の色指定
blue_button= findViewById(R.id.blue_button);
red_button= findViewById(R.id.red_button);
yellow_button= findViewById(R.id.yellow_button) ;
black_button= findViewById(R.id.black_button);
kesigomu_button= findViewById(R.id.kesigomu_button);
cyan_button= findViewById(R.id.cyan_button);
gray_button= findViewById(R.id.gray_button);
green_button= findViewById(R.id.green_button);
orange_button= findViewById(R.id.orange_button);
pink_button= findViewById(R.id.pink_button);
purple_button= findViewById(R.id.purple_button);
kimidori_button= findViewById(R.id.kimidori_button);
mizuiro_button= findViewById(R.id.mizuiro_button);
hadairo_button= findViewById(R.id.hadairo_button);
//線の太さ指定
hutosa1_button= findViewById(R.id.hutosa1_button);
hutosa2_button= findViewById(R.id.hutosa2_button);
//矩形処理
kukei_button= findViewById(R.id.kukei_button);
//保存ボタン
savebutton= findViewById(R.id.savebutton);
blue_button.setOnClickListener(this);
red_button.setOnClickListener(this);
yellow_button.setOnClickListener(this);
black_button.setOnClickListener(this);
kesigomu_button.setOnClickListener(this);
cyan_button.setOnClickListener(this);
gray_button.setOnClickListener(this);
green_button.setOnClickListener(this);
orange_button.setOnClickListener(this);
pink_button.setOnClickListener(this);
purple_button.setOnClickListener(this);
kimidori_button.setOnClickListener(this);
mizuiro_button.setOnClickListener(this);
hadairo_button.setOnClickListener(this);
hutosa1_button.setOnClickListener(this);
hutosa2_button.setOnClickListener(this);
kukei_button.setOnClickListener(this);
savebutton.setOnClickListener(this);
this.drawingView = findViewById(R.id.drawing_view);
findViewById(R.id.deletebutton).setOnClickListener(deleteDrawing);
}
View.OnClickListener deleteDrawing = new View.OnClickListener(){
@Override
public void onClick(View view){
drawingView.delete();
}
};
public void onClick(View v){
switch(v.getId()){
//線の色指定
case R.id.blue_button:
drawingView.setPen(Color.BLUE);
Toast.makeText(this,"blue",Toast.LENGTH_SHORT).show();
break;
case R.id.red_button:
drawingView.setPen(Color.RED);
Toast.makeText(this,"red",Toast.LENGTH_SHORT).show();
break;
case R.id.yellow_button:
drawingView.setPen(Color.YELLOW);
Toast.makeText(this,"yellow",Toast.LENGTH_SHORT).show();
break;
case R.id.black_button:
drawingView.setPen(Color.BLACK);
Toast.makeText(this,"black",Toast.LENGTH_SHORT).show();
break;
case R.id.kesigomu_button:
drawingView.setPen(Color.WHITE);
drawingView.setStrokeWidth(40);
Toast.makeText(this,"消しゴム",Toast.LENGTH_SHORT).show();
break;
case R.id.cyan_button:
drawingView.setPen(Color.CYAN);
Toast.makeText(this,"Cyan",Toast.LENGTH_SHORT).show();
break;
case R.id.gray_button:
drawingView.setPen(Color.GRAY);
Toast.makeText(this,"gray",Toast.LENGTH_SHORT).show();
break;
case R.id.green_button:
drawingView.setPen(Color.GREEN);
Toast.makeText(this,"green",Toast.LENGTH_SHORT).show();
break;
case R.id.orange_button:
drawingView.setPen(Color.rgb(255,165,0));
Toast.makeText(this,"orange",Toast.LENGTH_SHORT).show();
break;
case R.id.pink_button:
drawingView.setPen(Color.rgb(255,192,187));
Toast.makeText(this,"pink",Toast.LENGTH_SHORT).show();
break;
case R.id.purple_button:
drawingView.setPen(Color.rgb(128,0,128));
Toast.makeText(this,"purple",Toast.LENGTH_SHORT).show();
break;
case R.id.kimidori_button:
drawingView.setPen(Color.rgb(85,107,47));
Toast.makeText(this,"darkgreen",Toast.LENGTH_SHORT).show();
break;
case R.id.mizuiro_button:
drawingView.setPen(Color.GREEN);
Toast.makeText(this,"green",Toast.LENGTH_SHORT).show();
break;
case R.id.hadairo_button:
drawingView.setPen(Color.rgb(245,208,169));
Toast.makeText(this,"hadairo",Toast.LENGTH_SHORT).show();
break;
//線の太さ指定
case R.id.hutosa1_button:
drawingView.setStrokeWidth(40);
Toast.makeText(this,"ペン先 1×1",Toast.LENGTH_SHORT).show();
break;
case R.id.hutosa2_button:
drawingView.setStrokeWidth(120);
Toast.makeText(this,"ペン先 3×3",Toast.LENGTH_SHORT).show();
break;
case R.id.kukei_button:
drawingView.setStrokeWidth(200);
Toast.makeText(this,"矩形",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}