How to draw a line in android

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

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

15条回答
  •  礼貌的吻别
    2020-11-22 07:15

    Another approach to draw a line programatically using ImageView

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.widget.ImageView;
    
    public class Test extends Activity {
      ImageView drawingImageView;
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        drawingImageView = (ImageView) this.findViewById(R.id.DrawingImageView);
        Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
            .getDefaultDisplay().getWidth(), (int) getWindowManager()
            .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawingImageView.setImageBitmap(bitmap);
    
        // Line
        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(10);
        int startx = 50;
        int starty = 100;
        int endx = 150;
        int endy = 210;
        canvas.drawLine(startx, starty, endx, endy, paint);
    
      }
    }
    

提交回复
热议问题