, так что, может быть, моя проблема будет настолько очевидной, но я пробовал много испытаний, и любой из них был успешным. Поэтому я пишу простую программу для создания фрактала на холсте. Но мне нужно добавить некоторую кнопку на этом же слое, и у меня есть проблема, потому что я могу добавлять их только в RelativeLayout и скрываться на холсте. Как его решить? Если nedd, я могу вставить свой код.Canvas and android button
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) {
}
}
Последний ява файл:
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'. Таким образом, вы будете рисовать свое мнение, и андроид нарисует кнопку сверху. – eduyayo