这是我正在编写的代码:---
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);
}
必需的输出:-黑色背景上的红色实心三角形。
我得到的输出是:-仅黑色背景。
那么导致错误的多数民众赞成在代码出什么问题了。
您只需要第一个path.moveTo()即可。删除所有其他出现的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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句