简体   繁体   English

Android绘图线有2分

[英]Android Drawing Line with 2 points

Currently I'm developing an app, where line will be drawn from one point to another with button click on a bitmap. 目前我正在开发一个应用程序,通过按钮点击位图,将从一个点绘制到另一个点。 Here's my code in MainActivity.java: 这是我在MainActivity.java中的代码:

    public class MainActivity extends Activity {
    LineView lineview;
    Button button;

    @Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   //lineview = (LineView)findViewById (R.id.lineView1);
   button = (Button)findViewById(R.id.btnCapture); 
   //lineview.setVisibility(View.INVISIBLE);
   button.setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View v) {

          // lineview.setVisibility(View.VISIBLE);
       }
   });


 }
}

LineView.java - class that draws line LineView.java - 绘制线条的类

public class LineView extends View {
Paint paint = new Paint();

public LineView(Context context, AttributeSet attrs) {
  super(context, attrs);
  }

public LineView(Context context, AttributeSet attrs, int defstyle) {
super(context, attrs, defstyle );
  }


public LineView(Context context) {
super(context);
paint.setColor(Color.BLACK);
}

@Override
public void onDraw(Canvas canvas) {
    canvas.drawLine(0, 0, 20, 20, paint);
}

}

Right now it draws the line from the start using defined coordinates. 现在,它使用定义的坐标从头开始绘制线条。 I want line to be drawn using coordinates from 2 points that get declared in main activity. 我希望使用在主要活动中声明的2个点的坐标绘制线。 And that function should work after onClick. 并且该功能应该在onClick之后工作。 Thanks in advance. 提前致谢。

You will have to create a custom view class that extends View . 您必须创建一个扩展View的自定义视图类。 Within that class you will override the onDraw method which is passed a canvas. 在该类中,您将覆盖传递画布的onDraw方法。 create a Paint object and use it with the drawLine function above. 创建一个Paint对象并将其与上面的drawLine函数一起使用。

I would suggest looking up Finger Paint app tutorials. 我建议查找Finger Paint应用程序教程。 There are at least a few online, and they are a good introduction to custom views and overriding the onDraw method. 至少有几个在线,它们是对自定义视图的良好介绍并覆盖了onDraw方法。

Use customview by extending the view class to achieve this: Let's call your custom class say LineView. 通过扩展视图类来实现这一点来使用customview:让我们调用你的自定义类说LineView。 So this is what Line should look like. 所以这就是Line应该是什么样子。

LineView.java LineView.java

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.view.View;

    public class LineView extends View {
    Paint paint = new Paint();

  public LineView(Context context, AttributeSet attrs) {
      super(context, attrs);
      }

    public LineView(Context context, AttributeSet attrs, int defstyle) {
    super(context, attrs, defstyle );
      }


    public LineView(Context context) {
    super(context);
    paint.setColor(Color.BLACK);
}

@Override
public void onDraw(Canvas canvas) {
        canvas.drawLine(0, 0, 20, 20, paint);
}

}

Now, you'll have to instantiate this in your mainactivity. 现在,您必须在mainactivity中实例化它。 You can do that using java code or xml. 你可以使用java代码或xml来做到这一点。 Using java code it will look like this: 使用java代码,它将如下所示:

   import android.app.Activity;
   import android.graphics.Color;
   import android.os.Bundle;

 public class MainActivity extends Activity {
     LineView lineview;
     Button button;

     @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lineview = (LineView)findViewById (R.id.lineView1);
    button = (Button)findViewById(R.id.button1); 
    lineview.setVisibility(View.INVISIBLE);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            lineview.setVisibility(View.VISIBLE);
        }
    });


  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM