画布和Android按钮

时间:2015-06-27 20:19:26

标签: android android-canvas

所以也许我的问题会如此明显,但我已经尝试了许多试验,其中任何一个都是成功的。所以我正在编写简单的程序来在画布上生成分形。但是我需要在同一层上添加一些按钮,我有一个问题,因为我只能在RelativeLayout上添加它们,并且被画布隐藏了。怎么解决?如果是nedd我可以粘贴我的代码。

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>

1 个答案:

答案 0 :(得分:0)

将您的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">

    <com.example.rafa.fractalsgenerator.BarnsleyFern
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id:/myBarnsleyFern
    />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Color" />
  </RelativeLayout>

然后将您的活动onCreate代码更改为以下内容:

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainView);

        fern = (BarnsleyFern) findViewById(R.id.myBarnsleyFern);
        fern.setBackgroundColor(Color.WHITE);
        button = (Button) findViewById(R.id.button);

}