Reputation: 175
I am trying to make a simple drawing application, but am running into problems switching the paint color.Here is the tutorial I used to get to the point I am at. Here is my code: Drawing_View.java
public class Drawing_View extends View {
private Path path = new Path();
private Paint paint = new Paint();
public String paint_color = "#FFBB00";
public Drawing_View(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setStrokeWidth(5f);
paint.setColor(Color.parseColor(paint_color));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventY = event.getY();
float eventX = event.getX();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Set a new starting point
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
// Connect the points
path.lineTo(eventX, eventY);
break;
default:
return false;
}
invalidate();
return true;
}
}
Here is the code for the activity the custom view sits inside. Drawing_Main.java
public class Drawing_Main extends ActionBarActivity {
Button green_light,green_dark,blue_light,blue_dark,blue_verydark,purple,magenta,red,orange_dark,orange_light,orange_lightest,yellow,black;
String btn_color_hex;
SharedPreferences.Editor editor = getSharedPreferences("paint_color", MODE_PRIVATE).edit();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emoji_creation);
green_light = (Button)findViewById(R.id.button1);
green_dark = (Button)findViewById(R.id.button2);
blue_light = (Button)findViewById(R.id.button3);
blue_dark = (Button)findViewById(R.id.button4);
blue_verydark = (Button)findViewById(R.id.button5);
purple = (Button)findViewById(R.id.button6);
magenta = (Button)findViewById(R.id.button7);
red = (Button)findViewById(R.id.button8);
orange_dark = (Button)findViewById(R.id.button9);
orange_light = (Button)findViewById(R.id.button10);
orange_lightest = (Button)findViewById(R.id.button11);
yellow = (Button)findViewById(R.id.button12);
black = (Button)findViewById(R.id.button13);
green_light.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn_color_hex="#0fad00";
editor.putString("paint_color_hex", btn_color_hex);
editor.commit();
}
});
green_dark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
blue_light.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
blue_dark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
blue_verydark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
purple.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
magenta.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
red.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
orange_light.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
orange_lightest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
orange_dark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
yellow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
black.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_emojicreation, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
As you can see I started exploring the sharedPreferences option, after my attempt to create a service failed. If a Service is the best option, how can I make Drawing_View.java constantly listen for color changes? Thanks everyone!
Upvotes: 0
Views: 51
Reputation: 2764
You could add a setPaintColor
method to your Drawing_View
class. That way, you would not need to use SharedPreferences
and you could just change the paint color in your onClickListener
s.
Something like this should work:
In your Drawing_View
class, add:
public void setPaintColor (String color) {
paint.setColor(Color.parseColor(color));
}
In your onClickListener
, call the setPaintColor
method:
drawingView.setPaintColor(color);
I assume that your Drawing_View
is already instantiated.
Update
You need to instantiate your Drawing_View
the same way you instantiated the Button
s in your activity's onCreate
method.
public class Drawing_Main extends ActionBarActivity {
...
Drawing_View drawingView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emoji_creation);
drawingView = (Drawing_View)findViewById(R.id.drawing_view);
green_light = (Button)findViewById(R.id.button1);
...
I hope this helps.
Upvotes: 1