Permalink
Browse files

Changing the Color

  • Loading branch information...
dragosholban committed Apr 21, 2018
1 parent acd049c commit 62492a193a2328ca72a6da699c53de6fef4b9d68
@@ -15,6 +15,8 @@
public class DrawingView extends View {
private ArrayList<Path> paths = new ArrayList<>();
private ArrayList<Integer> colors = new ArrayList<>();
private int currentColor = 0xFF000000;
public DrawingView(Context context) {
super(context);
@@ -35,6 +37,7 @@ public DrawingView(Context context, @Nullable AttributeSet attrs, int defStyleAt
public void addPath(Path path) {
paths.add(path);
colors.add(currentColor);
}
public Path getLastPath() {
@@ -49,12 +52,18 @@ public Path getLastPath() {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int i = 0;
for (Path path : paths) {
Paint paint = new Paint();
paint.setColor(0X80000000);
paint.setColor(colors.get(i));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3f);
canvas.drawPath(path, paint);
i++;
}
}
public void setCurrentColor(int color) {
currentColor = color;
}
}
@@ -1,7 +1,9 @@
package com.dragosholban.androiddrawing;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@@ -15,4 +17,9 @@ protected void onCreate(Bundle savedInstanceState) {
drawingView = findViewById(R.id.canvas);
drawingView.setOnTouchListener(new TouchListener());
}
public void setColor(View view) {
ColorDrawable buttonColor = (ColorDrawable) view.getBackground();
drawingView.setCurrentColor(buttonColor.getColor());
}
}
@@ -39,24 +39,28 @@
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="1dp"
android:background="#FF0000"/>
android:background="#FF0000"
android:onClick="setColor"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="1dp"
android:background="#00FF00"/>
android:background="#00FF00"
android:onClick="setColor"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="1dp"
android:background="#0000FF"/>
android:background="#0000FF"
android:onClick="setColor"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="1dp"
android:background="#000000"/>
android:background="#000000"
android:onClick="setColor"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

0 comments on commit 62492a1

Please sign in to comment.