gpt4 book ai didi

java - 用 Java 类 Android 画一条线

转载 作者:行者123 更新时间:2023-12-01 18:06:20 25 4
gpt4 key购买 nike

我是 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);
}

}
}

关于java - 用 Java 类 Android 画一条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36151662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com