Xamarin Android ValueAnimator.OfInt() not work - android

I've try to animate int value like this
intAnim = ValueAnimator.OfInt(0, 100);
intAnim.AddUpdateListener(this);
intAnim.SetDuration(450);
intAnim.Start();
And write it in console while updating
public void OnAnimationUpdate(ValueAnimator animation)
{
Console.WriteLine("e.Animation.CurrentPlayTime " + animation.CurrentPlayTime + "/ " + animation.Duration);
Console.WriteLine("e.Animation.AnimatedValue " + animation.AnimatedValue);
}
The value does not update from start value it's always set to target value so message in console will be like
e.Animation.CurrentPlayTime 0/ 450
e.Animation.AnimatedValue 100
Does not sure what is wrong..

Related

Text UI not updating on *mobile* game Unity

I'm setting up a game which will be played on Android. For some reason, the UI Text is not updating on the mobile phone whilst it works well when the game is played through Unity.
The process is as follows. The users selects a mini-game he would like to play, according to his score, he will get that amount in in game money.
The starting cash amount is 20, and after playing the game on Unity one can notice that it updates (even through the inspector).
Unity Game Screen
On the other hand, on mobile it doesn't update and stays the initial amount of 20.
Is this a known bug, or did I do something wrong?
Code as requested:
This is the game controller were the UI element updates:
public void helpAtHome()
{
income = (5*globalMinigameScoreCounter) + income;
Debug.Log("here");
bankInvestment = investmentRate * income;
money += income - bankInvestment;
health -= 2;
hunger -= 2;
// bankInvestmentText.text = "€ " + bankInvestment;
// healthText.text = "Health: " + health;
// hungerText.text = "Hunger: " + hunger;
// moneyText.text = "Cash: " + money.ToString();
globalMinigameScoreCounter = 0;
}
Whilst this is the piece of code were the game is called and the values updated:
public void countDown(float timer){
int time = (int) timer;
timerText.text = time.ToString();
if(time == 0){
gameController.helpAtHome();
gameController.bankInvestmentText.text = "€ " + gameController.bankInvestment;
gameController.healthText.text = "Health: " + gameController.health;
gameController.hungerText.text = "Hunger: " + gameController.hunger;
gameController.moneyText.text = "Cash: " + gameController.money.ToString();
gameController.globalMinigameScoreCounter = 0;
SceneManager.LoadScene(1);
}
}
Why are you calling SceneManager.LoadScene(1); ?
I guess you are updating text values and then opening the scene again. If you are doing so when a scene is loaded it is set to its default value. Still want to load the scene than try using PlayerPrefs.
PlayerPrefs.SetInt("health",health);
gameController.healthText.text = "Health: " + PlayerPrefs.GetInt("health");

when generating a number, only gives 0

when i generate a problem or number in this case, it seems to always come out as 0, when it should be generating a number between 0 and the value AD given. which in this case is 0 through 11, but only generates a 0 every time. for some reason the AD int is returning as 1 when i log it to the debug console. any help?
Double d = Math.random() * Max1;
O1 = d.intValue();
Log.d("BasicsPractice.java","the GenerateOperation function generated : " + O1 + " : as the number to determin the operation to be built");
if(O1 == 0) {
if(AddP == true) {
int AD = DM.AddDiff + 1;
Double d1 = Math.random() * AD;
A1 = d1.intValue();
Log.d("BasicsPractice.java","the first Addition number generated was: " + A1);
Double d5 = Math.random() * AD;
A2 = d5.intValue();
Log.d("BasicsPractice.java","the second Addition number generated was: " + A2);
Output = A1 + " + " + A2;
Log.d("BasicsPractice.java","the outputted problem was: " + Output);
OutputToProblemView();
}
Math.random() returns a number from 0.0 to 1.0. So you need to convert it into int.
Try this out.
Math.floor(Math.random()*12);
This will generate numbers between 0 to 11.

ValueAnimator doesn't trigger properties methods

I had been debugged the following code for whole day, and have no idea why it doesn't work as expected.
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.PropertyValuesHolder;
import com.nineoldandroids.animation.ValueAnimator;
private void animateThumbCoorToMatchCurrentIndixes() {
float newThumbLeftCoor = this.indexToCoor(this.thumbLeftIndex);
float newThumbRightCoor = this.indexToCoor(this.thumbRightIndex);
PropertyValuesHolder thumbLeftPropertyValuesHolder = PropertyValuesHolder.ofFloat("thumbLeftCoor", this.thumbLeftCoor, newThumbLeftCoor);
PropertyValuesHolder thumbRightPropertyValuesHolder = PropertyValuesHolder.ofFloat("thumbRightCoor", this.thumbRightCoor, newThumbRightCoor);
Log.i("CHEOK", "Animate " + this.thumbLeftCoor + " -> " + newThumbLeftCoor);
Log.i("CHEOK", "Animate " + this.thumbRightCoor + " -> " + newThumbRightCoor);
ValueAnimator valueAnimator = ObjectAnimator.ofPropertyValuesHolder(this, thumbLeftPropertyValuesHolder, thumbRightPropertyValuesHolder);
valueAnimator.setDuration(getResources().getInteger(android.R.integer.config_shortAnimTime));
valueAnimator.setRepeatCount(0);
valueAnimator.setInterpolator(new DecelerateInterpolator());
Log.i("CHEOK", "Animation duration = " + valueAnimator.getDuration());
valueAnimator.start();
}
// Do not remove this code. It is used for reflection call for animation.
#SuppressWarnings("unused")
private void setThumbLeftCoor(float thumbLeftCoor) {
this.thumbLeftCoor = thumbLeftCoor;
Log.i("CHEOK", "SET thumbLeftCoor " + thumbLeftCoor);
this.postInvalidate();
}
// Do not remove this code. It is used for reflection call for animation.
#SuppressWarnings("unused")
private void setThumbRightCoor(float thumbRightCoor) {
this.thumbRightCoor = thumbRightCoor;
Log.i("CHEOK", "SET thumbRightCoor " + thumbRightCoor);
this.postInvalidate();
}
My console log is as follow :
Animate 636.0985 -> 0.0
Animate 679.27057 -> 578.0
Animation duration = 200
However, methods setThumbLeftCoor and setThumbRightCoor never get triggered.
Anyone have idea why it happens so?
All the while, by having setThumbLeftCoor and setThumbRightCoor as private method work for my case.
Just that recently, a child class is being derived from the above class.
To make the above code work as it is, changing setThumbLeftCoor and setThumbRightCoor to public method is required. (Even protected won't work)
My guess is, this is due to limitation of "reflection"

Android Beacon sample app

I trying to build a app on beacons. so I got a sample code from here.
I have run that code and as soon as I entered into range of beacon the app is crashed and error in the logcat is as follow:
06-20 17:48:24.256: E/AndroidRuntime(18104): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Please help me out.
I checked the code and the error lies in between these lines.
iBeaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
if (iBeacons.size() > 0) {
double distance = iBeacons.iterator().next().getAccuracy();
DecimalFormat decimalFormat = new DecimalFormat("##.##");
double distanceFormatted = Double.valueOf(decimalFormat.format(distance));
TextView distanceTextView = (TextView) findViewById(R.id.am_tv_distance);
distanceTextView.setText(String.valueOf(distanceFormatted) + " m");
}
}
});
You need to perform UI changes in the UI thread:
iBeaconManager.setRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
if (iBeacons.size() > 0) {
double distance = iBeacons.iterator().next().getAccuracy();
DecimalFormat decimalFormat = new DecimalFormat("##.##");
double distanceFormatted = Double.valueOf(decimalFormat.format(distance));
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView distanceTextView = (TextView) findViewById(R.id.am_tv_distance);
distanceTextView.setText(String.valueOf(distanceFormatted) + " m");
}
});
}
}
});
For the CalledFromWrongThreadException,
write a function and call that like:
private void toastToDisplay(final String line) {
runOnUiThread(new Runnable() {
public void run() {
//Toast.makeText(YourActivity.this, "" + line,
// Toast.LENGTH_LONG).show();
TextView yourTextView = (TextView) findViewById(R.id.your_tv);
yourTextView .setText(line);
}
});
}
You can edit that function with your required distanceTextView or other views if you want.
What happens there is that the Service is running on the background thread in your application, so when you directly call an operation that would update the UI, it conflicts with the thread you are in while writing that code.
So If the current thread is not the UI thread, the action is posted to the event queue of the UI thread by using runOnUiThread
What exactly is the meaning of "I am getting only distance not range" ?
...for the iBeacons.iterator().next().getAccuracy();, part.. you can collect the beacon details by using a for each loop like:
for (IBeacon iBeacon : iBeacons) {
String line = "Major: " + iBeacon.getMajor()
+ "Minor: " + iBeacon.getMinor()
+ "Accuracy in m(s): "
+ iBeacon.getAccuracy()
+ "BluetoothAddress: "
+ iBeacon.getBluetoothAddress()
+ "Proximity: " + iBeacon.getProximity()
+ "ProximityUuid: "
+ iBeacon.getProximityUuid() + "Rssi: "
+ iBeacon.getRssi() + "TxPower: "
+ iBeacon.getTxPower();
toastToDisplay(line);
}
In case you haven't already seen it, this Android iBeacon Library sample application is a good reference.

How to retrieve getUpdated or getNotebookGuid information in evernote api

In android, with SearchNotes.java example and a very simple add from me:
Log.d("something", title + " "+ note.getUpdated() + " "+ note.getNotebookGuid());
Api returns only 0 or null! Did I missed something?
More details of the context of the code:
try{
mEvernoteSession.getClientFactory().createNoteStoreClient()
.findNotesMetadata(filter, offset, pageSize, spec, new OnClientCallback<NotesMetadataList>() {
#Override
public void onSuccess(NotesMetadataList data) {
Toast.makeText(getApplicationContext(), R.string.notes_searched, Toast.LENGTH_LONG).show();
removeDialog(DIALOG_PROGRESS);
for(NoteMetadata note : data.getNotes()) {
String title = note.getTitle();
notesNames.add(title);
Log.d("something", title + " "+ note.getUpdated() + " "+ note.getNotebookGuid());
}
When you call NoteStore#findNotesMetadata, you can specify what it returns with NotesMetadetaResultSpec.
http://dev.evernote.com/documentation/reference/NoteStore.html#Fn_NoteStore_findNotesMetadata
http://dev.evernote.com/documentation/reference/NoteStore.html#Struct_NotesMetadataResultSpec
Make sure you set includeUpdated and includeNotebookGuid to true.

Categories

Resources