Improve Java code to be executed faster on Android The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceIs Java “pass-by-reference” or “pass-by-value”?Does a finally block always get executed in Java?Close/hide the Android Soft KeyboardWhy is the Android emulator so slow? How can we speed up the Android emulator?Improve INSERT-per-second performance of SQLite?Creating a memory leak with JavaWhy are elementwise additions much faster in separate loops than in a combined loop?Why is it faster to process a sorted array than an unsorted array?Is < faster than <=?Proper use cases for Android UserManager.isUserAGoat()?

Why doesn't a hydraulic lever violate conservation of energy?

How do spell lists change if the party levels up without taking a long rest?

Simulating Exploding Dice

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

Does Parliament need to approve the new Brexit delay to 31 October 2019?

Is every episode of "Where are my Pants?" identical?

Why did Peik Lin say, "I'm not an animal"?

Are spiders unable to hurt humans, especially very small spiders?

Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?

Example of compact Riemannian manifold with only one geodesic.

How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time

Single author papers against my advisor's will?

Huge performance difference of the command find with and without using %M option to show permissions

When did F become S? Why?

How many cones with angle theta can I pack into the unit sphere?

Why can't devices on different VLANs, but on the same subnet, communicate?

What is the padding with red substance inside of steak packaging?

My body leaves; my core can stay

Is 'stolen' appropriate word?

Deal with toxic manager when you can't quit

How to read αἱμύλιος or when to aspirate

Homework question about an engine pulling a train

Can the DM override racial traits?

How to make Illustrator type tool selection automatically adapt with text length



Improve Java code to be executed faster on Android



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceIs Java “pass-by-reference” or “pass-by-value”?Does a finally block always get executed in Java?Close/hide the Android Soft KeyboardWhy is the Android emulator so slow? How can we speed up the Android emulator?Improve INSERT-per-second performance of SQLite?Creating a memory leak with JavaWhy are elementwise additions much faster in separate loops than in a combined loop?Why is it faster to process a sorted array than an unsorted array?Is < faster than <=?Proper use cases for Android UserManager.isUserAGoat()?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















My Java code is executing really slowly in my Android device. I think it's because I made it all in one class. I need some recommendations for how to improve my code, maybe by dividing it into different classes. How you would do it?



public class MainActivity extends AppCompatActivity 
View white;
Animation downtoup;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Remove notification bar
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

//MovingScanSquare
white = findViewById(R.id.white);
downtoup = AnimationUtils.loadAnimation(this, R.anim.downtoup);

white.setVisibility(View.VISIBLE);
downtoup = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.RELATIVE_TO_PARENT, 1.1f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.f);
downtoup.setDuration(2200);
downtoup.setRepeatCount(-1);
downtoup.setRepeatMode(Animation.REVERSE);
downtoup.setInterpolator(new LinearInterpolator());
white.setAnimation(downtoup);


//movingscreen
final ImageView backgroundOne = findViewById(R.id.background_one);
final ImageView backgroundTwo = findViewById(R.id.background_two);

final ValueAnimator animator = ValueAnimator.ofFloat(0.0f, -1.0f);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.setDuration(3000L);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
@Override
public void onAnimationUpdate(ValueAnimator animation)
final float progress = (float) animation.getAnimatedValue();
final float width = backgroundOne.getWidth();
final float translationX = width * progress;
backgroundOne.setTranslationX(translationX);
backgroundTwo.setTranslationX(translationX + width);

);
animator.start();











share|improve this question
























  • Splitting the code in several classes could improve readability, but I fail to see how it would improve performance.

    – Maurice Perry
    Mar 22 at 5:46

















0















My Java code is executing really slowly in my Android device. I think it's because I made it all in one class. I need some recommendations for how to improve my code, maybe by dividing it into different classes. How you would do it?



public class MainActivity extends AppCompatActivity 
View white;
Animation downtoup;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Remove notification bar
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

//MovingScanSquare
white = findViewById(R.id.white);
downtoup = AnimationUtils.loadAnimation(this, R.anim.downtoup);

white.setVisibility(View.VISIBLE);
downtoup = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.RELATIVE_TO_PARENT, 1.1f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.f);
downtoup.setDuration(2200);
downtoup.setRepeatCount(-1);
downtoup.setRepeatMode(Animation.REVERSE);
downtoup.setInterpolator(new LinearInterpolator());
white.setAnimation(downtoup);


//movingscreen
final ImageView backgroundOne = findViewById(R.id.background_one);
final ImageView backgroundTwo = findViewById(R.id.background_two);

final ValueAnimator animator = ValueAnimator.ofFloat(0.0f, -1.0f);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.setDuration(3000L);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
@Override
public void onAnimationUpdate(ValueAnimator animation)
final float progress = (float) animation.getAnimatedValue();
final float width = backgroundOne.getWidth();
final float translationX = width * progress;
backgroundOne.setTranslationX(translationX);
backgroundTwo.setTranslationX(translationX + width);

);
animator.start();











share|improve this question
























  • Splitting the code in several classes could improve readability, but I fail to see how it would improve performance.

    – Maurice Perry
    Mar 22 at 5:46













0












0








0








My Java code is executing really slowly in my Android device. I think it's because I made it all in one class. I need some recommendations for how to improve my code, maybe by dividing it into different classes. How you would do it?



public class MainActivity extends AppCompatActivity 
View white;
Animation downtoup;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Remove notification bar
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

//MovingScanSquare
white = findViewById(R.id.white);
downtoup = AnimationUtils.loadAnimation(this, R.anim.downtoup);

white.setVisibility(View.VISIBLE);
downtoup = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.RELATIVE_TO_PARENT, 1.1f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.f);
downtoup.setDuration(2200);
downtoup.setRepeatCount(-1);
downtoup.setRepeatMode(Animation.REVERSE);
downtoup.setInterpolator(new LinearInterpolator());
white.setAnimation(downtoup);


//movingscreen
final ImageView backgroundOne = findViewById(R.id.background_one);
final ImageView backgroundTwo = findViewById(R.id.background_two);

final ValueAnimator animator = ValueAnimator.ofFloat(0.0f, -1.0f);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.setDuration(3000L);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
@Override
public void onAnimationUpdate(ValueAnimator animation)
final float progress = (float) animation.getAnimatedValue();
final float width = backgroundOne.getWidth();
final float translationX = width * progress;
backgroundOne.setTranslationX(translationX);
backgroundTwo.setTranslationX(translationX + width);

);
animator.start();











share|improve this question
















My Java code is executing really slowly in my Android device. I think it's because I made it all in one class. I need some recommendations for how to improve my code, maybe by dividing it into different classes. How you would do it?



public class MainActivity extends AppCompatActivity 
View white;
Animation downtoup;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Remove notification bar
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

//MovingScanSquare
white = findViewById(R.id.white);
downtoup = AnimationUtils.loadAnimation(this, R.anim.downtoup);

white.setVisibility(View.VISIBLE);
downtoup = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.ABSOLUTE, 0f,
TranslateAnimation.RELATIVE_TO_PARENT, 1.1f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.f);
downtoup.setDuration(2200);
downtoup.setRepeatCount(-1);
downtoup.setRepeatMode(Animation.REVERSE);
downtoup.setInterpolator(new LinearInterpolator());
white.setAnimation(downtoup);


//movingscreen
final ImageView backgroundOne = findViewById(R.id.background_one);
final ImageView backgroundTwo = findViewById(R.id.background_two);

final ValueAnimator animator = ValueAnimator.ofFloat(0.0f, -1.0f);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.setDuration(3000L);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
@Override
public void onAnimationUpdate(ValueAnimator animation)
final float progress = (float) animation.getAnimatedValue();
final float width = backgroundOne.getWidth();
final float translationX = width * progress;
backgroundOne.setTranslationX(translationX);
backgroundTwo.setTranslationX(translationX + width);

);
animator.start();








java android performance






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 5:59









karel

2,32782732




2,32782732










asked Mar 22 at 5:43









Braulio TrujilloBraulio Trujillo

11




11












  • Splitting the code in several classes could improve readability, but I fail to see how it would improve performance.

    – Maurice Perry
    Mar 22 at 5:46

















  • Splitting the code in several classes could improve readability, but I fail to see how it would improve performance.

    – Maurice Perry
    Mar 22 at 5:46
















Splitting the code in several classes could improve readability, but I fail to see how it would improve performance.

– Maurice Perry
Mar 22 at 5:46





Splitting the code in several classes could improve readability, but I fail to see how it would improve performance.

– Maurice Perry
Mar 22 at 5:46












2 Answers
2






active

oldest

votes


















1














Splitting the code into different classes won't get you so far. And it's quite difficult to say what's going on there without profiling it. Use one of the profiling tools, VisualVM for example, to do that.



Monitor your application, find out which line of code takes too much time and try to optimize it.






share|improve this answer






























    0














    To improve readability you can use different classes. But it will not improve performance much.



    For long processing task use background thread instead of doing it on the main thread.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293538%2fimprove-java-code-to-be-executed-faster-on-android%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Splitting the code into different classes won't get you so far. And it's quite difficult to say what's going on there without profiling it. Use one of the profiling tools, VisualVM for example, to do that.



      Monitor your application, find out which line of code takes too much time and try to optimize it.






      share|improve this answer



























        1














        Splitting the code into different classes won't get you so far. And it's quite difficult to say what's going on there without profiling it. Use one of the profiling tools, VisualVM for example, to do that.



        Monitor your application, find out which line of code takes too much time and try to optimize it.






        share|improve this answer

























          1












          1








          1







          Splitting the code into different classes won't get you so far. And it's quite difficult to say what's going on there without profiling it. Use one of the profiling tools, VisualVM for example, to do that.



          Monitor your application, find out which line of code takes too much time and try to optimize it.






          share|improve this answer













          Splitting the code into different classes won't get you so far. And it's quite difficult to say what's going on there without profiling it. Use one of the profiling tools, VisualVM for example, to do that.



          Monitor your application, find out which line of code takes too much time and try to optimize it.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 at 6:18









          Pavel SmirnovPavel Smirnov

          2,367818




          2,367818























              0














              To improve readability you can use different classes. But it will not improve performance much.



              For long processing task use background thread instead of doing it on the main thread.






              share|improve this answer



























                0














                To improve readability you can use different classes. But it will not improve performance much.



                For long processing task use background thread instead of doing it on the main thread.






                share|improve this answer

























                  0












                  0








                  0







                  To improve readability you can use different classes. But it will not improve performance much.



                  For long processing task use background thread instead of doing it on the main thread.






                  share|improve this answer













                  To improve readability you can use different classes. But it will not improve performance much.



                  For long processing task use background thread instead of doing it on the main thread.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 22 at 6:10









                  MagudeshMagudesh

                  15818




                  15818



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293538%2fimprove-java-code-to-be-executed-faster-on-android%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript