2

嗨,我正在做一个练习项目,可以用我放置的坐标画一条线。屏幕只有文本字段和一个按钮。例如,如果我在这两个文本字段中输入“20”和“30”并单击“绘制”按钮,我希望应用程序在另一个视图中从 (0,0) 到 (20,30) 绘制一条线。

我已经知道如何使用“onDraw()”来画一条线,但我不知道如何将这两个参数传递给 onDraw() 函数。另外,我应该在每次单击绘图按钮时创建一个新视图还是只在一个视图中更改 onDraw() 函数?

谢谢!!!!!!!

4

2 回答 2

5

所以你想要做的是让视图不必担心彼此。你有一个处理画线的EditText视图,两个处理输入的视图,例如,一个提交坐标的按钮。假设您有一个包含这些视图的布局,下面是一个简单的自定义视图示例,您可以使用它来绘制线条:

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
            );
        }
    }
}

除了一个简单的例子,考虑到上面对你的布局所做的假设,你可以如何实现它。

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;
        }
    }
}
于 2013-09-15T07:03:10.497 回答
0

是您问题的答案。要获得值,EditText您只需在方法中执行此操作DrawView-

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);

value_intcanvas.DrawLine(..)方法中替换它。

于 2013-09-15T06:16:37.293 回答