Android 开发记录

64 阅读6分钟

一、让子View显示超出自身容器大小

在子View的父布局上设置android:clipChildren=“false”,在View会通过这个方法判断是否需要clip。使用的时候需要注意,这个参数只能设置在ViewGroup上,而生效的范围是ViewGroup内的子View。

void setDisplayListProperties(RenderNode renderNode) {
        if (renderNode != null) {
            ...
            renderNode.setClipToBounds(mParent instanceof ViewGroup
                    && ((ViewGroup) mParent).getClipChildren());
            ...
       }
      ....
}

没有添加android:clipChildren=“false”,图1的imagview是无法把图片显示完整

<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">

    <ImageView
        android:id="@+id/image"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:src="@drawable/snow"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

图1.png

添加android:clipChildren=“false”后,图2的imagview的大小是没有改变,但是可以把超出imagview范围的图片内容都显示出来。 图2.png

二、取消RecyclerView滑动到边缘上的水纹提示

RecyclerView是继承ViewGroup的,而VIewGroup在初始的时候会设置WILL_NOT_DRAW进行绘制的优化,因为一般ViewGroup只是作为容器用,具体的内容应该由View来绘制。

private void initViewGroup() {
        // ViewGroup doesn't draw by default
        if (!debugDraw()) {
            setFlags(WILL_NOT_DRAW, DRAW_MASK);
        }
  ...
}

当然也有特殊情况,例如RecyclerView的滑动到边缘的水纹提示,这个就需要RecyclerView自身绘制,所以View也提供setWillNotDraw(boolean willNotDraw)这个方法对这个参数进行修改。

  public void setWillNotDraw(boolean willNotDraw) {
        setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
    }

所以RecyclerView也会调用这个方法进行设置,果然在构造里调用了setWillNotDraw(boolean willNotDraw)这个方法。

public RecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
...
        setWillNotDraw(getOverScrollMode() == View.OVER_SCROLL_NEVER);
...
}

需要取消水纹效果,就需要用setWillNotDraw(boolean willNotDraw)这个方法设置为true,而当getOverScrollMode() == View.OVER_SCROLL_NEVER为true的时候就不会在绘制水纹了。而这个getOverScrollMode()方法是View里自带的一个方法,所以应该还有设置的方法。 在View里面可以找到这个设置方法,可以通过在代码里设置了这个方法就可以把水纹取消。

    public void setOverScrollMode(int overScrollMode) {
        if (overScrollMode != OVER_SCROLL_ALWAYS &&
                overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
                overScrollMode != OVER_SCROLL_NEVER) {
            throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
        }
        mOverScrollMode = overScrollMode;
    }

其实除了可以在代码上设置之外,还可以通过xml布局的时候设置,android:overScrollMode="never"

...
    case R.styleable.View_overScrollMode:
            overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
            break;
...

三、状态栏的设置

在style主题中设置这个 true 会导致内容显示到状态栏中。 隐藏状态栏的方法 window.decorView.windowInsetsController?.hide(WindowInsets.Type.systemBars()) 在刘海屏隐藏状态栏,会导致状态栏显示黑色,可以用下面代码解决

    /**
     * 适配刘海屏
     * Fits notch screen.
     */
    private fun fitsNotchScreen() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            val lp: WindowManager.LayoutParams = window.getAttributes()
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
            window.setAttributes(lp)
        }
    }

四、将View提示到顶层

view.bringToFront() 方法可以将view提示到顶层

/**
 * Change the view's z order in the tree, so it's on top of other sibling
 * views. This ordering change may affect layout, if the parent container
 * uses an order-dependent layout scheme (e.g., LinearLayout). Prior
 * to {@link android.os.Build.VERSION_CODES#KITKAT} this
 * method should be followed by calls to {@link #requestLayout()} and
 * {@link View#invalidate()} on the view's parent to force the parent to redraw
 * with the new child ordering.
 *
 * @see ViewGroup#bringChildToFront(View)
 */
public void bringToFront() {
    if (mParent != null) {
        mParent.bringChildToFront(this);
    }
}

五、代码触发View的OnClickListener和OnLongClickListener、 performLongClick(float x, float y)

view.performClick() 和 view.performLongClick() 可以分别触发view的OnClickListener和OnLongClickListener事件 view.performLongClick(float x, float y)方法可以通过坐标来触发对应view的OnLongClickListener事件

/**
 * Call this view's OnClickListener, if it is defined.  Performs all normal
 * actions associated with clicking: reporting accessibility event, playing
 * a sound, etc.
 *
 * @return True there was an assigned OnClickListener that was called, false
 *         otherwise is returned.
 */
// NOTE: other methods on View should not call this method directly, but performClickInternal()
// instead, to guarantee that the autofill manager is notified when necessary (as subclasses
// could extend this method without calling super.performClick()).
public boolean performClick() {
    // We still need to call this method to handle the cases where performClick() was called
    // externally, instead of through performClickInternal()
    notifyAutofillManagerOnClick();
    final boolean result;
    final ListenerInfo li = mListenerInfo;
    if (li != null && li.mOnClickListener != null) {
        playSoundEffect(SoundEffectConstants.CLICK);
        li.mOnClickListener.onClick(this);
        result = true;
    } else {
        result = false;
    }
    sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
    notifyEnterOrExitForAutoFillIfNeeded(true);
    return result;
}

/**
 * Calls this view's OnLongClickListener, if it is defined. Invokes the
 * context menu if the OnLongClickListener did not consume the event.
 *
 * @return {@code true} if one of the above receivers consumed the event,
 *         {@code false} otherwise
 */
public boolean performLongClick() {
    return performLongClickInternal(mLongClickX, mLongClickY);
}

/**
 * Calls this view's OnLongClickListener, if it is defined. Invokes the
 * context menu if the OnLongClickListener did not consume the event,
 * anchoring it to an (x,y) coordinate.
 *
 * @param x x coordinate of the anchoring touch event, or {@link Float#NaN}
 *          to disable anchoring
 * @param y y coordinate of the anchoring touch event, or {@link Float#NaN}
 *          to disable anchoring
 * @return {@code true} if one of the above receivers consumed the event,
 *         {@code false} otherwise
 */
public boolean performLongClick(float x, float y) {
    mLongClickX = x;
    mLongClickY = y;
    final boolean handled = performLongClick();
    mLongClickX = Float.NaN;
    mLongClickY = Float.NaN;
    return handled;
}

六、编译后的R文件路径

image.png

七、Android全局实现控件变灰

看了鸿洋大神的文章,才知道原来还可以这么简单的实现全局控件变灰,有兴趣的可以去看看。下面只是我个人的学习记录。

实现的灰度的工具

做这个的时候让我想起了之前做的人脸识别,人脸识别的第一步就是将获取的图片转换成灰度图。转换成灰度图的方式是用矩阵的方式实现的。关于矩阵这里就不展开来说了,都差不多还给了大学的高数老师了,有空再复习一下。 而在Android中已经有现成封装好的矩阵转换的类ColorMatrix,通过setSaturation(float sat)方法可以转变成灰度图,实现方法可以看图1。 图1.png

那么这矩阵要怎么用呢?如果是非常了解自定义view的人,应该很快就能想到Paint.setColorFilter(ColorFilter filter)这个方法,通过查看ColorFilter的继承结构图,就可以找到ColorMatrixColorFilter是通过矩阵来设置色彩的。 图2.png

为什么只设置ViewGroup的灰度,其子View也会跟着变?

按照鸿洋大神的思路,自定义一个GrayFramelayout实现一个Paint,并把Paint设置到Canvas中,最后就是用GrayFramelayout替换Activity最外层的Framelayout。自此整个过程就完成了。 但是重点来了,为什么只是将最外层的Framelayout设置灰度,它的子View也会跟着被设置灰度呢?其实鸿洋大神已经给了提示,那就是dispatchDraw(canvas: Canvas?)这个方法。 带着这个问题,我重新查看了View的draw的过程,看看下面的图3。 图3.png 在这里调用我们复写的dispatchDraw(canvas: Canvas?)方法,而这个方法的描述是draw the children,而我们的复写是将Paint放进了Canvas中。

override fun dispatchDraw(canvas: Canvas?) {
        canvas?.saveLayer(null, mPaint, Canvas.ALL_SAVE_FLAG)
        super.dispatchDraw(canvas)
        canvas?.restore()
}

继续查看原生的dispatchDraw(canvas: Canvas?)方法,View的dispatchDraw(canvas: Canvas?)是一个空方法, 图4.png 那看看ViewGroup的dispatchDraw(canvas: Canvas?)方法,在这里会调用drawChild(Canvas canvas, View child, long drawingTime)方法,并且把我们修改的Canvas传进了这个方法。

override fun dispatchDraw(canvas: Canvas?) {
...
 for (int i = 0; i < childrenCount; i++) {
            while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) {
                final View transientChild = mTransientViews.get(transientIndex);
                if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
                        transientChild.getAnimation() != null) {
                    more |= drawChild(canvas, transientChild, drawingTime);
                }
                transientIndex++;
                if (transientIndex >= transientCount) {
                    transientIndex = -1;
                }
            }

            final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
            final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
            if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
                more |= drawChild(canvas, child, drawingTime);
            }
        }
        while (transientIndex >= 0) {
            // there may be additional transient views after the normal views
            final View transientChild = mTransientViews.get(transientIndex);
            if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||
                    transientChild.getAnimation() != null) {
                more |= drawChild(canvas, transientChild, drawingTime);
            }
            transientIndex++;
            if (transientIndex >= transientCount) {
                break;
            }
        }
...
}

再看看drawChild(Canvas canvas, View child, long drawingTime)这个方法,这个方法很简单,就是调用子View的draw(Canvas canvas, ViewGroup parent, long drawingTime),并把我们修改的Canvas传给子View使用。 图5.png 至此,我们就明白了为什么只是修改了最外层的VIewGroup,内部的子View也会跟着修改了,这是因为所有的控件都是默认使用最外层的ViewGroup的Canvas。