android面试录

Android面试专题整理

2020-01-13  本文已影响0人  白茫茫的大地

fresco加载图片原理 优势是什么

缓存怎么处理的

a、根据Uri在已解码的(Bitmap缓存)内存缓存中查找,找到了则返回Bitmap对象;如果没找到,则开启后台线程开始后续的工作。
b、根据Uri在未解码的内存缓存中查找,若找到了则解码,然后缓存到已解码的内存缓存中,并且返回Bitmap对象。
d、如果在未解码的内存缓存中没找到,则根据Uri在磁盘缓存中查找,若找到了则读取数据(byte数组),并缓存到未解码的内存缓存中,解码、然后缓存到已解码的内存缓存中,并且返回Bitmap对象。
e、如果在磁盘缓存中没找到,则从网络或者本地加载数据。加载完成后,依次缓存到磁盘缓存、未解码的内存缓存中。解码、然后缓存到已解码的内存缓存中,并且返回Bitmap对象。

bitmap 内存分配

data binding

Android中动画的原理

touch事件分发

事件处理都是在 onTouchEvent 里面做的,这一点需要先理解,举个简单的例子,我点击一个按钮,有点击事件的回调,这个点击事件就是在onTouch里面处理的,所有的子View消费了这个事件,大致可以理解为,子View自己处理了touch事件,父View就别处理了,子View处理了点击事件,父View就别处理点击事件了

dispatchTouchEvent

事件分发

public boolean dispatchTouchEvent(MotionEvent ev) {
    if (child.dispatchTouchEvent(ev)) {
        return true;    //如果子View消费了该事件,则返回TRUE,让调用者知道该事件已被消费
    } else {
        return onTouchEvent(ev);    //如果子View没有消费该事件,则调用自身的onTouchEvent尝试处理。
    }
}

如果返回了true,就说明子View消费了这个事件,就不会调用 onTouchEvent 方法

public boolean dispatchTouchEvent(MotionEvent ev) {
    if (!onInterceptTouchEvent(ev)) {
        return child.dispatchTouchEvent(ev);    //不拦截,则传给子View进行分发处理
    } else {
        return onTouchEvent(ev);    //拦截事件,交由自身对象的onTouchEvent方法处理
    }
}

如果拦截了触摸事件,则就会调用自己的 onTouchEvent 方法,如果不拦截,就遍历自己子view,发现自己的符合某个条件,就调用子View的dispatchTouchEvent方法

public boolean dispatchTouchEvent(MotionEvent ev) {
    //如果该对象的监听成员变量不为空,则会调用其onTouch方法,
    if (mOnTouchListener != null && mOnTouchListener.onTouch(this, event)) {
        return true;    //若onTouch方法返回TRUE,则表示消费了该事件,则dispachtouTouchEvent返回TRUE,让其调用者知道该事件已被消费。
    }
    return onTouchEvent(ev);    //若监听成员为空或onTouch没有消费该事件,则调用对象自身的onTouchEvent方法处理。
}

若listener的onTouch方法返回TRUE,则dispatchTouchEvent也会返回TRUE,表示消费该事件。不会调用onTouchEvent方法了

整体代码流程
///////////ViewGroup
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
       ...省略部分代码
        boolean handled = false;
        if (onFilterTouchEventForSecurity(ev)) {
            final int action = ev.getAction();
            final int actionMasked = action & MotionEvent.ACTION_MASK;
            //如果是DOWN事件,清除标记,恢复一些标记状态
            if (actionMasked == MotionEvent.ACTION_DOWN) {
                cancelAndClearTouchTargets(ev);
                resetTouchState();
            }

            //判断是否拦截
            final boolean intercepted;
            //触发DOWN事件或者mFirstTouchTarget不是Null,才会走if里面的逻辑
            if (actionMasked == MotionEvent.ACTION_DOWN
                    || mFirstTouchTarget != null) {
                // 子View可以通知父View,不要拦截
                final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
                if (!disallowIntercept) {
                    intercepted = onInterceptTouchEvent(ev);
                    ev.setAction(action); // restore action in case it was changed
                } else { // 不拦截
                    intercepted = false;
                }
            } else {
                // There are no touch targets and this action is not an initial down
                // so this view group continues to intercept touches.
                //不是DOWN事件 mFirstTouchTarget为空  拦截后续事件
                intercepted = true;
            }

             ...省略部分代码

            final boolean canceled = resetCancelNextUpFlag(this)
                    || actionMasked == MotionEvent.ACTION_CANCEL;

            // Update list of touch targets for pointer down, if needed.
            final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
            TouchTarget newTouchTarget = null;
            boolean alreadyDispatchedToNewTouchTarget = false;
            //事件没有被取消或者没有被拦截执行if中逻辑
            if (!canceled && !intercepted) {
                View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus()
                        ? findChildWithAccessibilityFocus() : null;

                if (actionMasked == MotionEvent.ACTION_DOWN
                        || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)
                        || actionMasked == MotionEvent.ACTION_HOVER_MOVE) {
                    final int actionIndex = ev.getActionIndex(); // always 0 for down
                    final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex)
                            : TouchTarget.ALL_POINTER_IDS;

                    // Clean up earlier touch targets for this pointer id in case they
                    // have become out of sync.
                    removePointersFromTouchTargets(idBitsToAssign);

                    final int childrenCount = mChildrenCount;
                    // 
                    if (newTouchTarget == null && childrenCount != 0) {
                        final float x = ev.getX(actionIndex);
                        final float y = ev.getY(actionIndex);
                        // Find a child that can receive the event.
                        // 从前到后为View进行排序
                        final ArrayList<View> preorderedList = buildTouchDispatchChildList();
                        final boolean customOrder = preorderedList == null && isChildrenDrawingOrderEnabled();
                        final View[] children = mChildren;
                        // 遍历排序后的View,进入淘汰机制
                        for (int i = childrenCount - 1; i >= 0; i--) {
                            final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
                            final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
                            ...
                            //判断child是否可以响应事件 是否点击在了View的范围之内,如果不是的话,就不会进行下面的操作,也就不会有touch事件的调用
                            if (!canViewReceivePointerEvents(child)
                                    || !isTransformedTouchPointInView(x, y, child, null)) {
                                ev.setTargetAccessibilityFocus(false);
                                continue;
                            }
                            //获取上次响应事件的View,DOWN事件时候newTouchTarget为Null
                            newTouchTarget = getTouchTarget(child);
                            if (newTouchTarget != null) {
                                // Child is already receiving touch within its bounds.
                                // Give it the new pointer in addition to the ones it is handling.
                                newTouchTarget.pointerIdBits |= idBitsToAssign;
                                break;
                            }

                            resetCancelNextUpFlag(child);
                            //很重要的一个方法,在下面我们会看这个方法做了什么
                            if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) { 
                                // 消费了这个事件,如果消费了这个事件。会为newTouchTarget赋值,mFirstTouchTarget赋值
                                // Child wants to receive touch within its bounds.
                                mLastTouchDownTime = ev.getDownTime();
                                if (preorderedList != null) {
                                    // childIndex points into presorted list, find original index
                                    for (int j = 0; j < childrenCount; j++) {
                                        if (children[childIndex] == mChildren[j]) {
                                            mLastTouchDownIndex = j;
                                            break;
                                        }
                                    }
                                } else {
                                    mLastTouchDownIndex = childIndex;
                                }
                                mLastTouchDownX = ev.getX();
                                mLastTouchDownY = ev.getY();
                                //在这个方法中把child赋值给了mFirstTouchTarget
                                newTouchTarget = addTouchTarget(child, idBitsToAssign);
                                alreadyDispatchedToNewTouchTarget = true;
                                break;
                            }                           
                            ev.setTargetAccessibilityFocus(false);
                        }
                        if (preorderedList != null) preorderedList.clear();
                    }

                   ...
                }
            }

            // Dispatch to touch targets.
            if (mFirstTouchTarget == null) { // 这里就标示没有要消费这个事件的View,如果没有,就调用自己的 dispatchTransformedTouchEvent 方法。这里的第三个参数为null
                // No touch targets so treat this as an ordinary view.
                handled = dispatchTransformedTouchEvent(ev, canceled, null,TouchTarget.ALL_POINTER_IDS);
            } else {
                // Dispatch to touch targets, excluding the new touch target if we already
                // dispatched to it.  Cancel touch targets if necessary.
                TouchTarget predecessor = null;
                TouchTarget target = mFirstTouchTarget;
                while (target != null) {
                    final TouchTarget next = target.next;
                    if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {
                        handled = true;
                    } else {
                        final boolean cancelChild = resetCancelNextUpFlag(target.child)
                                || intercepted;
                        // 这里调用子View的dispatch事件
                        if (dispatchTransformedTouchEvent(ev, cancelChild,
                                target.child, target.pointerIdBits)) {
                            handled = true;
                        }
                        if (cancelChild) {
                            if (predecessor == null) {
                                mFirstTouchTarget = next;
                            } else {
                                predecessor.next = next;
                            }
                            target.recycle();
                            target = next;
                            continue;
                        }
                    }
                    predecessor = target;
                    target = next;
                }
            }

            ...
        }    

        ...
        return handled;
    }
    
    
    
    //最重要的几行代码的逻辑
    private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel,
            View child, int desiredPointerIdBits) {
        final boolean handled;
        ...省略部分代码
        // Perform any necessary transformations and dispatch.
        //child为空调用父View的dispatchTouchEvent
        if (child == null) {
            handled = super.dispatchTouchEvent(transformedEvent);
        } else {
            ...
            //child不为空调用child的dispatchTouchEvent方法
            handled = child.dispatchTouchEvent(transformedEvent);
        }
        ...
        return handled;
    }

    ////////// ViewGroup
     public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.isFromSource(InputDevice.SOURCE_MOUSE)
                && ev.getAction() == MotionEvent.ACTION_DOWN
                && ev.isButtonPressed(MotionEvent.BUTTON_PRIMARY)
                && isOnScrollbarThumb(ev.getX(), ev.getY())) {
            return true;
        }
        return false;
    }
    ////////// View
     public boolean dispatchTouchEvent(MotionEvent event) {
        ...

        boolean result = false;
        ...

        if (onFilterTouchEventForSecurity(event)) {
            if ((mViewFlags & ENABLED_MASK) == ENABLED && handleScrollBarDragging(event)) {
                result = true;
            }
            //noinspection SimplifiableIfStatement
            ListenerInfo li = mListenerInfo;
            //我们看到有mOnTouchListener,并且onTouch返回true,就没有onTouchEvent啥事了
            if (li != null && li.mOnTouchListener != null
                    && (mViewFlags & ENABLED_MASK) == ENABLED
                    && li.mOnTouchListener.onTouch(this, event)) {
                result = true;
            }

            if (!result && onTouchEvent(event)) {
                result = true;
            }
        }

       ...
        return result;
    }
    
    public boolean onTouchEvent(MotionEvent event) {
       ...
       // 一点击的话,项目返回为true,自己消费事件
        if (clickable || (viewFlags & TOOLTIP) == TOOLTIP) {
            ...
            return true;
        }

        return false;
    }

几点结论

ANR产生的原因

SpareArray

Eventbus

// 子类调用的时候 methodClassOld 为空,
// 父类的时候 methodClassOld(此时为子类)不为空,并且methodClassOld.isAssignableFrom(methodClass)为false,此时返回为false,并且 subscriberClassByMethodKey 里面为子类
Class<?> methodClassOld = subscriberClassByMethodKey.put(methodKey, methodClass);
if (methodClassOld == null || methodClassOld.isAssignableFrom(methodClass)) {
    // Only add if not already found in a sub class
    return true;
} else {
    // Revert the put, old class is further down the class hierarchy
    subscriberClassByMethodKey.put(methodKey, methodClassOld);
    return false;
}

Handler

okhttp

LRUCache

RxJava 功能与原理实现,与平时使用的异步操作来比,优势,优缺点

插件化

热修复

图片的加载问题

Binder机制

上一篇 下一篇

猜你喜欢

热点阅读