我有以下java类
public class TouchEventView extends View {
private Paint paint = new Paint();
private Path path = new Path();
public TouchEventView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5f);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float xPos = event.getX();
float yPos = event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(xPos, yPos);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(xPos, yPos);
System.out.println("X: " + xPos + " Y: " + yPos);
break;
case MotionEvent.ACTION_UP:
break; //do nothing, finger up
default:
return false;
}
//schedule a repaint
invalidate();
return true;
}
我希望有一个活动,有自己的布局,并且该布局有几个按钮,以及上面的视图作为活动设计的一部分。
此课程的当前布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_enter_pw"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.islarf6546.gmail.lock_screen_settings.Enter_Pw">
<TextView
android:text="Draw new password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/textView" />
<!--<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>
-->
<Button
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/button3"
android:elevation="0dp" />
</RelativeLayout>
谢谢!