First of all I apologize for my English. I am a french developer.
I want to make a view that helps me to choose a color. A circle with all the color well know and after I listen the touch screen to calculate the color chosen.
I am suprised by the long running task of drawing : about 20 s, I think the use is not ready to wait so much time. I give you my code source.
Thx.
ToToHH
Using java Syntax Highlighting
- package fr.android.colorpicker;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.PointF;
- import android.util.Log;
- import android.view.View;
- public class CircleView extends View {
- private final static int WIDTH = 280;
- private final static int HEIGHT = 280;
- public CircleView(Context context) {
- super(context);
- this.setDrawingCacheEnabled(true);
- }
- private ARGB getRGBAAtPoint(PointF point) {
- long t = System.currentTimeMillis();
- float radius = CircleView.WIDTH / 2;
- float centreX = CircleView.WIDTH / 2;
- float centreY = CircleView.HEIGHT / 2;
- float deltaX = point.x - centreX;
- float deltaY = point.y - centreY;
- float distSquared = deltaX * deltaX + deltaY * deltaY;
- if (distSquared > radius * radius) {
- return null;
- } else {
- float dist = (float) Math.sqrt(distSquared);
- float hue = (float) (Math.atan2(deltaY, deltaX) * 180.0f / Math.PI);
- float saturation = dist / radius;
- HSV hsv = new HSV(hue, saturation, 1);
- float alpha = Math.abs(dist - radius) < 1.0f ? Math.abs(dist - radius) : 1.0f;
- Log.i("CIRCLEVIEW_getRGBAAtPoint", "time to draw: " + (System.currentTimeMillis() - t) / 1000);
- t = System.currentTimeMillis();
- ARGB argb = new ARGB(alpha, hsv.toRGB());
- Log.i("CIRCLEVIEW_hsv.toRGB", "time to draw: " + (System.currentTimeMillis() - t) / 1000);
- return argb;
- }
- }
- @Override
- public void draw(Canvas canvas) {
- // super.draw(canvas);
- long t = System.currentTimeMillis();
- for (int x = 0; x < CircleView.WIDTH; x++) {
- for (int y = 0; y < CircleView.HEIGHT; y++) {
- ARGB argb = this.getRGBAAtPoint(new PointF((float) x, (float) y));
- if (argb != null) {
- int color = ((int) (argb.getAlpha() * 255.f) << 24)
- | ((int) (argb.getRGB().getB() * 255.f) << 16)
- | ((int) (argb.getRGB().getG() * 255.f) << <img src="http://www.anddev.org/images/smilies/cool.png" alt="8)" title="Cool" />
- | ((int) (argb.getRGB().getR() * 255.f));
- Paint paint = new Paint();
- paint.setColor(color);
- canvas.drawPoint((float)x, (float)y, paint);
- }
- }
- }
- Log.i("CIRCLEVIEW_DRAW", "time to draw: " + (System.currentTimeMillis() - t) / 1000);
- }
- }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


