gpt4 book ai didi

android - 如何堆叠 Canvas 和按钮?

转载 作者:行者123 更新时间:2023-11-29 00:44:35 25 4
gpt4 key购买 nike

我想编写以下应用程序。有一个 Canvas 和一个垂直堆叠在 LinearView 中的 Button。当按钮第一次在 Canvas 中绘制圆时按下,然后如果再次按下圆圈消失。圆圈必须出现以其空间为中心。

有什么想法吗?

谢谢,

JG

最佳答案

这应该可行

自定义 View 类

public class DrawView extends View {
private Canvas viewCanvas;

public DrawView(Context context) {
super(context);
setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT
,LinearLayout.LayoutParams.FILL_PARENT));
}

public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.RED);
canvas.drawCricle(getWidth()/2,getHeight()/2,50,null);
viewCanvas = canvas;
}

public clearCircle(){
viewCanvas.drawColor(Color.BLACK);
}
}

Activity 类应该是这样的

public class KeyboardTopDemo extends Activity {
private FrameLayout container;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ss);

container = (FrameLayout)findViewById(R.id.sc);
container.addView(new DrawView(this));
}

public void clearHandler(View target){
container.getChildAt(0).clearCircle();
}
}

这是布局xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="0dip" android:id="@+id/sc"
android:scrollbars="horizontal" android:layout_weight="1.0">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:src="@drawable/chips"/>
</FrameLayout>
<Button android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Clear" android:onClick="clearHandler"/>
</LinearLayout>

关于android - 如何堆叠 Canvas 和按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7140658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com