Я создал свой Custom View, на котором рисую, и разместил его на одном экране вместе с кнопками-виджетами. Как мне сделать так, чтобы через нажатия на кнопки происходили какие-то преобразования на Canvas моего View?
MainActivity:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Custom View:
public class CustomView extends View {
private Paint mPaint = new Paint();
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawARGB(80, 102, 204, 255);
canvas.drawRect(getWidth()/2-125, getHeight()/2-25, getWidth()/2-75, getHeight()/2+25, mPaint);
canvas.drawRect(getWidth()/2-25, getHeight()/2-25, getWidth()/2+25, getHeight()/2+25, mPaint);
canvas.drawRect(getWidth()/2+75, getHeight()/2-25, getWidth()/2+125, getHeight()/2+25, mPaint);
}
}
activity_main:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ru.cirq.myapplication1.CustomView
android:id="@+id/myCustomView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="10"
android:gravity="bottom">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="@+id/button1"
android:layout_gravity="bottom"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="@+id/button2"
android:layout_gravity="bottom"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:id="@+id/button3"
android:layout_gravity="bottom"
android:layout_weight="1" />
</LinearLayout>
Я для примера сразу нарисовал три черных квадратика по центру экрана. Допустим, как сделать так, чтобы при нажатии на button1 появлялся первый квадрат, при нажатии на button2 - второй и, аналогично, третий?
Я знаю, что через обработчик onTouchEvent() можно тыкать прям по экрану в области моего CustomView и назначать там нужные действия, но мне нужно реализовать именно через кнопки-виджеты button.