How to draw a line in android

前端 未结 15 1757
野趣味
野趣味 2020-11-22 06:27

Can anybody tell how to draw a line in Android, perhaps with an example?

15条回答
  •  情深已故
    2020-11-22 07:04

    package com.example.helloandroid;
    
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.os.Bundle;
    import android.view.View;
    
    public class HelloAndroid2Activity extends Activity {
        /** Called when the activity is first created. */
    DrawView drawView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);
    }
    class DrawView extends View {
            Paint paint = new Paint();
    
            public DrawView(Context context) {
                super(context);
                paint.setColor(Color.BLUE);
            }
            @Override
            public void onDraw(Canvas canvas) {
                 super.onDraw(canvas);
                    canvas.drawLine(10, 20, 30, 40, paint);
                    canvas.drawLine(20, 10, 50, 20, paint);
    
            }
    }
    }
    

提交回复
热议问题