Android Interview Questions: Animation + Event Processing

Preface

Reminder: Autumn recruits are coming soon, Ji Meng will prepare a set of Android primary interview questions before the end of the National Day holiday. I hope it will be helpful to everyone.


Tip: The following is the content of this article

Animation

1. What are the types of animation in Android

Frame animation, tween animation, attribute animation


2. Can animations be used together?

You can combine animations together using AnimatorSet,

AnimatorSet.play() can play the current animation at the same time

  • .with(): Execute the existing animation and the incoming animation at the same time

  • .after(): Insert the existing animation into the incoming animation to execute

  • .before(): Insert the existing animation before the incoming animation


3. What are the types of interpolators?

  • AccelerateDecelerateInterpolator: The rate changes slowly at the beginning and end of the animation, and accelerates in the middle
  • AccelerateInterpolator: The rate changes slowly at the beginning of the animation, and then the rate changes faster
  • LinearInterpolator: change at a constant rate
  • AnticipateInterpolator: Flick backward and then forward at the beginning
  • PathInterpolator: The effect of animation execution is based on Bezier curve
  • AnticipateOvershootInterpolator: At the beginning, go backwards and then forward a certain value and return to the final value
  • CycleInterpolator: The animation plays a specific number of times in a loop, and the rate changes along a sinusoidal curve
  • OvershootInterpolator: Flick forward a certain value and then return to the original position
  • BounceInterpolator: There is a bounce effect when the animation ends

4. How to customize the interpolator

Write a class that implements the Interpolator interface.
Interpolator is an empty interface that inherits the TimeInterpolator interface.
Just define the getInterpolation method


5. How to modify the Activity entry and exit animation

There are two ways, one is by defining the theme of the Activity, and the other is by overriding the overridePendingTransition method of the Activity.


Event handling

1. Handler mechanism

Handler can act as a bridge between the child thread and the main thread.
Usually the Handler is declared in the Activity, and then the handleMessage method in the Handler is overwritten. When the child thread calls the handler.sendMessage() method, the handleMessage method will be executed in the main thread.

In addition to Handler and Message, there are hidden Looper and MessageQueue objects. In the main thread, Android has called Looper.preper() by default. The purpose of calling this method is in Looper

Create MessageQueue member variables and bind the Looper object to the current thread. When the sendMessage (object) method of the Handler is called, the Message object is added to the MessageQueue created by Looper, and the target object is assigned to the Message. In fact, the target object is the Handler object. The main thread executes the Looper.looper() method by default, which takes Message from the member variable MessageQueue of Looper.

Then call the handleMessage() method of the target object of Message. This completes the entire message mechanism.


2. What is the difference between onTouch and onTouchEvent

Both methods are called in dispatchTouchEvent of View, and onTouch is executed in priority to onTouchEvent. If the event is consumed by returning true in the onTouch method, onTouchEvent will not be executed again.

Also note that there are two prerequisites for onTouch to be executed. The value of the first mOnTouchListener cannot be empty, and the second currently clicked control must be enabled. So if you have a control that is non-enable, registering onTouch events for it will never be executed. For this type of control, if we want to listen to its touch event, we must do so by overriding the onTouchEvent method in the control.


3. Can new handlers be used in child threads? why?

No, if the new Handler() directly in the child thread will throw an exception

When we create the Handler object in the dao main thread, there is no problem, because the main thread will automatically call the Looper.prepare () method to create and set a Looper object for the current main thread, freely from the current in the Handler constructor Get this Looper on the object of the thread.
But this method is not automatically called in the child thread, so if you want to create the Handler object in the child thread, you must manually call the Looper.prepare() method before creation, otherwise an error will be reported.


4. The child thread sends a message to the main thread to update the UI. What else is there besides handler and AsyncTask?

1. Use the runOnUiThread method of the Activity object to update, and update the UI through the runOnUiThread() method in the child thread:
2. Use the View.post(Runnable r) method to update the UI


5. Why is the system not recommended to access the UI in the child thread?

Android UI access is not locked, multiple threads can simultaneously access the same UI control for update operations. That is to say, when accessing the UI, the controls in the android system are not thread-safe. This will cause uncontrollable errors to occur when multiple threads access and update the same UI control in multi-thread mode. Is fatal. Therefore, Android stipulates that the UI can only be accessed in the UI thread, which is equivalent to adding a lock to Android's UI access from another perspective, a pseudo-lock.


About sorting out

When everything is sorted, it will be sorted into pdf format for easy reading. The file is obtained as shown below (after October 8)!


Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42761395/article/details/108906142