2015년 9월 15일 화요일

[Guide] Floating in circular boundary (or rectangular) with accelerometer sensor

This guide is about floating, moving a point in coordinate system.

It may be useful to make spirit level (bubble level) or magic 8 ball and so on 

Just use device's accelerometer sensor to moving a center point.

I wrote android custom view which implements SensorEventListener.

Do some initialization (measuring screen size, set boundary size, assign values...) first.

Draw the x, y axis and boundary and little circle.

In end of the onDraw(), invalidate() makes little circle keep moving.



Important formula to make little circle inside circular boundary is below.



add the sensor's value to x, y coordinate (xCon, yCon) and check it is out of the circular boundary.
If it is change the value with the Formula

x = cos(atan2(y, x)) * CIRCULAR_BOUDNARY_RADIUS
y = sin(atan2(y, x)) * CIRCULAR_BOUDNARY_RADIUS

You can select the circle to float on water or roll on ground just change addition <-> subtraction.

- For simulating object floating on water, against gravity
xCon += mSensorX;
yCon -= mSensorY;

- For simulating object rolling on ground, adjust to gravity
xCon -= mSensorX;
yCon += mSensorY;

Also you can change boundary shape easily
For rectangle boundary





onSensorChanged() I just add sensor values(weighted) to center point x, y and it seems to be quite enough to do rough simulation.

Whole source code of my custom view is like below.




Hope this is helpful!

Thanks

[GUIDE] Customize Lollipop's official Clock application



I am a person who needs many many multiple alarms to wake up early in the morning.
Every clock/alarm applications I tried has no feature that turning on or off all alarms at once.
So I decided to customize google's official android clock app.

Let's get it started.

### Needed #####
google's clock app source (I use Lollipop's source for my device)
buttons icon image

### Steps #####

1) download source from googlesource.com repository
android system clock application's package name is "DeskClock".
You can clone the source like below


git clone -b lollipop-release https://android.googlesource.com/platform/packages/apps/DeskClock

2. import it & remove errors
When you import the project to your IDE, you will get thousand of import errors.
Check below
SDK Version
- Make sure that you have lollipop sdk and check "Build Target" in "Project Properties"
AndroidManifest
- Specify min/targetSdkVersion, if it is not in AndroidManifest.xml
- There is "DEVICE_POWER" permission because it is android system app but we can't build system application so just remove it. (In my usage, it seems to be no problem)
Library
- Put android support-library jar file and add it to project build path.

3. Add button icons

- add ic_all_on.png, ic_all_off.png as drawable


4. Edit the sources
There is "DeskClockFragmet" class which is parent of alam/timer/stopwatch/clock's Fragment class.
It uses common layout "desk_clock.xml". It has three buttons (Fab, sub buttons) in the bottom of the screen and Alarm Fragment does not use and just hide sub buttons.
Which means we can easily add buttons to that place.
setLeftRightButtonAppearance() in AlarmClockFragment.java

set mLeft/RightButton enabled and change visibilty to VISIBLE, set button image as drawable we added.

Override onLeftButtonClick/onRightButtonClick() from parent (DeskClockFragment.class) and call switchAlarms() what we will implement.

+ I just hide menu button in bottom-right just in alarm tab because right and menu button are overlayed.

onCreateView() in AlarmClockFragment.java

There is AlarmItemAdapter sub class in AlarmClockFragment.java which extends CursorAdapter.
We have to implement something that change actual alarm data in inner database and change views depend on each alarm's on/off value.
I just added a method "swichAlarms" into this sub class to do that.

boolean enabled value is passed as parameter (on : true / off : false)
Get every alarm's view and change the view when the alarm on/off status has to be changed

Also there's a way to get alarms llist in Alarm.java that is getAlarms() method.
I use it to get alarms list a alarm's enabled value is different.
And change alarms enabled value with asyncUpdateAlarm() method in AlarmClockFragment.java


That's all!
easy? 






Now I feel comfortable to sleep lately in the evening and wake up early in the morning 
Hope this thread is helpful to someone like me.
I attached project archive and apk.
Enjoy it!