我有问题。我用按钮等创建了一个类DrawingView,并在我称之为
的活动上public class PaintActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DrawingView view=new DrawingView(this);
setContentView(view);
addContentView(view.btnEraseAll, view.params);
}
这是我的配置
class DrawingView extends View {
private Paint brush = new Paint();
private Path path = new Path();
public Button btnEraseAll, btnAccept, btnBack;
public ViewGroup.LayoutParams params;
public DrawingView(Context context) {
super(context);
brush.setAntiAlias(true);
brush.setColor(Color.BLACK);
brush.setStyle(Paint.Style.STROKE);
brush.setStrokeJoin(Paint.Join.ROUND);
brush.setStrokeWidth(22f);
btnEraseAll = new Button(context);
btnEraseAll.setText("Clear");
btnAccept = new Button(context);
btnAccept.setText("Accept");
btnBack = new Button(context);
btnBack.setText("Back");
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
btnEraseAll.setLayoutParams(params);
btnAccept.setLayoutParams(params);
btnBack.setLayoutParams(params);
btnEraseAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
path.reset();
postInvalidate();
}
});
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, brush);
}
public boolean onTouchEvent(MotionEvent event) {
float pointX = event.getX();
float pointY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
break;
default:
return false;
}
postInvalidate();
return false;
}
}
但是如何在活动主页上添加更多按钮(我有3个)? 在这里我添加按钮
addContentView(view.btnEraseAll, view.params);
但我想添加这个按钮
btnAccept, btnBack;
谁能帮助我吗?我想在视图中添加3个按钮。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/monitor"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/MyCustomActionBar">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".WiFiServiceDiscoveryActivity" />
<activity android:name=".PaintActivity" />
</application>
</manifest>
答案 0 :(得分:1)
我重构了一些代码,以便更轻松地进行操作。 我还提取了.xml中的按钮,而不是以编程方式创建它们,因为它更容易。我还将DrawingView提取到一个单独的类中。我也对它进行了测试,以确保它有效。
(wts,)
<强> activity_paint.xml 强>
public class PaintActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paint);
final DrawingView drawingView = (DrawingView) findViewById(R.id.drawing_canvas);
View clearBtn = findViewById(R.id.btn_clear);
clearBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawingView.reset();
}
});
}
}
class DrawingView extends View {
private Paint brush = new Paint();
private Path path = new Path();
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
brush.setAntiAlias(true);
brush.setColor(Color.BLACK);
brush.setStyle(Paint.Style.STROKE);
brush.setStrokeJoin(Paint.Join.ROUND);
brush.setStrokeWidth(22f);
}
public void reset() {
path.reset();
postInvalidate();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, brush);
}
public boolean onTouchEvent(MotionEvent event) {
float pointX = event.getX();
float pointY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
break;
default:
return false;
}
postInvalidate();
return false;
}
}
<强> ids.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.myapplication.DrawingView
android:id="@+id/drawing_canvas"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Clear"/>
<Button
android:id="@+id/btn_accept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Accept"/>
<Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Back"/>
</RelativeLayout>
您也可以从此处下载整个项目:http://expirebox.com/download/130bc35765d0d4704c0e105627077281.html