Skip to content

Commit 62492a1

Browse files
committed
Changing the Color
1 parent acd049c commit 62492a1

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

app/src/main/java/com/dragosholban/androiddrawing/DrawingView.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
public class DrawingView extends View {
1616

1717
private ArrayList<Path> paths = new ArrayList<>();
18+
private ArrayList<Integer> colors = new ArrayList<>();
19+
private int currentColor = 0xFF000000;
1820

1921
public DrawingView(Context context) {
2022
super(context);
@@ -35,6 +37,7 @@ public DrawingView(Context context, @Nullable AttributeSet attrs, int defStyleAt
3537

3638
public void addPath(Path path) {
3739
paths.add(path);
40+
colors.add(currentColor);
3841
}
3942

4043
public Path getLastPath() {
@@ -49,12 +52,18 @@ public Path getLastPath() {
4952
protected void onDraw(Canvas canvas) {
5053
super.onDraw(canvas);
5154

55+
int i = 0;
5256
for (Path path : paths) {
5357
Paint paint = new Paint();
54-
paint.setColor(0X80000000);
58+
paint.setColor(colors.get(i));
5559
paint.setStyle(Paint.Style.STROKE);
5660
paint.setStrokeWidth(3f);
5761
canvas.drawPath(path, paint);
62+
i++;
5863
}
5964
}
65+
66+
public void setCurrentColor(int color) {
67+
currentColor = color;
68+
}
6069
}

app/src/main/java/com/dragosholban/androiddrawing/MainActivity.java

+7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.dragosholban.androiddrawing;
22

3+
import android.graphics.drawable.ColorDrawable;
34
import android.support.v7.app.AppCompatActivity;
45
import android.os.Bundle;
6+
import android.view.View;
57

68
public class MainActivity extends AppCompatActivity {
79

@@ -15,4 +17,9 @@ protected void onCreate(Bundle savedInstanceState) {
1517
drawingView = findViewById(R.id.canvas);
1618
drawingView.setOnTouchListener(new TouchListener());
1719
}
20+
21+
public void setColor(View view) {
22+
ColorDrawable buttonColor = (ColorDrawable) view.getBackground();
23+
drawingView.setCurrentColor(buttonColor.getColor());
24+
}
1825
}

app/src/main/res/layout/activity_main.xml

+8-4
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,28 @@
3939
android:layout_height="match_parent"
4040
android:layout_weight="1"
4141
android:layout_margin="1dp"
42-
android:background="#FF0000"/>
42+
android:background="#FF0000"
43+
android:onClick="setColor"/>
4344
<View
4445
android:layout_width="0dp"
4546
android:layout_height="match_parent"
4647
android:layout_weight="1"
4748
android:layout_margin="1dp"
48-
android:background="#00FF00"/>
49+
android:background="#00FF00"
50+
android:onClick="setColor"/>
4951
<View
5052
android:layout_width="0dp"
5153
android:layout_height="match_parent"
5254
android:layout_weight="1"
5355
android:layout_margin="1dp"
54-
android:background="#0000FF"/>
56+
android:background="#0000FF"
57+
android:onClick="setColor"/>
5558
<View
5659
android:layout_width="0dp"
5760
android:layout_height="match_parent"
5861
android:layout_weight="1"
5962
android:layout_margin="1dp"
60-
android:background="#000000"/>
63+
android:background="#000000"
64+
android:onClick="setColor"/>
6165
</LinearLayout>
6266
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)