java letter,Letterbox.java

/*

* Copyright (C) 2018 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package com.android.server.wm;

import static android.view.SurfaceControl.HIDDEN;

import android.graphics.Rect;

import android.view.SurfaceControl;

import java.util.function.Supplier;

/**

* Manages a set of {@link SurfaceControl}s to draw a black letterbox between an

* outer rect and an inner rect.

*/

public class Letterbox {

private static final Rect EMPTY_RECT = new Rect();

private final SuppliermFactory;

private final Rect mOuter = new Rect();

private final Rect mInner = new Rect();

private final LetterboxSurface mTop = new LetterboxSurface("top");

private final LetterboxSurface mLeft = new LetterboxSurface("left");

private final LetterboxSurface mBottom = new LetterboxSurface("bottom");

private final LetterboxSurface mRight = new LetterboxSurface("right");

/**

* Constructs a Letterbox.

*

* @param surfaceControlFactory a factory for creating the managed {@link SurfaceControl}s

*/

public Letterbox(SuppliersurfaceControlFactory) {

mFactory = surfaceControlFactory;

}

/**

* Lays out the letterbox, such that the area between the outer and inner

* frames will be covered by black color surfaces.

*

* The caller must use {@link #applySurfaceChanges} to apply the new layout to the surface.

*

* @param outer the outer frame of the letterbox (this frame will be black, except the area

* that intersects with the {code inner} frame).

* @param inner the inner frame of the letterbox (this frame will be clear)

*/

public void layout(Rect outer, Rect inner) {

mOuter.set(outer);

mInner.set(inner);

mTop.layout(outer.left, outer.top, inner.right, inner.top);

mLeft.layout(outer.left, inner.top, inner.left, outer.bottom);

mBottom.layout(inner.left, inner.bottom, outer.right, outer.bottom);

mRight.layout(inner.right, outer.top, outer.right, inner.bottom);

}

/**

* Gets the insets between the outer and inner rects.

*/

public Rect getInsets() {

return new Rect(

mLeft.getWidth(),

mTop.getHeight(),

mRight.getWidth(),

mBottom.getHeight());

}

/**

* Returns true if any part of the letterbox overlaps with the given {@code rect}.

*/

public boolean isOverlappingWith(Rect rect) {

return mTop.isOverlappingWith(rect) || mLeft.isOverlappingWith(rect)

|| mBottom.isOverlappingWith(rect) || mRight.isOverlappingWith(rect);

}

/**

* Hides the letterbox.

*

* The caller must use {@link #applySurfaceChanges} to apply the new layout to the surface.

*/

public void hide() {

layout(EMPTY_RECT, EMPTY_RECT);

}

/**

* Destroys the managed {@link SurfaceControl}s.

*/

public void destroy() {

mOuter.setEmpty();

mInner.setEmpty();

mTop.destroy();

mLeft.destroy();

mBottom.destroy();

mRight.destroy();

}

/** Returns whether a call to {@link #applySurfaceChanges} would change the surface. */

public boolean needsApplySurfaceChanges() {

return mTop.needsApplySurfaceChanges()

|| mLeft.needsApplySurfaceChanges()

|| mBottom.needsApplySurfaceChanges()

|| mRight.needsApplySurfaceChanges();

}

public void applySurfaceChanges(SurfaceControl.Transaction t) {

mTop.applySurfaceChanges(t);

mLeft.applySurfaceChanges(t);

mBottom.applySurfaceChanges(t);

mRight.applySurfaceChanges(t);

}

private class LetterboxSurface {

private final String mType;

private SurfaceControl mSurface;

private final Rect mSurfaceFrame = new Rect();

private final Rect mLayoutFrame = new Rect();

public LetterboxSurface(String type) {

mType = type;

}

public void layout(int left, int top, int right, int bottom) {

if (mLayoutFrame.left == left && mLayoutFrame.top == top

&& mLayoutFrame.right == right && mLayoutFrame.bottom == bottom) {

// Nothing changed.

return;

}

mLayoutFrame.set(left, top, right, bottom);

}

private void createSurface() {

mSurface = mFactory.get().setName("Letterbox - " + mType)

.setFlags(HIDDEN).setColorLayer(true).build();

mSurface.setLayer(-1);

mSurface.setColor(new float[]{0, 0, 0});

}

public void destroy() {

if (mSurface != null) {

mSurface.destroy();

mSurface = null;

}

}

public int getWidth() {

return Math.max(0, mLayoutFrame.width());

}

public int getHeight() {

return Math.max(0, mLayoutFrame.height());

}

public boolean isOverlappingWith(Rect rect) {

if (getWidth() <= 0 || getHeight() <= 0) {

return false;

}

return Rect.intersects(rect, mLayoutFrame);

}

public void applySurfaceChanges(SurfaceControl.Transaction t) {

if (mSurfaceFrame.equals(mLayoutFrame)) {

// Nothing changed.

return;

}

mSurfaceFrame.set(mLayoutFrame);

if (!mSurfaceFrame.isEmpty()) {

if (mSurface == null) {

createSurface();

}

t.setPosition(mSurface, mSurfaceFrame.left, mSurfaceFrame.top);

t.setSize(mSurface, mSurfaceFrame.width(), mSurfaceFrame.height());

t.show(mSurface);

} else if (mSurface != null) {

t.hide(mSurface);

}

}

public boolean needsApplySurfaceChanges() {

return !mSurfaceFrame.equals(mLayoutFrame);

}

}

}

Java程序

|

201行

|

6.49 KB

/*

* Copyright (C) 2018 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package com.android.server.wm;

import static android.view.SurfaceControl.HIDDEN;

import android.graphics.Rect;

import android.view.SurfaceControl;

import java.util.function.Supplier;

/**

* Manages a set of {@link SurfaceControl}s to draw a black letterbox between an

* outer rect and an inner rect.

*/

public class Letterbox {

private static final Rect EMPTY_RECT = new Rect();

private final Supplier mFactory;

private final Rect mOuter = new Rect();

private final Rect mInner = new Rect();

private final LetterboxSurface mTop = new LetterboxSurface("top");

private final LetterboxSurface mLeft = new LetterboxSurface("left");

private final LetterboxSurface mBottom = new LetterboxSurface("bottom");

private final LetterboxSurface mRight = new LetterboxSurface("right");

/**

* Constructs a Letterbox.

*

* @param surfaceControlFactory a factory for creating the managed {@link SurfaceControl}s

*/

public Letterbox(Supplier surfaceControlFactory) {

mFactory = surfaceControlFactory;

}

/**

* Lays out the letterbox, such that the area between the outer and inner

* frames will be covered by black color surfaces.

*

* The caller must use {@link #applySurfaceChanges} to apply the new layout to the surface.

*

* @param outer the outer frame of the letterbox (this frame will be black, except the area

* that intersects with the {code inner} frame).

* @param inner the inner frame of the letterbox (this frame will be clear)

*/

public void layout(Rect outer, Rect inner) {

mOuter.set(outer);

mInner.set(inner);

mTop.layout(outer.left, outer.top, inner.right, inner.top);

mLeft.layout(outer.left, inner.top, inner.left, outer.bottom);

mBottom.layout(inner.left, inner.bottom, outer.right, outer.bottom);

mRight.layout(inner.right, outer.top, outer.right, inner.bottom);

}

/**

* Gets the insets between the outer and inner rects.

*/

public Rect getInsets() {

return new Rect(

mLeft.getWidth(),

mTop.getHeight(),

mRight.getWidth(),

mBottom.getHeight());

}

/**

* Returns true if any part of the letterbox overlaps with the given {@code rect}.

*/

public boolean isOverlappingWith(Rect rect) {

return mTop.isOverlappingWith(rect) || mLeft.isOverlappingWith(rect)

|| mBottom.isOverlappingWith(rect) || mRight.isOverlappingWith(rect);

}

/**

* Hides the letterbox.

*

* The caller must use {@link #applySurfaceChanges} to apply the new layout to the surface.

*/

public void hide() {

layout(EMPTY_RECT, EMPTY_RECT);

}

/**

* Destroys the managed {@link SurfaceControl}s.

*/

public void destroy() {

mOuter.setEmpty();

mInner.setEmpty();

mTop.destroy();

mLeft.destroy();

mBottom.destroy();

mRight.destroy();

}

/** Returns whether a call to {@link #applySurfaceChanges} would change the surface. */

public boolean needsApplySurfaceChanges() {

return mTop.needsApplySurfaceChanges()

|| mLeft.needsApplySurfaceChanges()

|| mBottom.needsApplySurfaceChanges()

|| mRight.needsApplySurfaceChanges();

}

public void applySurfaceChanges(SurfaceControl.Transaction t) {

mTop.applySurfaceChanges(t);

mLeft.applySurfaceChanges(t);

mBottom.applySurfaceChanges(t);

mRight.applySurfaceChanges(t);

}

private class LetterboxSurface {

private final String mType;

private SurfaceControl mSurface;

private final Rect mSurfaceFrame = new Rect();

private final Rect mLayoutFrame = new Rect();

public LetterboxSurface(String type) {

mType = type;

}

public void layout(int left, int top, int right, int bottom) {

if (mLayoutFrame.left == left && mLayoutFrame.top == top

&& mLayoutFrame.right == right && mLayoutFrame.bottom == bottom) {

// Nothing changed.

return;

}

mLayoutFrame.set(left, top, right, bottom);

}

private void createSurface() {

mSurface = mFactory.get().setName("Letterbox - " + mType)

.setFlags(HIDDEN).setColorLayer(true).build();

mSurface.setLayer(-1);

mSurface.setColor(new float[]{0, 0, 0});

}

public void destroy() {

if (mSurface != null) {

mSurface.destroy();

mSurface = null;

}

}

public int getWidth() {

return Math.max(0, mLayoutFrame.width());

}

public int getHeight() {

return Math.max(0, mLayoutFrame.height());

}

public boolean isOverlappingWith(Rect rect) {

if (getWidth() <= 0 || getHeight() <= 0) {

return false;

}

return Rect.intersects(rect, mLayoutFrame);

}

public void applySurfaceChanges(SurfaceControl.Transaction t) {

if (mSurfaceFrame.equals(mLayoutFrame)) {

// Nothing changed.

return;

}

mSurfaceFrame.set(mLayoutFrame);

if (!mSurfaceFrame.isEmpty()) {

if (mSurface == null) {

createSurface();

}

t.setPosition(mSurface, mSurfaceFrame.left, mSurfaceFrame.top);

t.setSize(mSurface, mSurfaceFrame.width(), mSurfaceFrame.height());

t.show(mSurface);

} else if (mSurface != null) {

t.hide(mSurface);

}

}

public boolean needsApplySurfaceChanges() {

return !mSurfaceFrame.equals(mLayoutFrame);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值