I have created simple test project to test admob banner. I have issue when banner is loaded the whole activity seems laggy. I have button with animation which scale whole button up and down after click but its look laggy also I tried button click without animation and still problem remains and button click looks laggy. I was also looking to app profile to see whats going on and after button click CPU goes up to 30% and memory usage is around 100 mb. When I remove method loadAd() it runs smoothly. Tested on Pixel XL emulator 27 API.
MainActivity class:
public class MainActivity extends AppCompatActivity {
private Animation scaleUp,scaleDown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadAd();
btnClickListener();
}
private void loadAd(){
final LinearLayout adLayout = findViewById(R.id.adLayout);
final AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); //test ad id
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest); //load ad
adLayout.addView(adView); //add ad to layout
}
},1000);
}
private void btnClickListener() {
scaleUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale_up);
scaleDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale_down);
Button btnClick = findViewById(R.id.btn);
btnClick.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
btnClick.startAnimation(scaleUp);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
btnClick.startAnimation(scaleDown);
}
return false;
}
});
}
}
activity xml:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/adLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Animation scale-up:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromYScale="1.2"
android:fromXScale="1"
android:toYScale="1.2"
android:toXScale="1.2"
android:pivotY="50%"
android:pivotX="50%"
android:duration="100"/>
Animation scale-down:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromYScale="1.2"
android:fromXScale="1.2"
android:toYScale="1"
android:toXScale="1"
android:pivotY="50%"
android:pivotX="50%"
android:duration="100"/>