【发布时间】:2017-08-09 14:42:57
【问题描述】:
我对编程很陌生,所以我可能在这里把整个想法搞错了,但是..
我有两个类,另一个是主类(Main),第二个(DrawingLines)只是普通的一个。我正在尝试从主类中的 DrawingLines 调用 onDraw 方法。
我做了一些研究,但我发现的只是使用
DrawingLines draw = new DrawingLines ();
draw.onDraw
我认为这是应该的,但我就是不知道应该使用什么参数。您可以在下面的示例中看到我一直在尝试使用的参数,但 AndroidStudio 只是给出“无法解析符号”。
主类:
public class Main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
final TextView text = (TextView) findViewById(R.id.text);
parent.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent ev) {
text.setText("Touch at " + (int)ev.getX() + ", " + (int)ev.getY());
return true;
}
});
}
DrawingLines draw = new DrawingLines(Context context);
draw.onDraw(canvas);
}
绘图线类:
class DrawingLines extends View {
public DrawingLines(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect pallo = new Rect();
pallo.set(0, 0, canvas.getWidth(), canvas.getHeight() / 2);
Paint blue = new Paint();
blue.setColor(Color.BLUE);
blue.setStyle(Paint.Style.FILL);
canvas.drawRect(pallo, blue);
}
}
【问题讨论】:
-
你想达到什么目的?
-
您将
Context context作为参数传递,您不必改为传递this.getApplicationContext()吗? -
感谢@Asew 帮助了我一点。这只是一个测试,但我希望我的绘图方法在一个单独的类中,这样我的 main 将保持干净且易于遵循。当我完成这项工作时,我将使它从您在屏幕上按下的位置到屏幕上标记的最近点画一条线。