1
所以也许我的问题会如此明显,但我已经尝试了很多试验,其中任何一个都是成功的。所以我正在编写简单的程序在画布上生成分形。但我需要在同一图层上添加一些按钮,并且遇到问题,因为我只能将它们添加到RelativeLayout上,并且由画布隐藏。如何解决它?如果需要,我可以粘贴我的代码。画布和安卓按钮
DrawView.java
package com.example.rafa.fractalsgenerator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawView extends View {
protected Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {} //this public method will be changed in other class
}
下一个:
MainActivity.java
package com.example.rafa.fractalsgenerator;
import android.app.Activity;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
public class MainActivity extends Activity implements View.OnClickListener {
BarnsleyFern fern;
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fern = new BarnsleyFern(this);
fern.setBackgroundColor(Color.WHITE);
button = (Button) findViewById(R.id.button);
setContentView(fern);
}
@Override
public void onClick(View v) {
}
}
最后的java文件:
BarnsleyFern.java
package com.example.rafa.fractalsgenerator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
public class BarnsleyFern extends DrawView {
private double x,y,newx,newy; //coordinates of point of iteration
private double xe,ye; //screen coordinates
private double rand; //generated number from [0;1) to choose corect iteration equation
private final int Nmax = 180000; //number of iteration
public BarnsleyFern(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.argb(127, 255, 0, 255));
x =0;
y= 0; //beginning values
for (int i = 0; i < Nmax; i++) {
rand = Math.random();
if (rand < 0.01) {
x = 0;
y = 0.16 * y;
}
else if (rand < 0.84) {
newx = (0.85 * x) + (0.04 * y);
newy = (-0.04 * x) + (0.85 * y) + 1.6;
x = newx;
y = newy;
}
else if (rand < 0.92) {
newx = (0.2 * x) - (0.26 * y);
newy = (0.23 * x) + (0.22 * y) + 1.6;
x = newx;
y = newy;
}
else {
newx = (-0.15 * x) + (0.28 * y);
newy = (0.26 * x) + (0.24 * y) + 0.44;
x = newx;
y = newy;
}
xe = (float) (70*x + 300);
ye = (float) (70*y +55);
canvas.drawCircle((float) xe, (float) ye,1,paint); //cast needed cause, drawCircle(float,x float y...)
}
}
}
和XML文件:
<RelativeLayout
android:id="@+id/mainView"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Color" />
</RelativeLayout>
更多的细节需要......添加代码... – Proxytype
您可以通过使用'FrameLayout'叠加两种意见。这样你就可以绘制你的视图,android会在上面绘制按钮。 – eduyayo