简体   繁体   English

Android Canvas drawLine无法在MainActivity上绘制

[英]Android Canvas drawLine not drawing on MainActivity

I want to draw a line on the Main Activity using Canvas. 我想使用Canvas在Main Activity上画一条线。 The problem is, it is not drawing anything. 问题是,它没有画任何东西。 I have the following code: 我有以下代码:

 Bitmap bitmap = Bitmap.createBitmap(1920, 1080, Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(10);

float left = 20;
float top = 20;
float right = 50;
float bottom = 100;

canvas.drawLine(left, top, right, bottom, paint);

you can display the bitmap like that: 您可以像这样显示位图:

canvas.drawBitmap(bmp, positionX, positionY, paint);

in your case you can try somthing like this: 在您的情况下,您可以尝试以下操作:

canvas.drawBitmap(bitmap, 0, 0, null);

but you need to use a diffrent canvas for it. 但您需要使用其他画布。 The canvas wich let you draw stuff on your screen will be passed to your onDraw() method in your View. 允许您在屏幕上绘制内容的画布将被传递到View中的onDraw()方法。 So you need to make a View class first and add it in your MainActivity. 因此,您需要首先创建一个View类并将其添加到MainActivity中。

You can do that like this: First you create a class called MyView and add this code to it: 您可以这样进行:首先创建一个名为MyView的类,然后将以下代码添加到其中:

public class MyView extends View {

    Bitmap bitmap;

    public MyView(Context context) {
        bitmap = Bitmap.createBitmap(1920, 1080, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeWidth(10);

        float left = 20;
        float top = 20;
        float right = 50;
        float bottom = 100;

        canvas.drawLine(left, top, right, bottom, paint);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(bitmap, 0, 0, null);
        super.onDraw(canvas);
    }
}

then you change the code in your onCreate() method in your MainActivity to this: 然后在MainActivity的onCreate()方法中将代码更改为此:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  

    MyView myView = new MyView(this);
    setContentView(myView);
}

Create a class like this 创建这样的课程

public class MyView extends View {
public MyView(Context context) {
    super(context);
}

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setStrokeWidth(10);

    float left = 20;
    float top = 20;
    float right = 50;
    float bottom = 100;

    canvas.drawLine(left, top, right, bottom, paint);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM