Mandelbrot Android应用程序的颜色渐变不起作用

时间:2011-07-27 17:46:25

标签: android colors gradient mandelbrot

我正在尝试为Android设置Mandelbrot设置。 我尝试在Canvas上绘制它。它有效,但我希望有一个颜色渐变。 这是我的代码:

package de.turbofractal;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;


public class FractalView extends View {
   private int n,g,xMax,yMax;
   private float zr,zrh,zr0,zi,zi0,ar,br,ai,bi;
   private int[][] Iterationen = new int[2560][1440];
   private Paint paint;

   public FractalView(Context context) {
      super(context);
      paint = new Paint();
      ar=-2; br=2; ai=-2; bi=2;
   }

   @Override
   protected void onDraw(Canvas canvas) {
       g = 20;
       for(int xx = 1; xx <= xMax; xx++) {
           for(int yy = 1; yy <= yMax; yy++) {
               schirmzupap(ar,br,bi,ai,xx,yy,xMax,yMax);
               n = 1;
               zr0 = zr;
               zi0 = zi;
               while ((n<g) && (zr*zr+zi*zi<4)) {
                   zrh = zr;
                   zr = (zr*zr)-zi*zi+zr0;
                   zi = zrh*zi+zi*zrh+zi0;
                   n++;
               }
               paint.setARGB(255,0,0,Math.round((n/g)*255));  //<--------------------this is where the color gradient should be created. But the programm only draws full blue (0,0,255). Why is that?
               canvas.drawPoint(xx, yy, paint);            
           }

        }
        invalidate();
   }      


   private void schirmzupap(float ax,float bx,float ay,float by,int xschirm,int yschirm,int w,int h) {
       zr = xschirm*(bx-ax)/w+ax;
       zi = yschirm*(ay-by)/h+by;
   } 

   @Override
   public void onSizeChanged(int w, int h, int oldW, int oldH) {
      xMax = w-1;
      yMax = h-1;
   }
}

编辑: 这是eclipse模拟器绘制的方式: http://i53.tinypic.com/2mwfs7c.png

但我希望它像这样画(用Delphi编程): http://i53.tinypic.com/ehg2et.png

1 个答案:

答案 0 :(得分:1)

paint.setARGB(255,0,0,Math.round((n/g)*255));  

setARGB的论据是a lpha,r ed,g reen和b lue - 您只为alpha提供值,蓝色参数。

b参数中,ng都是整数参数,因此当您划分它们时,您将获得整数结果, a双。 (参见“Java Integer Division, How do you produce a double?”)这意味着你只得到结果的整数部分,这不是你想要的。将ng投射到双人身上,它应该有效。