How to draw a line in android

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

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

相关标签:
15条回答
  • 2020-11-22 07:11

    If you want to have a simple Line in your Layout to separate two views you can use a generic View with the height and width you want the line to have and a set background color.

    With this approach you don't need to override a View or use a Canvas yourself just simple and clean add the line in xml.

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

    The example code I provided will generate a line that fills the screen in width and has a height of one dp.

    If you have problems with the drawing of the line on small screens consider to change the height of the line to px. The problem is that on a ldpi screen the line will be 0.75 pixel high. Sometimes this may result in a rounding that makes the line vanish. If this is a problem for your layout define the width of the line a ressource file and create a separate ressource file for small screens that sets the value to 1px instead of 1dp.

    This approach is only usable if you want horizontal or vertical lines that are used to divide layout elements. If you want to achieve something like a cross that is drawn into an image my approach will not work.

    0 讨论(0)
  • 2020-11-22 07:11

    or if you just want a line

    TextView line = new TextView(this);
                line.setBackgroundResource(android.R.color.holo_red_dark);
                line.setHeight((int) Utility.convertDpToPixel(1,this));
    
    0 讨论(0)
  • 2020-11-22 07:12

    You can draw multiple straight lines on view using Finger paint example which is in Developer android. example link

    Just comment: mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); You will be able to draw straight lines.

    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.graphics.Point;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.widget.ImageView;
    
    public class JoinPointsActivity extends Activity  {
        /** Called when the activity is first created. */
        Paint mPaint;
        float Mx1,My1;
        float x,y;
        @Override
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           // setContentView(R.layout.main);
            MyView view1 =new MyView(this);
            view1.setBackgroundResource(R.drawable.image_0031_layer_1);
            setContentView(view1);
    
    
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(0xFFFF0000);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
           // mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(10);
    
        }
    
        public class MyView extends View {
    
            private static final float MINP = 0.25f;
            private static final float MAXP = 0.75f;
    
          private Bitmap  mBitmap;
            private Canvas  mCanvas;
            private Path    mPath;
           private Paint   mBitmapPaint;
    
            public MyView(Context c) {
                super(c);
    
                mPath = new Path();
              mBitmapPaint = new Paint(Paint.DITHER_FLAG);
            }
    
            @Override
           protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                super.onSizeChanged(w, h, oldw, oldh);
                mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
                mCanvas = new Canvas(mBitmap);
            }
    
            @Override
            protected void onDraw(Canvas canvas) {
                canvas.drawColor(0xFFAAAAAA);
               // canvas.drawLine(mX, mY, Mx1, My1, mPaint);
               // canvas.drawLine(mX, mY, x, y, mPaint);
                canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
                canvas.drawPath(mPath, mPaint);
    
            }
    
            private float mX, mY;
            private static final float TOUCH_TOLERANCE = 4;
    
            private void touch_start(float x, float y) {
                mPath.reset();
                mPath.moveTo(x, y);
                mX = x;
                mY = y;
            }
            private void touch_move(float x, float y) {
                float dx = Math.abs(x - mX);
                float dy = Math.abs(y - mY);
                if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                   // mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                    mX = x;
                    mY = y;
                }
            }
            private void touch_up() {
                mPath.lineTo(mX, mY);
                // commit the path to our offscreen
                mCanvas.drawPath(mPath, mPaint);
                // kill this so we don't double draw
                mPath.reset();
            }
    
            @Override
            public boolean onTouchEvent(MotionEvent event) {
                float x = event.getX();
                float y = event.getY();
    
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        touch_start(x, y);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        touch_move(x, y);
                        invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        touch_up();
                   //   Mx1=(int) event.getX();
                     //  My1= (int) event.getY();
                       invalidate();
                        break;
                }
                return true;
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 07:12
    canvas.drawLine(10, 10, 90, 10, paint);
    canvas.drawLine(10, 20, 90, 20, paint);
    

    This will create a straight horizontal line, hope it helps!.

    0 讨论(0)
  • 2020-11-22 07:12

    You can make a drawable like circle, line, rectangle etc through shapes in xml as follow:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
        android:shape="line" >
    
        <solid android:color="#00000000" />
    
        <stroke
            android:width="2dp"
            android:color="#808080" />
    
    </shape>
    
    0 讨论(0)
  • 2020-11-22 07:12

    this code adds horizontal line to a linear layout

    View view = new View(this);
    LinearLayout.LayoutParams lpView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1); // --> horizontal
    view.setLayoutParams(lpView);
    view.setBackgroundColor(Color.DKGRAY);
    
    linearLayout.addView(view);
    
    0 讨论(0)
提交回复
热议问题