From 83ab7ee80c1894e45e037f09c5d0724b62c84c8d Mon Sep 17 00:00:00 2001 From: houzh Date: Sat, 13 Jan 2024 16:50:09 +0800 Subject: [PATCH] add InsetDrawable::getIntrinsicWidth/Height --- src/gui/drawables/drawable.cc | 6 +++--- src/gui/drawables/drawablewrapper.h | 2 +- src/gui/drawables/insetdrawable.cc | 20 ++++++++++++++++++++ src/gui/drawables/insetdrawable.h | 2 ++ src/gui/drawables/levellistdrawable.cc | 3 ++- src/gui/widgetEx/recyclerview/snaphelper.cc | 2 ++ 6 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/gui/drawables/drawable.cc b/src/gui/drawables/drawable.cc index bc313dfe..f69ebb0e 100644 --- a/src/gui/drawables/drawable.cc +++ b/src/gui/drawables/drawable.cc @@ -535,10 +535,10 @@ static Drawable*parseShapeDrawable(Context*ctx,const AttributeSet&atts, if(stroke) { d->setStroke(stroke->getDimensionPixelSize("width",1),stroke->getColorStateList("color"), - stroke->getDimensionPixelSize("dashWidth"),stroke->getDimensionPixelSize("dashGap")); + stroke->getDimensionPixelSize("dashWidth"),stroke->getDimensionPixelSize("dashGap")); } - if(padding)d->setPadding(padding->getInt("left"),padding->getInt("top"), - padding->getInt("right"),padding->getInt("bottom")); + if(padding)d->setPadding(padding->getDimensionPixelSize("left"),padding->getDimensionPixelSize("top"), + padding->getDimensionPixelSize("right"),padding->getDimensionPixelSize("bottom")); return d; } else { ShapeDrawable*sd=new ShapeDrawable(); diff --git a/src/gui/drawables/drawablewrapper.h b/src/gui/drawables/drawablewrapper.h index 90fa9c6f..81386c36 100755 --- a/src/gui/drawables/drawablewrapper.h +++ b/src/gui/drawables/drawablewrapper.h @@ -39,7 +39,7 @@ public: int getIntrinsicHeight()const override; int getChangingConfigurations()const override; void setDrawable(Drawable* dr); - Drawable* getDrawable(); + Drawable* getDrawable()const; DrawableWrapper*mutate()override; void clearMutated()override; std::shared_ptrgetConstantState()override; diff --git a/src/gui/drawables/insetdrawable.cc b/src/gui/drawables/insetdrawable.cc index 6ddd54c9..0dcac8d6 100755 --- a/src/gui/drawables/insetdrawable.cc +++ b/src/gui/drawables/insetdrawable.cc @@ -114,6 +114,26 @@ void InsetDrawable::onBoundsChange(const Rect&bounds){ DrawableWrapper::onBoundsChange(r); } +int InsetDrawable::getIntrinsicWidth()const { + const int childWidth = getDrawable()->getIntrinsicWidth(); + const float fraction = mState->mInsetLeft->mFraction + mState->mInsetRight.mFraction; + if (childWidth < 0 || fraction >= 1) { + return -1; + } + return (int) (childWidth / (1.f - fraction)) + mState->mInsetLeft.mDimension + + mState->mInsetRight.mDimension; +} + +int InsetDrawable::getIntrinsicHeight()const { + const int childHeight = getDrawable()->getIntrinsicHeight(); + const float fraction = mState->mInsetTop.mFraction + mState->mInsetBottom.mFraction; + if (childHeight < 0 || fraction >= 1) { + return -1; + } + return (int) (childHeight / (1.f - fraction)) + mState->mInsetTop.mDimension + + mState->mInsetBottom.mDimension; +} + std::shared_ptrInsetDrawable::getConstantState(){ return mState; } diff --git a/src/gui/drawables/insetdrawable.h b/src/gui/drawables/insetdrawable.h index c485d7ec..856952a4 100755 --- a/src/gui/drawables/insetdrawable.h +++ b/src/gui/drawables/insetdrawable.h @@ -38,6 +38,8 @@ public: InsetDrawable(Drawable*drawable,int inset); InsetDrawable(Drawable* drawable,int insetLeft,int insetTop,int insetRight,int insetBottom); std::shared_ptrgetConstantState()override; + int getIntrinsicWidth()const override; + int getIntrinsicHeight()const override; bool getPadding(Rect& padding)override; int getOpacity()override; Insets getOpticalInsets()override; diff --git a/src/gui/drawables/levellistdrawable.cc b/src/gui/drawables/levellistdrawable.cc index 47b0fe06..d0e0b840 100755 --- a/src/gui/drawables/levellistdrawable.cc +++ b/src/gui/drawables/levellistdrawable.cc @@ -45,7 +45,8 @@ LevelListDrawable::LevelListDrawable(std::shared_ptrstate){ } bool LevelListDrawable::onLevelChange(int level){ - int idx = mLevelListState->indexOfLevel(level); + const int idx = mLevelListState->indexOfLevel(level); + LOGV("%p level %d.index=%d",this,level,idx); if (selectDrawable(idx)) { return true; } diff --git a/src/gui/widgetEx/recyclerview/snaphelper.cc b/src/gui/widgetEx/recyclerview/snaphelper.cc index 2e8445de..b8a10593 100755 --- a/src/gui/widgetEx/recyclerview/snaphelper.cc +++ b/src/gui/widgetEx/recyclerview/snaphelper.cc @@ -6,6 +6,8 @@ namespace cdroid{ SnapHelper::SnapHelper(){ mScrolled = false; + mRecyclerView = nullptr; + mGravityScroller = nullptr; mScrollListener.onScrollStateChanged=[this](RecyclerView& recyclerView, int newState) { //super is null;super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView::SCROLL_STATE_IDLE && mScrolled) {