How to draw a line in android

前端 未结 15 1752
野趣味
野趣味 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);
    
      }
    }
    
    0 讨论(0)
  • 2020-11-22 07:19

    There are two main ways you can draw a line, by using a Canvas or by using a View.

    Drawing a Line with Canvas

    From the documentation we see that we need to use the following method:

    drawLine (float startX, float startY, float stopX, float stopY, Paint paint)
    

    Here is a picture:

    canvas.drawLine

    The Paint object just tells Canvas what color to paint the line, how wide it should be, and so on.

    Here is some sample code:

    private Paint paint = new Paint();
    ....
    
    private void init() {
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(1f);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    
        startX = 20;
        startY = 100;
        stopX = 140;
        stopY = 30;
    
        canvas.drawLine(startX, startY, stopX, stopY, paint);
    }
    

    Drawing a Line with View

    If you only need a straight horizontal or vertical line, then the easiest way may be to just use a View in your xml layout file. You would do something like this:

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/black" />
    

    Here is a picture with two lines (one horizontal and one vertical) to show what it would look like:

    drawing a line with a view in xml layout

    And here is the complete xml layout for that:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="TextView1 in vertical linear layout" />
    
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/black" />
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="TextView2 in vertical linear layout" />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:padding="10dp"
            android:text="TextView3 in horizontal linear layout" />
    
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/black" />
    
        <TextView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:padding="10dp"
            android:text="TextView4 in horizontal linear layout" />
    </LinearLayout>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-11-22 07:19

    for horizontal line on the layout :

     <View
                android:id="@+id/View03"
                android:layout_width="fill_parent"
                android:layout_height="5dip"
                android:background="#0f0" />
    

    for vertical line on the layout :

    <View
            android:id="@+id/View04"
            android:layout_width="5dip"
            android:layout_height="fill_parent"
            android:background="#0f0" />
    
    0 讨论(0)
提交回复
热议问题