有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Java 安卓上绘制相交的两条线

所以我想画两条在中间交叉的线(像X),我想把它画在我的随机生成的位图上。有人知道我怎么做线路诊断吗

公共静态位图随机图像(整数宽度、整数高度){

    Bitmap ranImage = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Random rand = new Random(System.currentTimeMillis());
    for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++) {
            int r = rand.nextInt(255);
            int g = rand.nextInt(255);
            int b = rand.nextInt(255);
            int color = Color.rgb(r, g, b);
            ranImage.setPixel(x, y, color);
        }
    //initialise a new canvas instance
    Canvas canvas = new Canvas(ranImage);

        //intialise a new paint instance to draw the line
    Paint paint = new Paint();
    //line color
    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.STROKE);
    //line width in pixels
    paint.setStrokeWidth(3);
    paint.setAntiAlias(true);

    //set a pizel value to offset the line from the canvas edge
    int offset = 50;




    //draw a line on canvas at the center postion ....for now
    canvas.drawLine(
            offset,
            canvas.getHeight()/2,
            canvas.getWidth()-offset,
            canvas.getHeight()/2,
            paint
    );
    

    return ranImage;
}

共 (0) 个答案