今、 Android Studioを使ってドット打ちアプリを開発しています。現在問題にぶち当たっており、ドット打ちアプリを作っているためCanvas上にマス目を表示できるように開発しております。マス目を表示させることまでは完成しているのですが、MainActivityの線の処理を使用しているのか線の色を変更したり太さを変更したりするとマス目の線の色や太さまでが一緒に変更されてしまいます。マス目側の処理で色を固定しようとすると、すべての色や太さが固定されてしまったりと開発が難航しております。いい方法などを模索はしているのですが見つかりません。また、マス目をボタンなどを使用して表示・非表示を制御したりすることはできるのでしょうか?
1月5日19:52分追記
ありがとうございます!!!このような形で実装することができました!!!
package myapplication; 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 { int touchX; //タッチした際のX int touchY; //タッチした際のY // 履歴 private List<DrawLine> lines; // 現在、描いている線の情報 private Paint paint; private Path path; private Rect rect; // 線の履歴(座標+色) class DrawLine { private Paint paint; private Path path; private Rect rect; DrawLine(Path path, Paint paint, Rect rect) { this.paint = new Paint(paint); this.path = new Path(path); this.rect = new Rect(rect); } 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(15); 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 paint = new Paint(Color.WHITE); // paint.setColor(WHITE);//ペン先に合わせて色が一緒に代わってしまうので保留 //canvas上に升目を表示 for (int i = 0; i < 101; i++) { for (int j = 0; j < 101; j++) { int a = 30; Rect rect = new Rect(a * i, a * j, a * (i + j) - 1, a * (i + j) - 1); canvas.drawRect(rect, paint); } } } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); 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.java
package myapplication; 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.Toast; import android.widget.SeekBar; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private DrawingView drawingView; Button red_button,blue_button,yellow_button,black_button,kesigomu_button; Button hutosa1_button,hutosa2_button; @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); //線の太さ指定 hutosa1_button= findViewById(R.id.hutosa1_button); hutosa2_button= findViewById(R.id.hutosa2_button); blue_button.setOnClickListener(this); red_button.setOnClickListener(this); yellow_button.setOnClickListener(this); black_button.setOnClickListener(this); kesigomu_button.setOnClickListener(this); hutosa1_button.setOnClickListener(this); hutosa2_button.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(60); Toast.makeText(this,"消しゴム",Toast.LENGTH_SHORT).show(); break; //線の太さ指定 case R.id.hutosa1_button: drawingView.setStrokeWidth(15); Toast.makeText(this,"太さ中",Toast.LENGTH_SHORT).show(); break; case R.id.hutosa2_button: drawingView.setStrokeWidth(30); Toast.makeText(this,"太さ大",Toast.LENGTH_SHORT).show(); break; } } @Override public void onPointerCaptureChanged(boolean hasCapture) { } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/03 14:58
2019/01/04 03:57
2019/01/04 11:27
2019/01/05 06:54
2019/01/05 10:51