samedi 27 juin 2015

Canvas and android button

so maybe my problem will be so obvious but I've tried many trials and any of them were succesfull. So I'm writing simple program to generate fractal on canvas. But I need to add some button on this same layer and I have a problem because I can only add them on RelativeLayout and there are being hidden by canvas. How to solve it? If nedd I can paste my code.

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
 }

The next one:

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) {
  }
 }

The last java file:

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...)
    }

   }

  }

And the xml file:

<RelativeLayout
android:id="@+id/mainView"
xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
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>

Aucun commentaire:

Enregistrer un commentaire