2

Hi I'm doing a practice project which could draw a line with the coordinates I put. The screen only have to textfields and one button. For example if I put "20" and "30" in those two textfields and click the "draw" button, I want the app to draw a line from (0,0) to (20,30) in ANOTHER VIEW.

I already know how to use "onDraw()" to draw a line but I dont know hot to pass those two parameters into the onDraw() function. Plus, should I create a new view every time I click the draw button or just change the onDraw() function in one view?

Thanks!!!!!!!

YueQi Li
  • 193
  • 3
  • 14

2 Answers2

5

So what you want to do is keep the Views from having to worry about each other. You have one View that handles drawing the line, two EditText views that handle the input, and for example's sake, a button to submit the coordinates. Assuming you have a layout containing these views, here's an example of a simple custom view that you could use to draw the line:

public class LineView extends View {
    /**
     * Container to hold the x1, y1, x2, y2 values, respectively
     */
    private float[] mCoordinates;

    /**
     * The paint with which the line will be drawn
     */
    private Paint mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public LineView (Context context) {
        super(context);
    }

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

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

    /**
     * Set the color with which the line should be drawn
     * @param color the color to draw the line with
     */
    public void setLineColor (int color) {
        mLinePaint.setColor(color);
        invalidate();
    }

    /**
     * Set the coordinates of the line to be drawn. The origin (0, 0) is the
     * top left of the View.
     * @param x1 the starting x coordinate
     * @param y1 the starting y coordinate
     * @param x2 the ending x coordinate
     * @param y2 the ending y coordinate
     */
    public void setCoordinates (float x1, float y1, float x2, float y2) {
        ensureCoordinates();

        mCoordinates[0] = x1;
        mCoordinates[1] = y1;
        mCoordinates[2] = x2;
        mCoordinates[3] = y2;

        invalidate();
    }

    private void ensureCoordinates () {
        if (mCoordinates == null) {
            mCoordinates = new float[4];
        }
    }

    @Override
    protected void onDraw (Canvas canvas) {
        if (mCoordinates != null) {
            canvas.drawLine(
                    mCoordinates[0],
                    mCoordinates[1],
                    mCoordinates[2],
                    mCoordinates[3],
                    mLinePaint
            );
        }
    }
}

Along with a quick example, given the assumptions made above about your layout, of how you could implement this.

public class EditTextActivity extends Activity implements View.OnClickListener {
    private EditText mInputX;
    private EditText mInputY;
    private Button mDrawButton;
    private LineView mLineView;

    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

        mInputX = (EditText) findViewById(R.id.input_x);
        mInputY = (EditText) findViewById(R.id.input_y);
        mDrawButton = (Button) findViewById(R.id.draw_button);

        mLineView = (LineView) findViewById(R.id.line_view);
        mLineView.setColor(Color.GREEN);

        mDrawButton.setOnClickListener(this);
    }

    @Override
    public void onClick (View v) {
        final float x1 = 0;
        final float y1 = 0;
        final float x2 = getValue(mInputX);
        final float y2 = getValue(mInputY);

        mLineView.setCoordinates(x1, y1, x2, y2);
    }

    private static float getValue (EditText text) {
        try {
            return Float.parseFloat(text.getText().toString());
        } catch (NumberFormatException ex) {
            return 0;
        }
    }
}
Kevin Coppock
  • 128,402
  • 43
  • 252
  • 270
  • Wow thanks for writing all those code for me.....By the way, why do you put the invalidate() functnion in setLineColor and setCoordinates? The official document says: "Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future." but I'm not sure what that means. Could you tell me why did you use the invalidate() the way you did? – YueQi Li Sep 15 '13 at 16:33
  • 1
    Because you've modified something that requires the View to redraw itself (the color of the line, or its position). `invalidate()` notifies the view that it should redraw itself (i.e. `onDraw()` will be called) on the next pass. – Kevin Coppock Sep 15 '13 at 16:53
  • Also, if this answer helps you, please upvote and/or mark it as accepted by clicking the check mark to the left of it. :) – Kevin Coppock Sep 15 '13 at 16:55
  • alright thank you very much, I can only accept now 'cause I dont have enough reputation to vote..... – YueQi Li Sep 15 '13 at 18:34
0

Here is the answer of your question. To get the value of the EditText you simply do this in the DrawView method-

public String value;
public int value_int;//This variable should be defined global
EditText text=(EditText)context.findViewById(ID);
value=text.getEditableText().toString();
value_int=Integer.parseInt(value);

Replace this value_int in the canvas.DrawLine(..) method.

Community
  • 1
  • 1
Naddy
  • 2,584
  • 6
  • 23
  • 38
  • What? No. No no. `getText()` gets a CharSequence from resources -- it has nothing to do with a View. Besides, a custom view should never have to reference an external view to draw itself. – Kevin Coppock Sep 15 '13 at 06:43
  • Yes it gets a string. You can easily parse it into an integer. Understand the question first. The coordinates are to be taken from the user. Its an easy way. – Naddy Sep 15 '13 at 06:47
  • No, that is incorrect. This would require a predefined string resource, which would defeat the purpose of it being dynamic to start with. He wants to get parameters from the user FROM an EditText, and pass those to a custom view to be drawn. NOT from resources. – Kevin Coppock Sep 15 '13 at 06:52
  • Thank you Naddy, your answer is helpful too:) – YueQi Li Sep 15 '13 at 18:36
  • I'm a new user so I dont have enough reputation to vote...But I will do that when I get 15 reputation:) – YueQi Li Sep 15 '13 at 21:05