【发布时间】:2014-02-28 01:18:59
【问题描述】:
这是我正在编写的代码:---
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);
}
需要的输出:-- 黑色背景上的红色实心三角形。
我得到的输出:-- 只是黑色背景。
那么导致错误的代码有什么问题。
【问题讨论】:
-
不,导致强制关闭。现在,如果我使用“canvas.drawLine”画线,我会得到输出,但不是这样。
-
ok 不要在每行之后移动到
-
是的,谢谢!