Reputation: 1063
Here is the code that I am writing:---
public class board extends Activity
{
Canvas canvas;Bitmap bitmap;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Log.d("board", "height is" + height + "width"+ width );
bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
Paint blue = new Paint();
Paint red = new Paint();
Paint green = new Paint();
Paint yellow = new Paint();
blue.setColor(Color.BLUE);
blue.setStrokeWidth(5);
red.setColor(Color.RED);
red.setStrokeWidth(5);
green.setColor(Color.GREEN);
green.setStrokeWidth(5);
yellow.setColor(Color.YELLOW);
yellow.setStrokeWidth(5);
canvas = new Canvas(bitmap);
canvas.drawColor(Color.BLACK);
red.setStyle(Paint.Style.FILL);
Path path = new Path();
path.setFillType(FillType.EVEN_ODD);
path.moveTo(width/5, height/3);
path.lineTo((4*width)/5, (height)/3);
path.moveTo((4*width)/5, (height)/3);
path.lineTo(width/2, (17*height)/30);
path.moveTo(width/2, (17*height)/30);
path.lineTo(width/5, height/3);
path.moveTo(width/5, height/3);
path.close();
canvas.drawPath(path, red);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmap);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(imageView, params);
layout.setBackgroundColor(Color.BLACK);
setContentView(layout);
}
Required Output:-- A filled triangle of red color on black background.
Output I am getting:-- Just a black background.
So what is wrong with the code thats causing the error.
Upvotes: 0
Views: 2514
Reputation: 8598
You only need the first path.moveTo(). Remove all other occurrences of path.moveTo():
Path path = new Path();
path.setFillType(FillType.EVEN_ODD);
path.moveTo(width/5, height/3); //<----- keep only this call to moveTo()
path.lineTo((4*width)/5, (height)/3);
path.moveTo((4*width)/5, (height)/3); //<----- remove this call
path.lineTo(width/2, (17*height)/30);
path.moveTo(width/2, (17*height)/30); //<----- remove this call
path.lineTo(width/5, height/3);
path.moveTo(width/5, height/3); //<----- remove this call
path.close();
canvas.drawPath(path, red);
//rest of the code
Upvotes: 6