我对编程很陌生,所以可能是我在这里弄错了整个想法,但是......
我有两个类,其他是主类(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);
}
}
答案 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)
DrawingLines draw = new DrawingLines(Context context);
draw.onDraw(canvas)
DrawingLines
置于布局R.layout.activity_main
内,并使用findViewById
创建变量。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);
}
}