从具有参数的类调用类(无法解析符号)

时间:2017-08-09 14:42:57

标签: java android class

我对编程很陌生,所以可能是我在这里弄错了整个想法,但是......

我有两个类,其他是主类(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);

}

DrawingLines类:

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

3 个答案:

答案 0 :(得分:2)

您的代码不在方法中:

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(this.getApplicationContext());
    draw.onDraw(canvas);

    }//onCreate

}//class main

答案 1 :(得分:0)

  1. 首先,如果你想绘制一些东西,你不必以编程方式强制它。 DrawingLines draw = new DrawingLines(Context context); draw.onDraw(canvas)
  2. 将您的视图DrawingLines置于布局R.layout.activity_main内,并使用findViewById创建变量。
  3. 然后您可以在该视图上执行drawingLines.invalidate(),以便重新绘制视图(将调用方法void onDraw(Canvas canvas)。)

答案 2 :(得分:0)

我创建了一个可以通过手指绘制的简单项目(我将所有在自定义视图中执行绘制的人员放置)

drawView函数:

public class DrawView extends View {

    private final static String LOG = "CUSTOM_LOG";

    private Paint paint;
    private Path path = new Path();

    public DrawView(Context context) {
        this(context, null);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        Log.d(LOG, "init");
        setOnTouchListener(onTouchListener);
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(15);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(LOG, "onDraw");
        canvas.drawPath(path, paint);
    }

    private OnTouchListener onTouchListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            float x = motionEvent.getX();
            float y = motionEvent.getY();
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.d(LOG, "ACTION_DOWN");
                    path.moveTo(x, y);
                    break;
                case MotionEvent.ACTION_MOVE:
                    Log.d(LOG, "ACTION_MOVE");
                    path.lineTo(x, y);
                    break;
                case MotionEvent.ACTION_UP:
                    Log.d(LOG, "ACTION_UP");
                    path.reset();
                    break;
            }
            invalidate();
            return true;
        }
    };
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.gitlab.onreg01.simpledrawexample.DrawView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"/>

</FrameLayout>

MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

project repository