下面列出了android.view.ViewGroup#getBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 初始化提示的布局
*/
private void initLayout() {
mMainLayout = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.widget_hint_layout, this, false);
mImageView = mMainLayout.findViewById(R.id.iv_hint_icon);
mTextView = mMainLayout.findViewById(R.id.iv_hint_text);
if (mMainLayout.getBackground() == null) {
// 默认使用 windowBackground 作为背景
TypedArray ta = getContext().obtainStyledAttributes(new int[]{android.R.attr.windowBackground});
mMainLayout.setBackground(ta.getDrawable(0));
ta.recycle();
}
addView(mMainLayout);
}
@Override
public void onSwapBuffersCompleted(int pendingSwapBuffersCount) {
TraceEvent.instant("onSwapBuffersCompleted");
// Wait until the second frame to turn off the placeholder background on
// tablets so the tab strip has time to start drawing.
final ViewGroup controlContainer = (ViewGroup) mControlContainer;
if (controlContainer != null && controlContainer.getBackground() != null && mHasDrawnOnce) {
post(new Runnable() {
@Override
public void run() {
controlContainer.setBackgroundResource(0);
}
});
}
mHasDrawnOnce = true;
mPendingSwapBuffersCount = pendingSwapBuffersCount;
if (!mSkipInvalidation || pendingSwapBuffersCount == 0) flushInvalidation();
mSkipInvalidation = !mSkipInvalidation;
}
@Override
public void onSwapBuffersCompleted(int pendingSwapBuffersCount) {
TraceEvent.instant("onSwapBuffersCompleted");
// Wait until the second frame to turn off the placeholder background on
// tablets so the tab strip has time to start drawing.
final ViewGroup controlContainer = (ViewGroup) mControlContainer;
if (controlContainer != null && controlContainer.getBackground() != null && mHasDrawnOnce) {
post(new Runnable() {
@Override
public void run() {
controlContainer.setBackgroundResource(0);
}
});
}
mHasDrawnOnce = true;
mPendingSwapBuffersCount = pendingSwapBuffersCount;
if (!mSkipInvalidation || pendingSwapBuffersCount == 0) flushInvalidation();
mSkipInvalidation = !mSkipInvalidation;
}
private void printViewsBehind(ViewGroup rootView) {
if (!this.isInEditMode() && !(rootView instanceof BlurBehindView) && rootView.getVisibility() == View.VISIBLE && rootView.getAlpha() != 0.0F) {
if (rootView.getBackground() != null) {
this.blurCanvas.save();
this.blurCanvas.translate((float) (this.childPositionInWindow[0] - this.thisPositionInWindow[0] + this.halfPaddingOnSides), (float) (this.halfPaddingOnSides + this.childPositionInWindow[1] - this.thisPositionInWindow[1]));
rootView.getBackground().draw(this.blurCanvas);
this.blurCanvas.restore();
}
for (int i = 0; i < rootView.getChildCount(); ++i) {
View childView = rootView.getChildAt(i);
if (childView.findViewWithTag(TAG_VIEW) != null & rootView.getVisibility() == View.VISIBLE) {
this.printViewsBehind((ViewGroup) childView);
} else if (childView.getVisibility() == View.VISIBLE) {
this.blurCanvas.save();
childView.getLocationOnScreen(this.childPositionInWindow);
this.blurCanvas.translate((float) (this.halfPaddingOnSides + this.childPositionInWindow[0] - this.thisPositionInWindow[0]), (float) (this.halfPaddingOnSides + this.childPositionInWindow[1] - this.thisPositionInWindow[1]));
this.blurCanvas.scale(childView.getScaleX(), childView.getScaleY());
childView.draw(this.blurCanvas);
this.blurCanvas.restore();
}
}
}
}
@Override
public void didSwapFrame(int pendingFrameCount) {
TraceEvent.instant("didSwapFrame");
// Wait until the second frame to turn off the placeholder background on
// tablets so the tab strip has time to start drawing.
final ViewGroup controlContainer = (ViewGroup) mControlContainer;
if (controlContainer != null && controlContainer.getBackground() != null && mHasDrawnOnce) {
post(new Runnable() {
@Override
public void run() {
controlContainer.setBackgroundResource(0);
}
});
}
mHasDrawnOnce = true;
mPendingFrameCount = pendingFrameCount;
if (!mSkipInvalidation || pendingFrameCount == 0) flushInvalidation();
mSkipInvalidation = !mSkipInvalidation;
}
@Override
protected void onTransitionsComplete() {
moveSharedElementsFromOverlay();
final ViewGroup decorView = getDecor();
if (decorView != null) {
decorView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Window window = getWindow();
if (window != null && mReplacedBackground == decorView.getBackground()) {
window.setBackgroundDrawable(null);
}
}
}
public void stop() {
// Restore the background to its previous state since the
// Activity is stopping.
if (mBackgroundAnimator != null) {
mBackgroundAnimator.end();
mBackgroundAnimator = null;
} else if (mWasOpaque) {
ViewGroup decorView = getDecor();
if (decorView != null) {
Drawable drawable = decorView.getBackground();
if (drawable != null) {
drawable.setAlpha(1);
}
}
}
makeOpaque();
mIsCanceled = true;
mResultReceiver = null;
mActivity = null;
moveSharedElementsFromOverlay();
if (mTransitioningViews != null) {
showViews(mTransitioningViews, true);
setTransitioningViewsVisiblity(View.VISIBLE, true);
}
showViews(mSharedElements, true);
clearState();
}
private void fadeOutBackground() {
if (mBackgroundAnimator == null) {
ViewGroup decor = getDecor();
Drawable background;
if (decor != null && (background = decor.getBackground()) != null) {
background = background.mutate();
getWindow().setBackgroundDrawable(background);
mBackgroundAnimator = ObjectAnimator.ofInt(background, "alpha", 0);
mBackgroundAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mBackgroundAnimator = null;
if (!mIsCanceled) {
mIsBackgroundReady = true;
notifyComplete();
}
backgroundAnimatorComplete();
}
});
mBackgroundAnimator.setDuration(getFadeDuration());
mBackgroundAnimator.start();
} else {
backgroundAnimatorComplete();
mIsBackgroundReady = true;
}
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mTextAndBackground = (ViewGroup) findViewById(R.id.text_and_background);
ColorDrawable colorBackground = (ColorDrawable) mTextAndBackground.getBackground();
mBackgroundColor = colorBackground.getColor();
RippleDrawable rippleBackground = new RippleDrawable(ColorStateList.valueOf(
ThemeUtils.getAttrColor(getContext(), android.R.attr.colorControlHighlight)),
colorBackground, null);
mTextAndBackground.setBackground(rippleBackground);
mTitleView = (TextView) mTextAndBackground.findViewById(R.id.title);
mTextView = (TextView) mTextAndBackground.findViewById(R.id.text);
}
@Override
public int getColor(ViewGroup view) {
Drawable bg = view.getBackground();
if(bg instanceof ColorDrawable){
return ((ColorDrawable) bg).getColor();
}
return 0;
}
public void startExit(int resultCode, Intent data) {
if (!mIsExitStarted) {
mIsExitStarted = true;
pauseInput();
ViewGroup decorView = getDecor();
if (decorView != null) {
decorView.suppressLayout(true);
}
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
mIsCanceled = true;
finish();
}
};
delayCancel();
moveSharedElementsToOverlay();
if (decorView != null && decorView.getBackground() == null) {
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
final boolean targetsM = decorView == null || decorView.getContext()
.getApplicationInfo().targetSdkVersion >= VERSION_CODES.M;
ArrayList<String> sharedElementNames = targetsM ? mSharedElementNames :
mAllSharedElementNames;
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(mActivity, this,
sharedElementNames, resultCode, data);
mActivity.convertToTranslucent(new Activity.TranslucentConversionListener() {
@Override
public void onTranslucentConversionComplete(boolean drawComplete) {
if (!mIsCanceled) {
fadeOutBackground();
}
}
}, options);
startTransition(new Runnable() {
@Override
public void run() {
startExitTransition();
}
});
}
}
public void init(ViewGroup vg, AttributeSet attrs){
mScaleType = TYPE_FIT_INSIDE;
String scaleTypeStr = null;
TypedArray a = null;
try{
a = vg.getContext().obtainStyledAttributes(
attrs, R.styleable.AutoScalingLayout);
// 获得设计宽高
mDesignWidth = a.getDimensionPixelOffset(R.styleable.AutoScalingLayout_designWidth, 0);
mDesignHeight = a.getDimensionPixelOffset(R.styleable.AutoScalingLayout_designHeight, 0);
// 是否开启自动缩放
mAutoScaleEnable = a.getBoolean(R.styleable.AutoScalingLayout_autoScaleEnable, true);
mPreScaling = a.getBoolean(R.styleable.AutoScalingLayout_preScaling, false);
scaleTypeStr = a.getString(R.styleable.AutoScalingLayout_autoScaleType);
}catch (Throwable e){
// 用户使用jar时,没有R.styleable.AutoScalingLayout,需要根据字符串解析参数
mAutoScaleEnable = true;
mDesignWidth = 0;
mDesignHeight = 0;
for (int i = 0; i < attrs.getAttributeCount(); i++){
if ("designWidth".equals(attrs.getAttributeName(i))){
String designWidthStr = attrs.getAttributeValue(i);
mDesignWidth = getDimensionPixelOffset(vg.getContext(), designWidthStr);
}
else if ("designHeight".equals(attrs.getAttributeName(i))) {
String designHeightStr = attrs.getAttributeValue(i);
mDesignHeight = getDimensionPixelOffset(vg.getContext(), designHeightStr);
}
else if ("autoScaleEnable".equals(attrs.getAttributeName(i))) {
String autoScaleEnableStr = attrs.getAttributeValue(i);
if (autoScaleEnableStr.equals("false"))
mAutoScaleEnable = false;
}
else if ("preScaling".equals(attrs.getAttributeName(i))) {
String preScalingStr = attrs.getAttributeValue(i);
if (preScalingStr.equals("true"))
mPreScaling = true;
}
else if ("autoScaleType".equals(attrs.getAttributeName(i))) {
scaleTypeStr = attrs.getAttributeValue(i);
}
}
}finally {
if(null != a)
a.recycle();
}
if (null != scaleTypeStr){
if (scaleTypeStr.equals("fitWidth"))
mScaleType = TYPE_FIT_WIDTH;
else if (scaleTypeStr.equals("fitHeight"))
mScaleType = TYPE_FIT_HEIGHT;
}
mCurrentWidth = mDesignWidth;
mCurrentHeight = mDesignHeight;
// 背景为空时,不进入draw函数,这里必须设置默认背景
if (null == vg.getBackground())
vg.setBackgroundColor(Color.TRANSPARENT);
if (DEBUG){
Log.v("AutoScalingLayout", "mDesignWidth=" + mDesignWidth + " mDesignHeight=" + mDesignHeight);
//Log.v("AutoScalingLayout", "1dp=" + getDimensionPixelOffset(vg.getContext(), "1dp") + "px");
}
}