我有DrawView类,它扩展了View,红线绘制,我希望在点击按钮后,屏幕上会出现另一行(canvas.drawLine(200,500,500,500,paint))。怎么做正确?
DrawView类:
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
paint.setColor(Color.RED);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawLine(200, 500, 200, 800, paint);
}
}
MainActivity类:
public class MainActivity extends AppCompatActivity {
DrawView drawView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
LinearLayout container = (LinearLayout) findViewById(R.id.conteiner);
container.addView(drawView);
Button important = (Button) findViewById(R.id.important);
important.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"Button 1 clicked",
Toast.LENGTH_LONG).show();
}
});
}
}
activity_main.xml中
<?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: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.example.attracti.redline.MainActivity">
<LinearLayout
android:id="@+id/conteiner"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/important">
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Draw"
android:id="@+id/important"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
答案 0 :(得分:1)
列出coords = new ArrayList();
[...]
{{1}}
编写一个名为Coord的类,其中包含x,y,width和height所需的get和set。