我正在尝试创建一个绘图应用程序,到目前为止,我可以选择两种颜色(绿色和黑色)和两种形状(圆形和方形)。我想添加更多颜色来绘画,但我无法弄清楚如何添加超过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;
}
答案 0 :(得分: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");
接下来,假设您的代码如何工作,它会通过fill
的{{1}}属性来处理它。如果colours
为0,则为绿色,否则为黑色。让我们添加一个案例fill
,它将是红色的。
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;
...
设为2并进一步推动了画笔设置。这是为了确保颜色分组。
现在,我可以从您提供的代码中建议这一切。也许还有更多。但你应该看看这个。