Reputation: 1
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();
}
}
Upvotes: 0
Views: 82
Reputation: 4799
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.
Upvotes: 1
Reputation: 449
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.
Upvotes: 0