The Java code for this is as follows (I don't expect you to read or understand it, just notice it's size and complexity, while scrolling to the end...):
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.graphics;
import android.os.Bundle;
import android.app.Dialog;
import android.content.Context;
import android.graphics.*;
import android.view.MotionEvent;
import android.view.View;
public class ColorPickerDialog extends Dialog {
public interface OnColorChangedListener {
void colorChanged(int color);
}
private OnColorChangedListener mListener;
private int mInitialColor;
private static class ColorPickerView extends View {
private Paint mPaint;
private Paint mCenterPaint;
private final int[] mColors;
private OnColorChangedListener mListener;
ColorPickerView(Context c, OnColorChangedListener l, int color) {
super(c);
mListener = l;
mColors = new int[] {
0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
0xFFFFFF00, 0xFFFF0000
};
Shader s = new SweepGradient(0, 0, mColors, null);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setShader(s);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(32);
mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCenterPaint.setColor(color);
mCenterPaint.setStrokeWidth(5);
}
private boolean mTrackingCenter;
private boolean mHighlightCenter;
@Override
protected void onDraw(Canvas canvas) {
float r = CENTER_X - mPaint.getStrokeWidth()*0.5f;
canvas.translate(CENTER_X, CENTER_X);
canvas.drawOval(new RectF(-r, -r, r, r), mPaint);
canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint);
if (mTrackingCenter) {
int c = mCenterPaint.getColor();
mCenterPaint.setStyle(Paint.Style.STROKE);
if (mHighlightCenter) {
mCenterPaint.setAlpha(0xFF);
} else {
mCenterPaint.setAlpha(0x80);
}
canvas.drawCircle(0, 0,
CENTER_RADIUS + mCenterPaint.getStrokeWidth(),
mCenterPaint);
mCenterPaint.setStyle(Paint.Style.FILL);
mCenterPaint.setColor(c);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(CENTER_X*2, CENTER_Y*2);
}
private static final int CENTER_X = 100;
private static final int CENTER_Y = 100;
private static final int CENTER_RADIUS = 32;
private int floatToByte(float x) {
int n = java.lang.Math.round(x);
return n;
}
private int pinToByte(int n) {
if (n < 0) {
n = 0;
} else if (n > 255) {
n = 255;
}
return n;
}
private int ave(int s, int d, float p) {
return s + java.lang.Math.round(p * (d - s));
}
private int interpColor(int colors[], float unit) {
if (unit <= 0) {
return colors[0];
}
if (unit >= 1) {
return colors[colors.length - 1];
}
float p = unit * (colors.length - 1);
int i = (int)p;
p -= i;
// now p is just the fractional part [0...1) and i is the index
int c0 = colors[i];
int c1 = colors[i+1];
int a = ave(Color.alpha(c0), Color.alpha(c1), p);
int r = ave(Color.red(c0), Color.red(c1), p);
int g = ave(Color.green(c0), Color.green(c1), p);
int b = ave(Color.blue(c0), Color.blue(c1), p);
return Color.argb(a, r, g, b);
}
private int rotateColor(int color, float rad) {
float deg = rad * 180 / 3.1415927f;
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
ColorMatrix cm = new ColorMatrix();
ColorMatrix tmp = new ColorMatrix();
cm.setRGB2YUV();
tmp.setRotate(0, deg);
cm.postConcat(tmp);
tmp.setYUV2RGB();
cm.postConcat(tmp);
final float[] a = cm.getArray();
int ir = floatToByte(a[0] * r + a[1] * g + a[2] * b);
int ig = floatToByte(a[5] * r + a[6] * g + a[7] * b);
int ib = floatToByte(a[10] * r + a[11] * g + a[12] * b);
return Color.argb(Color.alpha(color), pinToByte(ir),
pinToByte(ig), pinToByte(ib));
}
private static final float PI = 3.1415926f;
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX() - CENTER_X;
float y = event.getY() - CENTER_Y;
boolean inCenter = java.lang.Math.sqrt(x*x + y*y) <= CENTER_RADIUS;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mTrackingCenter = inCenter;
if (inCenter) {
mHighlightCenter = true;
invalidate();
break;
}
case MotionEvent.ACTION_MOVE:
if (mTrackingCenter) {
if (mHighlightCenter != inCenter) {
mHighlightCenter = inCenter;
invalidate();
}
} else {
float angle = (float)java.lang.Math.atan2(y, x);
// need to turn angle [-PI ... PI] into unit [0....1]
float unit = angle/(2*PI);
if (unit < 0) {
unit += 1;
}
mCenterPaint.setColor(interpColor(mColors, unit));
invalidate();
}
break;
case MotionEvent.ACTION_UP:
if (mTrackingCenter) {
if (inCenter) {
mListener.colorChanged(mCenterPaint.getColor());
}
mTrackingCenter = false; // so we draw w/o halo
invalidate();
}
break;
}
return true;
}
}
public ColorPickerDialog(Context context,
OnColorChangedListener listener,
int initialColor) {
super(context);
mListener = listener;
mInitialColor = initialColor;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OnColorChangedListener l = new OnColorChangedListener() {
public void colorChanged(int color) {
mListener.colorChanged(color);
dismiss();
}
};
setContentView(new ColorPickerView(getContext(), l, mInitialColor));
setTitle("Pick a Color");
}
}
*********************************************
***************** S T O P *****************
*********************************************
Now here are the core blocks to accomplish the same thing with AI:
But, wait - you might be saying, there is a lot of code under the covers to get these blocks to work - that is true but I don't have to deal with it. That is the point.
Productivity, power, ease of use, not to mention the fun of clickng blocks together - those are the things I am looking for. Of course you have to lay out the color wheel
(which I have admittedly just copied a picture of) in the Screen Designer and make menus to access the color picker dialog box, but that is fairly trivial. Here is the dialog box
in the Screen Designer:
Nothing really complicated there.
Here are some pictures of the output:
What do you think?
---
Scott Ferguson
On Monday, November 25, 2013 2:26:17 PM UTC-6, Josh @ MIT wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/mitappinventortest/3e11723b-b8a0-4cea-b9e2-376ba81082ec%40googlegroups.com.--
You received this message because you are subscribed to a topic in the Google Groups "MIT App Inventor Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mitappinventortest/NwFgN1CI4WU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mitappinventort...@googlegroups.com.
To post to this group, send email to mitappinv...@googlegroups.com.
Hello, I was just shown App Inventor1 earlier this month and quickly adapted to the easy process of programming with blocks. I must say, this program is truly amazing for anyone to learn. I remember way back in highschool when they use to teach turing, Qbasic, or Pascal. Its not always easy for kids to learn and understand logic for the first time, but App Inventor literally puts programming into a visual concept. Reguardless of the hidden programming behind each blocks, the blocks are a great teaching device for explaining how things really do actually flow in a program. Anyone can learn this quick....I know i sure did!
Hi Josh
I believe that the story about my APP will inspire the young students who always lament studies are hard and difficult such as my son.
This is the companion APP I made for My website.
It took me one week to make this APP from scratch. Meaning learning how to make an APP , doing it and posting it in APP store.
I did it to demonstrate by example to my 21 year old son who
is in the University here, who wants to
change his course halfway in to his degree saying subjects are too hard to learn.
Thus I wanted to demonstrate to him that
it is not hard to learn an any new subject. It does not take long either, even
a 58 year old man like myself can do it . What you need is dedication, interest
and set your mind on the task what ever you wanted to do. Then any thing is
possible.
I live in Australia
for the past 22 years and I made the first Sri Lankan Astrology web site about
14 years back which is still in the web. http://jyotisha.00it.com. Every thing
in that website is done by myself. Actually I thought of making it because at
that time there were no Eastern Astrological sites in the web as at that time
Internet was just beginning. And India was not that computer
literate.There were a few western Astrological sites that was all.
So I wanted to show the world that there are other systems of astrology in
eastern countries too.. This APP I made as a companion APP for my web site. It
is not aimed at Sri Lankan or Indian Audience. It's purpose is to give an
introduction of Eastern Astrology to people who have no idea about it .
Wishing you all the best
Lakshman
Hello everybody:
I’m an accountant who likes technology, so I can say without any doubt that AI2 is an incredible tool for those who wants to do something in the Android world (we hope soon in IOs, too), where no previous experience is required: you only need your imagination and your will to learn and create.
In my case, AI2 allows me to bring to modernity a Mexican tradition from Campeche: “Lotería Campechana”, and find out a lot of feedback from users to improve the app; some Friends that are professional developers, can’t believe it was made in AI2.
I’m a big fan and promotor of this platform.
Thanks to all AI2 team, for made this possible.
Regards
Carlos Pérez
Hello let me tell you my history. I am a Colombian psychologist with a specialization in the study of domestic violence. as a thesis I had to made a community activity for the prevention of violence against children. I thought that making a mobile application was very difficult, because I have no knowledge in code writing , but with app inventor I made an application that guides parents in the control of altered behaviors of their children without using violent methods. Actually I promote the use of app inventor among my colleagues to make applications that have a positive impact on the community , because the information we writhe in an application can reach many people.
Regards
Luis c