i have created a survey layout with a question and then you can select below avg, avg or above avg. a score is at the bottom of the page and it updates on click via listener.
I have a problem with the customer changing their answer and it not removing the pervious value.
so below is 1 and avg is 2 and above is 3. if you click above it adds 3 and then you change your mind and want to pick avg and you click it and it adds 2 but never removes then pervious 3.
help
below32 = (CheckBox) findViewById(R.id.below32);
below32.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (below32.isChecked()) {
sum += 1; double total = 100*sum/69;
result.setText(String.format("%.1f", total) + "%");
avg32.setChecked(false);
above32.setChecked(false);
}
else {
sum -= 1; double total = 100*sum/69;
result.setText(String.format("%.1f", total) + "%");
}
}
});
avg32 = (CheckBox) findViewById(R.id.avg32);
avg32.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (avg32.isChecked()) {
sum += 2; double total = 100*sum/69;
result.setText(String.format("%.1f", total) + "%");
below32.setChecked(false);
above32.setChecked(false);
}
else {
sum -= 2; double total = 100*sum/69;
result.setText(String.format("%.1f", total) + "%");
}
}
});
above32 = (CheckBox) findViewById(R.id.above32);
above32.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (above32.isChecked()) {
sum += 3; double total = 100*sum/69;
result.setText(String.format("%.1f", total) + "%");
below32.setChecked(false);
avg32.setChecked(false);
}
else {
sum -= 3; double total = 100*sum/69;
result.setText(String.format("%.1f", total) + "%");
}
}
});
what do i need to change or do to make it work? all other attempts have failed.
Related
I have a fairly simple calculator with 2 tabs, the tabs appear to be working fine with the first tab calculating as it should, but the calculating for the second tab keeps throwing the catch error.
In the 2nd tab, I have 2 if statements, which checks to see which edittext field contains data. Depending on which contains data to what calculation it does.
Below is the code for when my button is pressed I have put together, it could probably be streamlined but just I'm an amature so go easy on me.
Any advice to get this working please?
calculate.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
if ( tabs.getCurrentTab() == 0) {
try {
if (d.getText().toString().trim().length() == 0) {
a1 = a.getText().toString();
double a2 = Double.parseDouble(a1);
b1 = b.getText().toString();
double b2 = Double.parseDouble(b1);
double sum = (a2 * b2) / 10000;
double total = Math.round((sum) * 10) / 10.0;
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText(String.valueOf("Total Area Allocation = " + total + " ha"));
} else if (d.getText().toString().trim().length() >= 1) {
a1 = a.getText().toString();
double a2 = Double.parseDouble(a1);
b1 = b.getText().toString();
double b2 = Double.parseDouble(b1);
d1 = d.getText().toString();
double d2 = Double.parseDouble(d1);
double sum = (a2 * b2) / 10000;
double sum2 = (a2 * b2) / d2;
double total = Math.round((sum) * 10) / 10.0;
double total2 = Math.round((sum2) * 10) / 10.0;
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText(String.valueOf("Total Area Allocation = " + total + " ha"));
answer2.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer2.setText(String.valueOf("Set break " + total2 + " m"));
}
} catch (Exception e) {
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText("Enter values in input fields");
}
}
if (tabs.getCurrentTab() == 1) {
try {
if (ff.getText().toString().trim().length() == 0) {
aa1 = aa.getText().toString();
double aa2 = Double.parseDouble(aa1);
bb1 = bb.getText().toString();
double bb2 = Double.parseDouble(bb1);
cc1 = cc.getText().toString();
double cc2 = Double.parseDouble(cc1);
dd1 = dd.getText().toString();
double dd2 = Double.parseDouble(dd1);
ee1 = ee.getText().toString();
double ee2 = Double.parseDouble(ee1);
if (ee.getText().toString().trim().length() == 0){
double sum = (aa2 * bb2)/(cc2 - dd2);
double total = Math.round((sum) * 10) / 10.0;
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText(String.valueOf("Total Area Allocation = " + total + " ha"));
}
else if (cc.getText().toString().trim().length() == 0){
double sum = (aa2 * bb2)/ee2;
double total = Math.round((sum) * 10) / 10.0;
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText(String.valueOf("Total Area Allocation = " + total + " ha"));
}
} else if (ff.getText().toString().trim().length() >= 1) {
aa1 = aa.getText().toString();
double aa2 = Double.parseDouble(aa1);
bb1 = bb.getText().toString();
double bb2 = Double.parseDouble(bb1);
cc1 = cc.getText().toString();
double cc2 = Double.parseDouble(cc1);
dd1 = dd.getText().toString();
double dd2 = Double.parseDouble(dd1);
ee1 = ee.getText().toString();
double ee2 = Double.parseDouble(ee1);
ff1 = ff.getText().toString();
double ff2 = Double.parseDouble(ff1);
if (ee.getText().toString().trim().length() == 0){
double sum = (aa2*bb2)/(cc2-dd2);
double sum2 = (sum * 10000) / ff2;
double total = Math.round((sum) * 10) / 10.0;
double total2 = Math.round((sum2) * 10) / 10.0;
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText(String.valueOf("Total Area Allocation = " + total + " ha"));
answer2.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer2.setText(String.valueOf("Set break " + total2 + " m"));
}
else if (cc.getText().toString().trim().length() == 0){
double sum = (aa2 * bb2)/ee2;
double sum2 = (sum * 10000) / ff2;
double total = Math.round((sum) * 10) / 10.0;
double total2 = Math.round((sum2) * 10) / 10.0;
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText(String.valueOf("Total Area Allocation = " + total + " ha"));
answer2.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer2.setText(String.valueOf("Set break " + total2 + " m"));
}
}
} catch (Exception e) {
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText("Enter values in input fields 2");
}
}
}
});
While it's not strictly on topic of why you're running into the exceptions, might I make some suggestions to help improve your code and hopefully reduce problems down the road.
Try to use more helpful variable names, it's hard to know what aa and bb actually are. Try to explain what they actually contain. numOfCows or what have you.
Move your logic into smaller functions. This will help you understand at a glance what the code does, and what other code it calls.
For example, create functions for handling each tab's calculation, and move that logic into there.
calculate.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
if (tabs.getCurrentTab() == 0) {
calculateFirstTab();
} else if (tabs.getCurrentTab() == 1) {
calculateSecondTab();
}
}
});
And lastly, try to reduce duplication. You have a lot of code that does the same thing, but you can make it a lot cleaner by creating small functions to help.
For example, here I have created a function called setAnswer that lets me pass in a TextView, and some text. This will let you set the background color is one place, and update the text. Then, creating another function called setAnswers that lets you pass in the totals, you can now set both answers just like that.
private void setAnswers( double total1, double total2 ){
setAnswer(answer, "Total Area Allocation = " + total1 + " ha");
setAnswer(answer2, "Set break " + total2 + " m");
}
private void setAnswer( TextView view, String text ) {
view.setBackgroundColor(Color.parseColor("#d6ffffff"));
view.setText(text);
}
There are definitely a lot of other ways to improve, but just some suggestions that may help you out.
It works when I convert from Fahrenheit to Celsius but it doesn't work when I try to convert it back from Celsius into Fahrenheit. I've tried for so many times but it still doesn't work for me.
Here's my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.converter_second);
rb_celcius = (RadioButton) findViewById(R.id.radioButton_celcius);
rb_fahrenheit = (RadioButton) findViewById(R.id.radioButton2_fahrenheit);
btn_convert = (Button) findViewById(R.id.button3_convert);
btn_clear = (Button) findViewById(R.id.button5_clear);
edTxt_amount = (EditText) findViewById(R.id.editText_amount);
txtVw_answer = (TextView) findViewById(R.id.textView7_answer);
btn_convert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double dblFahrenheit = 0;
double dblCelcius = (5.0 / 9) * (dblFahrenheit - 32);
double dblConvertedTemp = 0;
double dblFahConversion;
if (v == btn_convert) {
if (rb_celcius.isChecked()) {
String strFah = edTxt_amount.getText().toString();
if (!strFah.isEmpty()) {
{
dblFahrenheit = Double.parseDouble(strFah);
}
if (dblFahrenheit <= 212) {
dblConvertedTemp = (5.0 / 9.0) * (dblFahrenheit - 32);
txtVw_answer.setText("The answer is " + dblConvertedTemp);
} if (rb_fahrenheit.isChecked()) {
String strCel = edTxt_amount.getText().toString();
if (!strCel.isEmpty()) {
{
dblCelcius = Double.parseDouble(strCel);
}
if (dblCelcius <= 100) {
dblFahConversion = dblCelcius * (9.0 / 5.0) + 32;
txtVw_answer.setText("The answer is" + dblFahConversion);
}
}
else if (v == btn_clear){
rg_group.clearCheck();
txtVw_answer.setText("");
}
}
}
}
}
}
});
}
}
I can't say if the logic is correct, but from what I see, the braces are all wrong. This is also not the best way of writing this solution, I will leave that to you.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.converter_second);
rb_celcius = (RadioButton) findViewById(R.id.radioButton_celcius);
rb_fahrenheit = (RadioButton) findViewById(R.id.radioButton2_fahrenheit);
btn_convert = (Button) findViewById(R.id.button3_convert);
btn_clear = (Button) findViewById(R.id.button5_clear);
edTxt_amount = (EditText) findViewById(R.id.editText_amount);
txtVw_answer = (TextView) findViewById(R.id.textView7_answer);
btn_convert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double dblFahrenheit = 0;
double dblCelcius = (5.0 / 9) * (dblFahrenheit - 32);
double dblConvertedTemp = 0;
double dblFahConversion;
if (v == btn_convert) {
if (rb_celcius.isChecked()) {
String strFah = edTxt_amount.getText().toString();
if (!strFah.isEmpty()) {
dblFahrenheit = Double.parseDouble(strFah);
}
if (dblFahrenheit <= 212) {
dblConvertedTemp = (5.0 / 9.0) * (dblFahrenheit - 32);
txtVw_answer.setText("The answer is " + dblConvertedTemp);
}
}
if (rb_fahrenheit.isChecked()) {
String strCel = edTxt_amount.getText().toString();
if (!strCel.isEmpty()) {
dblCelcius = Double.parseDouble(strCel);
}
if (dblCelcius <= 100) {
dblFahConversion = dblCelcius * (9.0 / 5.0) + 32;
txtVw_answer.setText("The answer is" + dblFahConversion);
} else if (v == btn_clear){
rg_group.clearCheck();
txtVw_answer.setText("");
}
}
}
}
}
});
}
Your indentation and use of braces makes it quite hard to see the problem. If I fix the indentation, I get the following code:
public void onClick(View v) {
double dblFahrenheit = 0;
double dblCelcius = (5.0 / 9) * (dblFahrenheit - 32);
double dblConvertedTemp = 0;
double dblFahConversion;
if (v == btn_convert) {
if (rb_celcius.isChecked()) {
String strFah = edTxt_amount.getText().toString();
if (!strFah.isEmpty()) {
{
dblFahrenheit = Double.parseDouble(strFah);
}
if (dblFahrenheit <= 212) {
dblConvertedTemp = (5.0 / 9.0) * (dblFahrenheit - 32);
txtVw_answer.setText("The answer is " + dblConvertedTemp);
}
if (rb_fahrenheit.isChecked()) {
String strCel = edTxt_amount.getText().toString();
if (!strCel.isEmpty()) {
{
dblCelcius = Double.parseDouble(strCel);
}
if (dblCelcius <= 100) {
dblFahConversion = dblCelcius * (9.0 / 5.0) + 32;
txtVw_answer.setText("The answer is" + dblFahConversion);
}
}
else if (v == btn_clear){
rg_group.clearCheck();
txtVw_answer.setText("");
}
}
}
}
}
}
I think it does not work because you check if rb_fahrenheit is checked within the if block that is executed when rb_celcius is checked. Given the fact that these views are radiobuttons, I assume that they reside in the same RadioGroup in which case both radiobuttons have to be checked. This is impossible for a RadioGroup. This explains why the other way around does not work. From the looks of it, the code itself should work.
I have 2 Edittext which only accepts numbers.
I want to make sure that the fields are not empty before the submit button is pressed to stop the application from crashing.
These are the 2 edittext fields i have.
EditText weightText = (EditText)findViewById(R.id.WeightText);
EditText heightText = (EditText)findViewById(R.id.Heighttext);
Code:
public class BMI extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
}
public void calculateHandler(View view) {
// make sure we handle the click of the calculator button
if (view.getId() == R.id.calculate) {
// get the references to the widgets
EditText weightText = (EditText)findViewById(R.id.WeightText);
EditText heightText = (EditText)findViewById(R.id.Heighttext);
TextView resultText = (TextView)findViewById(R.id.resultLabel);
// get the users values from the widget references
float weight = Float.parseFloat(weightText.getText().toString());
float height = Float.parseFloat(heightText.getText().toString());
// calculate the bmi value
float bmiValue = calculateBMI(weight, height);
// interpret the meaning of the bmi value
String bmiInterpretation = interpretBMI(bmiValue);
// now set the value in the result text
resultText.setText(bmiValue + " = " + bmiInterpretation);
}
}
private float calculateBMI (float weight, float height) {
return (float)Math.round((weight / (height * height)) * 100) / 100 ;
}
// interpret what BMI means
private String interpretBMI(float bmiValue) {
if (bmiValue < 16) {
return "Severely underweight";
} else if (bmiValue < 18.5) {
return "Underweight";
} else if (bmiValue < 25) {
return "Normal";
} else if (bmiValue < 30) {
return "Overweight";
} else {
return "Obese";
}
}
}
You can check that your EditTexts are not empty like below:
weightText.getText().length() != 0 && heightText.getText().length() != 0
And you can use this condition inside the onClick(View) method, which is called when your button is clicked like below:
yourButton.setOnClickListener( new OnClickListener(){
public void onClick(){
if(weightText.getText().length() != 0 && heightText.getText().length() != 0){
//do sth
}
});
The second way, you can create TextWatcher which set to both EditTexts and in onTextChanged() you can check that both EditTexts are not empty like below:
private class YourTextWatcher implements TextWatcher{
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
yourButton.setEnabled(weightText.getText().length() != 0 && heightText.getText().length() != 0)
}
#Override
public void afterTextChanged(Editable editable) {
}
}
you can set this TextWatcher for your EditText like below:
weightText.addTextChangedListener(new YourTextWatcher());
heightText.addTextChangedListener(new YourTextWatcher());
Here is code that your Activity should look like:
public class BMI extends Activity {
EditText weightText;
EditText heightText;
TextView resultText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
weightText = (EditText) findViewById(R.id.WeightText);
heightText = (EditText) findViewById(R.id.Heighttext);
resultText = (TextView) findViewById(R.id.resultLabel);
Button button = (Button) findViewById(R.id.calulate);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
calculateHandler();
}
});
}
public void calculateHandler() {
// make sure we handle the click of the calculator button
if (weightText.getText().length() == 0 || heightText.getText().length() == 0) {
return;
}
// get the users values from the widget references
float weight = Float.parseFloat(weightText.getText().toString());
float height = Float.parseFloat(heightText.getText().toString());
// calculate the bmi value
float bmiValue = calculateBMI(weight, height);
// interpret the meaning of the bmi value
String bmiInterpretation = interpretBMI(bmiValue);
// now set the value in the result text
resultText.setText(bmiValue + " = " + bmiInterpretation);
// display toast additionally to example
Toast.makeText(this, bmiValue + " = " + bmiInterpretation, Toast.LENGTH_LONG).show();
}
private float calculateBMI(float weight, float height) {
return (float) Math.round((weight / (height * height)) * 100) / 100;
}
// interpret what BMI means
private String interpretBMI(float bmiValue) {
if (bmiValue < 16) {
return "Severely underweight";
} else if (bmiValue < 18.5) {
return "Underweight";
} else if (bmiValue < 25) {
return "Normal";
} else if (bmiValue < 30) {
return "Overweight";
} else {
return "Obese";
}
}
}
Calculate method
public void calculateHandler() {
// make sure we handle the click of the calculator button
if (weightText.getText().toString().trim().length() == 0 || heightText.getText().toString().trim().length() == 0) {
Toast.makeText(this, "Please fill all field by numbers", Toast.LENGTH_LONG).show();
return;
}
// get the users values from the widget references
float weight = Float.parseFloat(weightText.getText().toString());
float height = Float.parseFloat(heightText.getText().toString());
// calculate the bmi value
float bmiValue = calculateBMI(weight, height);
// interpret the meaning of the bmi value
String bmiInterpretation = interpretBMI(bmiValue);
// now set the value in the result text
resultText.setText(bmiValue + " = " + bmiInterpretation);
// display toast additionally to example
}
weightText.getText().toString().isEmpty();
try like this..
String weight = weightText.getText().toString();
String height = heightText.getText().toString();
if((Integer.parseInt(weight)!=null) && (Integer.parseInt(height)!=null)){
//Not an empty
}
else{
//empty
}
I have the following code and i need to change a little bit the procedure. As it is it works and when i press b1 button returns me on text field et3 a calculation of 3 other text fields. I need to return the calculation on text field et3 without pressing the button. I think i need to add a watcher on text field et4 and when this is not empty then to return me the calculation on et3. The code is this
package com.example.b15_calc;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText et1, et2, et3, et4;
Button b1, b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
et3 = (EditText) findViewById(R.id.editText3);
et4 = (EditText) findViewById(R.id.editText4);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
//step3 : write add functionality.
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
et1.setText("");
et2.setText("");
et3.setText("");
et4.setText("");
}
});
//step3 : ΜΕΤΑΒΛΗΤΕΣ
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String f = et1.getText().toString();
double i = Double.parseDouble(f);
String s = et2.getText().toString();
double j = Double.parseDouble(s);
String w = et4.getText().toString();
double q = Double.parseDouble(w);
double price_gold = 10;
double fpa = 1.23;
double fpol = 0.10;
double fpolam = 999;
double isot = 100;
double sint_ker = 10;
double result1 = (i * price_gold) + (j * 1000) + (q * isot);
double result2 = result1 / 340.75;
int gap;
if (result2 >= fpolam){
double result = (result2 * fpol);
double result3 = ((result2 * sint_ker) * fpa) + result;
if(result3 < 1000)
{
gap = 10;
}
else if(result3 < 5000)
{
gap = 50;
}
else //5000+
{
gap = 100;
}
int total = (int) Math.ceil(result3 / gap) * gap;
String res = String.valueOf(total);
et3.setText(res);
} else{
double result3 = ((result2 * sint_ker) * fpa);
if(result3 < 1000)
{
gap = 10;
}
else if(result3 < 5000)
{
gap = 50;
}
else //5000+
{
gap = 100;
}
int total = (int) Math.ceil(result3 / gap) * gap;
String res = String.valueOf(total);
et3.setText(res);
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
You can use TextWatcher
et4.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// make the calculation here and set the result to et3
String f = et1.getText().toString();
double i = Double.parseDouble(f);
String s = et2.getText().toString();
double j = Double.parseDouble(s);
String w = s.toString();
double q = Double.parseDouble(w);
double price_gold = 10;
double fpa = 1.23;
double fpol = 0.10;
double fpolam = 999;
double isot = 100;
double sint_ker = 10;
double result1 = (i * price_gold) + (j * 1000) + (q * isot);
double result2 = result1 / 340.75;
int gap;
if (result2 >= fpolam){
double result = (result2 * fpol);
double result3 = ((result2 * sint_ker) * fpa) + result;
if(result3 < 1000)
{
gap = 10;
}
else if(result3 < 5000)
{
gap = 50;
}
else //5000+
{
gap = 100;
}
int total = (int) Math.ceil(result3 / gap) * gap;
String res = String.valueOf(total);
et3.setText(res);
} else{
double result3 = ((result2 * sint_ker) * fpa);
if(result3 < 1000)
{
gap = 10;
}
else if(result3 < 5000)
{
gap = 50;
}
else //5000+
{
gap = 100;
}
int total = (int) Math.ceil(result3 / gap) * gap;
String res = String.valueOf(total);
et3.setText(res);
}
}
});
I need to move circles along a circular path for that i am using Object Animator and Path evaluator.All the information is dynamic means it is changing when i receive response so that total number of circles can be changed at runtime.Here is the 1 part of method where all circles are positioning themselves according to their position on screen that is if user release the touch then this method gets called -:
public void slideDownSetToCenter() {
for (int i = 0; i < leftCurrentRunningAnimation.length; i++) {
Logger.i(TAG, "in slide down animation");
if (leftReadyToMove[i]) {
if (leftUpQueue.contains(i)) {
leftUpQueue.remove(i);
}
leftAngle = leftSlice * ++leftAllCirclesAngles[i];
Logger.i("circle leftAngle points of curve down", String.valueOf(leftAllCirclesAngles[i]));
if (leftAngle > leftAngleTop) {
if (i < leftCurrentRunningAnimation.length - 1) {
leftReadyToMove[i + 1] = true; // set next circle to
// move
}
}
Arrays.fill(leftCurrentRunningAnimation, false);
leftCurrentRunningAnimation[i] = true;
Logger.i(TAG, "leftAngle" + leftAngle);
if (leftReadyToMove[i] == true && leftAngle == leftAngleDownOut) {
leftReadyToMove[i] = false;
leftDownStack.add(i);
}
xPosition = (int) (leftCircleCenterX + leftCircleX * Math.cos(leftAngle));
yPosition = (int) (leftCircleCenterY + leftCircleY * Math.sin(leftAngle));
Path = new AnimatorPath();
Path.moveTo(xPosition, yPosition);
Path.lineTo(xPosition, yPosition);
while (true) {
if (leftAngle == leftAngleDownOut) {
break;
}
leftAngle = leftSlice * ++leftAllCirclesAngles[i];
xPosition = (int) (leftCircleCenterX + leftCircleX * Math.cos(leftAngle));
yPosition = (int) (leftCircleCenterY + leftCircleY * Math.sin(leftAngle));
Path.lineTo(xPosition, yPosition);
Logger.i(TAG, "path.........");
}
slideCircleAnimator = ObjectAnimator.ofObject(DynamicCircleSwipeAnimation.this, "leftButtonLocationDynamic", new PathEvaluator(), Path.getPoints().toArray());
slideCircleAnimator.setInterpolator(linearInterpolator);
slideCircleAnimator.setDuration(500);
context.runOnUiThread(new Runnable() {
#Override
public void run() {
slideCircleAnimator.start();
}
});
}
break;
}
}
Here is the Object animator's animation method -:
public void setLeftButtonLocationDynamic(final PathPoint newLoc) {
for (int i = 0; i < leftCurrentRunningAnimation.length; i++) {
if (leftCurrentRunningAnimation[i] == true) {
Logger.i("current button id", String.valueOf(i));
leftArrayOfButtons[i].setTranslationX(newLoc.mX);
leftArrayOfButtons[i].setTranslationY(newLoc.mY);
break;
}
}
}
Here my animation is not happening in correct way that' why i need to pass my view reference from Object.OfObject() method so that i can get it in setLeftButtonLocationDynamic(final PathPoint newLoc,View v).Is anyone has any idea how can i do this?I searched a lot and tried to develop custom class of ObjectAnimator,ValueAnimator and ProperyViewHolder classes but when i copied it from google open source then i get errors in that.Any help is appreciable?
Here to achieve this i changed the approach to move all circles.Previously, i was using ObjectAnimator to move circles one by one and now i am using Animator set for x position and y position.I play both animation together.
Here is the example -:
public void SlideDownFromRightAnimation() {
int rightArrayOfButtonsLength = rightArrayOfButtons.length;
Logger.i(TAG, "queue " + rightUpQueue.size() + "= stack " + rightDownStack.size());
if (rightUpQueue.size() == rtNumberOfButtons) {
Arrays.fill(rightReadyToMove, false);
rightReadyToMove[0] = true;
rightUpQueue.poll();
} else if (rightDownStack.size() == rtNumberOfButtons) {
rightUpQueue.clear();
for (int i = 0; i < rightArrayOfButtonsLength; i++) {
rightUpQueue.add(rightDownStack.pop());
rightAllCirclesAngles[i] = rightCircleUpOutPosition;
}
Arrays.fill(rightReadyToMove, false);
rightReadyToMove[0] = true;
}
for (int i = 0; i < rightArrayOfButtonsLength; i++) {
Logger.i(TAG, "in slide down animation");
if (rightReadyToMove[i]) {
if (rightUpQueue.contains(rightArrayOfButtons[i].getTag())) {
rightUpQueue.remove(i);
}
rightAngle = rightSlice * --rightAllCirclesAngles[i];
Logger.i("circle rightAngle points of curve down", String.valueOf(rightAllCirclesAngles[i]));
if (rightAngle <= rightAngleTop) {
if (i < rightArrayOfButtonsLength - 1) {
rightReadyToMove[i + 1] = true; // set next circle to
// move
}
}
Logger.i(TAG, "rightAngle" + rightAngle);
if (rightAngle <= rightAngleDownOut) {
rightReadyToMove[i] = false;
rightDownStack.add((Integer)rightArrayOfButtons[i].getTag());
rightAllCirclesAngles[i] = rightCircleDownOutPosition;
rightStartAnimation(rightAngle, null, i);
} else {
double rightAngleNext = rightSlice * rightAllCirclesAngles[i];
if (rightAngleNext <= rightAngleDownOut) {
rightReadyToMove[i] = false;
rightDownStack.add((Integer)rightArrayOfButtons[i].getTag());
}
rightStartAnimation(rightAngle, rightAngleNext, i);
}
}
}
}
and here is the right wheels animation function -:
public void rightStartAnimation(Double rightAngle, Double rightAngleNext, int bttnId) {
xPosition = (int) (rightCircleCenterX + rightCircleX * Math.cos(rightAngle));
yPosition = (int) (rightCircleCenterY + rightCircleY * Math.sin(rightAngle));
if (rightAngleNext != null) {
xPosition = (int) (rightCircleCenterX + rightCircleX * Math.cos(rightAngleNext));
yPosition = (int) (rightCircleCenterY + rightCircleY * Math.sin(rightAngleNext));
}
final ObjectAnimator animation1 = ObjectAnimator.ofFloat(rightArrayOfButtons[bttnId], "x", rightArrayOfButtons[bttnId].getX(), xPosition);
animation1.setDuration(0);
animation1.setInterpolator(linearInterpolator);
final ObjectAnimator animation2 = ObjectAnimator.ofFloat(rightArrayOfButtons[bttnId], "y", rightArrayOfButtons[bttnId].getY(), yPosition);
animation2.setDuration(0);
animation2.setInterpolator(linearInterpolator);
final AnimatorSet set = new AnimatorSet();
set.setInterpolator(linearInterpolator);
context.runOnUiThread(new Runnable() {
#Override
public void run() {
set.playTogether(animation1, animation2);
set.start();
}
});
}