画布和Android布局按钮

时间:2015-06-28 11:47:52

标签: android canvas

抱歉我的帖子翻了一倍,但是昨天我写了关于我的问题,只有2个答案,但是其中任何一个对我都有帮助,之后我想了几个小时怎么解决,我完全没有想法为什么它不起作用。我预测非常明显,但Android技术对我来说是全新的,我不知道如何解决它。所以我画了一张巴恩斯利分形画布。我需要一个按钮,但是当我的程序正在制作时,我只能看到带有分形的画布,没有按钮。这是我的代码:

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.util.AttributeSet;
import android.view.View;

public class DrawView extends View {

protected Paint paint = new Paint();

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

@Override
public void onDraw(Canvas canvas) {}  //this public method will be changed      in other class
 }

下一个java文件:

BarnsleyFern.java

package com.example.rafa.fractalsgenerator;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;

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, AttributeSet attrs) {
    super(context, attrs);
}

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

}

 }

和活动文件:

MainActivity.java


    package com.example.rafa.fractalsgenerator;

    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;


    public class MainActivity extends Activity {

BarnsleyFern fern;
Button button;

@Override
public View findViewById(int id) {
    return super.findViewById(id);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

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


    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            button.setText("clicked");
        }
    });
 }
}

最后我的xml文件:

<RelativeLayout
android:id="@+id/activity_main"
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" />

<com.example.rafa.fractalsgenerator.BarnsleyFern
    android:id="@+id/myBarnsleyFern"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

</RelativeLayout>

也许另外你可以告诉我如何以更优雅和更复杂的方式解决它。

0 个答案:

没有答案