Color Choiser

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Color Choiser

Postby totohh » Tue May 04, 2010 9:50 am

Hi,

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


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package fr.android.colorpicker;
  3.  
  4.  
  5.  
  6. import android.content.Context;
  7.  
  8. import android.graphics.Canvas;
  9.  
  10. import android.graphics.Paint;
  11.  
  12. import android.graphics.PointF;
  13.  
  14. import android.util.Log;
  15.  
  16. import android.view.View;
  17.  
  18.  
  19.  
  20. public class CircleView extends View {
  21.  
  22.         private final static int WIDTH = 280;
  23.  
  24.         private final static int HEIGHT = 280;
  25.  
  26.  
  27.  
  28.         public CircleView(Context context) {
  29.  
  30.                 super(context);
  31.  
  32.                 this.setDrawingCacheEnabled(true);
  33.  
  34.         }
  35.  
  36.  
  37.  
  38.         private ARGB getRGBAAtPoint(PointF point) {
  39.  
  40.  
  41.  
  42.                 long t = System.currentTimeMillis();
  43.  
  44.                
  45.  
  46.                 float radius = CircleView.WIDTH / 2;
  47.  
  48.                 float centreX = CircleView.WIDTH / 2;
  49.  
  50.                 float centreY = CircleView.HEIGHT / 2;
  51.  
  52.  
  53.  
  54.                 float deltaX = point.x - centreX;
  55.  
  56.                 float deltaY = point.y - centreY;
  57.  
  58.                 float distSquared = deltaX * deltaX + deltaY * deltaY;
  59.  
  60.  
  61.  
  62.                 if (distSquared > radius * radius) {
  63.  
  64.                         return null;
  65.  
  66.                 } else {
  67.  
  68.                         float dist = (float) Math.sqrt(distSquared);
  69.  
  70.                         float hue = (float) (Math.atan2(deltaY, deltaX) * 180.0f / Math.PI);
  71.  
  72.                         float saturation = dist / radius;
  73.  
  74.                         HSV hsv = new HSV(hue, saturation, 1);
  75.  
  76.                         float alpha = Math.abs(dist - radius) < 1.0f ? Math.abs(dist - radius) : 1.0f;
  77.  
  78.  
  79.  
  80.                         Log.i("CIRCLEVIEW_getRGBAAtPoint", "time to draw: " + (System.currentTimeMillis() - t) / 1000);
  81.  
  82.                         t = System.currentTimeMillis();
  83.  
  84.                         ARGB argb = new ARGB(alpha, hsv.toRGB());
  85.  
  86.                         Log.i("CIRCLEVIEW_hsv.toRGB", "time to draw: " + (System.currentTimeMillis() - t) / 1000);
  87.  
  88.                         return argb;
  89.  
  90.                 }
  91.  
  92.         }
  93.  
  94.  
  95.  
  96.         @Override
  97.  
  98.         public void draw(Canvas canvas) {
  99.  
  100.                 // super.draw(canvas);
  101.  
  102.                 long t = System.currentTimeMillis();
  103.  
  104.  
  105.  
  106.                 for (int x = 0; x < CircleView.WIDTH; x++) {
  107.  
  108.                         for (int y = 0; y < CircleView.HEIGHT; y++) {
  109.  
  110.                                 ARGB argb = this.getRGBAAtPoint(new PointF((float) x, (float) y));
  111.  
  112.                                 if (argb != null) {
  113.  
  114.                                         int color = ((int) (argb.getAlpha() * 255.f) << 24)
  115.  
  116.                                                         | ((int) (argb.getRGB().getB() * 255.f) << 16)
  117.  
  118.                                                         | ((int) (argb.getRGB().getG() * 255.f) << <img src="http://www.anddev.org/images/smilies/cool.png" alt="8)" title="Cool" />
  119.  
  120.                                                         | ((int) (argb.getRGB().getR() * 255.f));
  121.  
  122.                                         Paint paint = new Paint();
  123.  
  124.                                         paint.setColor(color);
  125.  
  126.                                         canvas.drawPoint((float)x, (float)y, paint);
  127.  
  128.                                 }
  129.  
  130.                         }
  131.  
  132.                 }
  133.  
  134.  
  135.  
  136.                 Log.i("CIRCLEVIEW_DRAW", "time to draw: " + (System.currentTimeMillis() - t) / 1000);
  137.  
  138.         }
  139.  
  140. }
  141.  
  142.  
  143.  
  144.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
totohh
Freshman
Freshman
 
Posts: 2
Joined: Tue May 04, 2010 9:44 am
Location: Sophia Antipolis - France

Top

Postby MichaelEGR » Tue May 04, 2010 4:51 pm

Hi.. This can be made more efficient. Right off the bat be very mindful of creating new objects in Java especially in processing loops.

Also you can draw the image once into a Bitmap and simply redraw the Bitmap in the "onDraw" method of your View. You should use "onDraw" instead of "draw" also don't forget to call super.onDraw(canvas).
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Postby totohh » Tue May 04, 2010 7:16 pm

MichaelEGR wrote:Also you can draw the image once into a Bitmap...


Thx for your answer. I've also already test this java code but same result.
Maybe I've made a mistake ...

I think it is because I instantiate too much objects. So I've tried this morning to do that with int[] arrays but nevertheless my view takes about 10s to be loaded. What can I do please ?
totohh
Freshman
Freshman
 
Posts: 2
Joined: Tue May 04, 2010 9:44 am
Location: Sophia Antipolis - France

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest