我是Android(Java)的新手。
我要画一条线:

public void drawAline(int x1,int y1, int x2, int y2){
        Canvas canvas=new Canvas();
        paint.setColor(Color.BLACK);
        canvas.drawLine(x1,y1,x2,y2,paint);
    }


paint.setColor正在工作,但未画线:

呼叫:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.RED);
        drawView.drawAline(0,10,1000,10);

        setContentView(drawView);


问题是什么?

谢谢!

亲切的问候

丹尼尔

最佳答案

画一条线,然后使用此代码简单地画一条线

public class MainActivity extends Activity {

DrawLine drawLine;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    drawLine = new DrawLine(this);

    drawLine.setBackgroundColor(Color.CYAN);

    setContentView(drawLine);
  }

    class DrawLine extends View {
        Paint paint = new Paint();
        public DrawLine(Context context) {
            super(context);
            paint.setColor(Color.BLACK);
        }

        @Override
        public void onDraw(Canvas canvas) {
            canvas.drawLine(50, 100, 600, 600, paint);
            canvas.drawLine(50, 550, 770, 0, paint);
        }

    }
}

10-08 02:53