[英]Java. How do I add more colours to paint?
我正在尝试创建一个绘图应用程序,到目前为止,我可以选择两种颜色(绿色和黑色)和两种形状(圆形和方形)。 我想添加更多颜色来绘画,但我无法弄清楚如何添加超过2种颜色。
这些是我正在使用的类。
public class DrawLinesActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawshapes);
DrawLines d = new DrawLines(this);
setContentView (d);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuItem menu1 = menu.add(0, 0, Menu.NONE, "Green");
MenuItem menu2 = menu.add(0, 1, Menu.NONE, "Black");
MenuItem menu3 = menu.add(0,2, Menu.NONE, "Pixel Brush");
MenuItem menu4 = menu.add(0,3, Menu.NONE, "Smooth Brush");
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case 0:
colours.fill = 0;
return true;
case 1:
colours.fill = 1;
return true;
case 2:
colours.shape = 0;
return true;
case 3:
colours.shape = 1;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
...
public class DrawLines extends View
{
Canvas c;
Paint paint;
Bitmap bmp;
Random g;
float X, Y;
public DrawLines(Context context)
{
super(context);
g=new Random();
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
bmp= Bitmap.createBitmap(1050,1650,conf);
paint = new Paint();
paint.setStyle (Paint.Style.STROKE);
paint.setColor (Color.WHITE);
this.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
float x, y;
c = new Canvas (bmp);
x = event.getX ();
y = event.getY ();
System.out.printf ("%f %f\n", X, Y);
paint.setAntiAlias (true);
if (colours.fill == 0)
{
paint.setStyle (Paint.Style.FILL);
paint.setColor (Color.GREEN);
if (colours.shape == 0)
c.drawRect (x, y, x + 50, y + 50, paint);
else
c.drawOval(new RectF (x, y, x + 50, y + 50), paint);
}
else
{
paint.setStyle (Paint.Style.FILL);
paint.setColor (Color.BLACK);
if (colours.shape == 0)
c.drawRect (x, y, x + 50, y + 50, paint);
else
c.drawOval(new RectF (x, y, x + 50, y + 50), paint);
}
paint.setColor (Color.WHITE);
invalidate ();
return true;
}
});
}
@Override
protected void onDraw (Canvas c)
{
super.onDraw (c);
c.drawBitmap (bmp, 0, 0, paint);
}
}
...
public class colours
{
static public int shape = 0;
static public int fill = 0;
}
首先,确保用户可以访问其他颜色。 我们试着添加红色。
MenuItem menu1 = menu.add(0, 0, Menu.NONE, "Green");
MenuItem menu2 = menu.add(0, 1, Menu.NONE, "Black");
MenuItem menu2 = menu.add(0, 2, Menu.NONE, "Red"); //Make an item for red.
MenuItem menu3 = menu.add(0, 3, Menu.NONE, "Pixel Brush");
MenuItem menu4 = menu.add(0, 4, Menu.NONE, "Smooth Brush");
接下来,假设您的代码如何工作,它通过colours
的fill
属性处理它。 如果fill
为0,则为绿色,否则为黑色。 让我们添加一个案例1
,它将是红色的。
if (colours.fill == 1)
{
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
...
}
else if (colours.fill == 0)
{
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.GREEN);
...
}
else
{
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLACK);
...
}
我们还应确保在按钮点击代码区域中添加Red
的案例:
...
case 0:
colours.fill = 0;
return true;
case 1:
colours.fill = 1;
return true;
case 2:
colours.fill = 2;
return true;
case 3:
colours.shape = 0;
return true;
case 4:
colours.shape = 1;
return true;
...
您可能会看到我将Red
为2并进一步推动了画笔设置。 这是为了确保颜色分组。
现在,这是我可以从您提供的代码中建议的全部内容。 也许还有更多。 但你应该看看这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.