From 2b8b3153c55b236923fcd090fe3903ec3ab2accc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=AF=E6=AD=8C?= Date: Tue, 7 Dec 2021 15:09:55 +0800 Subject: [PATCH] fix view.setScaleX/Y skip zero scaler --- src/gui/drawables/bitmapdrawable.cc | 13 ++++++----- src/gui/drawables/colordrawable.cc | 11 +++++----- src/gui/drawables/colorfilters.cc | 30 ++++++++++++++++++++++++-- src/gui/drawables/colorfilters.h | 1 + src/gui/drawables/drawable.cc | 4 +++- src/gui/drawables/gradientdrawable.cc | 2 +- src/gui/drawables/gradientdrawable.h | 1 + src/gui/drawables/ninepatchdrawable.cc | 20 ++++++++--------- src/gui/drawables/shapedrawable.cc | 11 +++++++--- src/gui/widget/tablayout.cc | 4 ++-- src/gui/widget/view.cc | 14 ++++++++---- src/gui/widget/view.h | 1 + 12 files changed, 77 insertions(+), 35 deletions(-) diff --git a/src/gui/drawables/bitmapdrawable.cc b/src/gui/drawables/bitmapdrawable.cc index 997df1b9..0c5d1af2 100755 --- a/src/gui/drawables/bitmapdrawable.cc +++ b/src/gui/drawables/bitmapdrawable.cc @@ -261,7 +261,7 @@ void BitmapDrawable::onBoundsChange(const Rect&r){ } bool BitmapDrawable::onStateChange(const std::vector&){ - if (mBitmapState->mTint /*&& mBitmapState->mTintMode != nullptr*/) { + if (mBitmapState->mTint && mBitmapState->mTintMode != TintMode::NONOP) { mTintFilter = updateTintFilter(mTintFilter, mBitmapState->mTint, mBitmapState->mTintMode); return true; } @@ -313,7 +313,7 @@ void BitmapDrawable::draw(Canvas&canvas){ RefPtrpats= SurfacePattern::create(subs); canvas.set_source(pats); if(mBounds.height>mBitmapHeight&&mBitmapState->mTileModeY==TileMode::DISABLED) - setPatternByTileMode(pats,TileMode::CLAMP);//mBitmapState->mTileModeY); + setPatternByTileMode(pats,TileMode::CLAMP); else setPatternByTileMode(pats,mBitmapState->mTileModeY); canvas.rectangle(mBounds.left,mBounds.top,mBounds.width,mBounds.height); canvas.fill(); @@ -334,12 +334,12 @@ void BitmapDrawable::draw(Canvas&canvas){ canvas.rectangle(mBounds.left,mBounds.top,mBounds.width,mBounds.height); canvas.fill(); } - }else{ + }else { const float sw=mBitmapWidth, sh = mBitmapHeight; float dx = mBounds.left , dy = mBounds.top; float dw = mBounds.width , dh = mBounds.height; const float fx = dw / sw , fy = dh / sh; - const float alpha=mBitmapState->mBaseAlpha*mBitmapState->mAlpha/255; + const float alpha=mBitmapState->mBaseAlpha*mBitmapState->mAlpha/255.f; canvas.rectangle(mBounds.left,mBounds.top,mBounds.width,mBounds.height); canvas.clip(); @@ -377,12 +377,11 @@ Drawable*BitmapDrawable::inflate(Context*ctx,const AttributeSet&atts){ {"disabled",TileMode::DISABLED}, {"clamp",TileMode::CLAMP}, {"repeat",TileMode::REPEAT}, {"mirror",TileMode::MIRROR}}; const int tileMode = atts.getInt("tileMode",kvs,-1); - int tileModeX= atts.getInt("tileModeX",kvs,tileMode); - int tileModeY= atts.getInt("tileModeY",kvs,tileMode); + const int tileModeX= atts.getInt("tileModeX",kvs,tileMode); + const int tileModeY= atts.getInt("tileModeY",kvs,tileMode); std::string path=src; if(ctx==nullptr)path=atts.getAbsolutePath(src); - LOGD("src=%s",src.c_str()); if(src.empty()) return nullptr; BitmapDrawable*d=new BitmapDrawable(ctx,path); LOGD("bitmap=%p",d); diff --git a/src/gui/drawables/colordrawable.cc b/src/gui/drawables/colordrawable.cc index 14f13fb3..0b6442ff 100755 --- a/src/gui/drawables/colordrawable.cc +++ b/src/gui/drawables/colordrawable.cc @@ -29,14 +29,14 @@ int ColorDrawable::ColorState::getChangingConfigurations()const{ ColorDrawable::ColorDrawable(int color){ mColorState=std::make_shared(); - mMutated=false; + mMutated=false; mTintFilter=nullptr; setColor(color); } ColorDrawable::ColorDrawable(std::shared_ptr state){ mColorState = state; - mMutated=false; + mMutated=false; } std::shared_ptrColorDrawable::getConstantState(){ @@ -83,7 +83,7 @@ void ColorDrawable::setAlpha(int alpha){ } bool ColorDrawable::onStateChange(const std::vector&stateSet){ - if (mColorState->mTint != nullptr/*& && state.mTintMode != null*/) { + if (mColorState->mTint && mColorState->mTintMode != TintMode::NONOP) { mTintFilter = updateTintFilter(mTintFilter, mColorState->mTint, mColorState->mTintMode); return true; } @@ -105,12 +105,13 @@ void ColorDrawable::setTintMode(int tintMode) { mTintFilter = updateTintFilter(mTintFilter, mColorState->mTint, tintMode); invalidateSelf(); } + bool ColorDrawable::isStateful()const{ - return mColorState->mTint && mColorState->mTint->isStateful(); + return mColorState->mTint && mColorState->mTint->isStateful(); } bool ColorDrawable::hasFocusStateSpecified()const{ - return mColorState->mTint && mColorState->mTint->hasFocusStateSpecified(); + return mColorState->mTint && mColorState->mTint->hasFocusStateSpecified(); } void ColorDrawable::draw(Canvas&canvas){ diff --git a/src/gui/drawables/colorfilters.cc b/src/gui/drawables/colorfilters.cc index fdcbfff2..43543a05 100755 --- a/src/gui/drawables/colorfilters.cc +++ b/src/gui/drawables/colorfilters.cc @@ -1,7 +1,33 @@ #include - +#include namespace cdroid{ +int ColorFilter::tintMode2CairoOperator(int tintMode){ + switch(tintMode){ + case NONOP :/*=-1*/ return CAIRO_OPERATOR_CLEAR; + case CLEAR :/*= 0*/ return CAIRO_OPERATOR_CLEAR; + case SRC :/*= 1*/ return CAIRO_OPERATOR_SOURCE; + case DST :/*= 2*/ return CAIRO_OPERATOR_DEST; + case SRC_OVER:/*= 3*/ return CAIRO_OPERATOR_OVER; + case DST_OVER:/*= 4*/ return CAIRO_OPERATOR_DEST_OVER; + case SRC_IN :/*= 5*/ return CAIRO_OPERATOR_IN; + case DST_IN :/*= 6*/ return CAIRO_OPERATOR_DEST_IN; + case SRC_OUT :/*= 7*/ return CAIRO_OPERATOR_OUT; + case DST_OUT :/*= 8*/ return CAIRO_OPERATOR_DEST_OUT; + case SRC_ATOP:/*= 9*/ return CAIRO_OPERATOR_ATOP; + case DST_ATOP:/*=10*/ return CAIRO_OPERATOR_DEST_ATOP; + case XOR :/*=11*/ return CAIRO_OPERATOR_XOR; + case ADD :/*=12*/ return CAIRO_OPERATOR_ADD; + case MULTIPLY:/*=13*/ return CAIRO_OPERATOR_MULTIPLY; + case SCREEN :/*=14*/ return CAIRO_OPERATOR_SCREEN; + case OVERLAY :/*=15*/ return CAIRO_OPERATOR_OVERLAY; + case DARKEN :/*=16*/ return CAIRO_OPERATOR_DARKEN; + case LIGHTEN :/*=17*/ return CAIRO_OPERATOR_LIGHTEN; + } + LOGD("TintMode %d is not support",tintMode); + return CAIRO_OPERATOR_SOURCE; +} + ColorMatrixColorFilter::ColorMatrixColorFilter(const float(&v)[20]){ mCM.set(v); } @@ -15,7 +41,7 @@ PorterDuffColorFilter::PorterDuffColorFilter(int color,int mode){ } void PorterDuffColorFilter::apply(Canvas&canvas,const Rect&rect){ - canvas.set_operator((Cairo::Context::Operator)mMode);//2,5(6,7),8,9 + canvas.set_operator((Cairo::Context::Operator)tintMode2CairoOperator(mMode));//2,5(6,7),8,9 canvas.set_color(mColor); canvas.rectangle(rect.left,rect.top,rect.width,rect.height); canvas.fill(); diff --git a/src/gui/drawables/colorfilters.h b/src/gui/drawables/colorfilters.h index 023004ff..8db3dacb 100755 --- a/src/gui/drawables/colorfilters.h +++ b/src/gui/drawables/colorfilters.h @@ -29,6 +29,7 @@ typedef TintMode PorterDuffMode; class ColorFilter{ public: virtual void apply(Canvas&canvas,const Rect&)=0; + static int tintMode2CairoOperator(int tintMode); }; class ColorMatrixColorFilter:public ColorFilter{ diff --git a/src/gui/drawables/drawable.cc b/src/gui/drawables/drawable.cc index 1c76dc73..a65f05fd 100755 --- a/src/gui/drawables/drawable.cc +++ b/src/gui/drawables/drawable.cc @@ -66,8 +66,10 @@ int Drawable::getOpacity(){ void Drawable::setHotspot(float x,float y){ } + void Drawable::setHotspotBounds(int left,int top,int width,int height){ } + void Drawable::getHotspotBounds(Rect&outRect){ outRect=mBounds; } @@ -97,7 +99,7 @@ void Drawable::setTint(int color){ } PorterDuffColorFilter *Drawable::updateTintFilter(PorterDuffColorFilter* tintFilter,ColorStateList* tint,int tintMode){ - if (tint == nullptr/*|| tintMode == nullptr*/) { + if (tint == nullptr|| tintMode == TintMode::NONOP) { return nullptr; } diff --git a/src/gui/drawables/gradientdrawable.cc b/src/gui/drawables/gradientdrawable.cc index 54c5b4ba..310c4586 100755 --- a/src/gui/drawables/gradientdrawable.cc +++ b/src/gui/drawables/gradientdrawable.cc @@ -66,7 +66,7 @@ GradientDrawable::GradientState::GradientState(const GradientState& orig){ mOpaqueOverBounds = orig.mOpaqueOverBounds; mOpaqueOverShape = orig.mOpaqueOverShape; mTint = orig.mTint; - //mTintMode = orig.mTintMode; + mTintMode = orig.mTintMode; //mThemeAttrs = orig.mThemeAttrs; mAttrSize = orig.mAttrSize; mAttrGradient = orig.mAttrGradient; diff --git a/src/gui/drawables/gradientdrawable.h b/src/gui/drawables/gradientdrawable.h index a515027d..1012e479 100755 --- a/src/gui/drawables/gradientdrawable.h +++ b/src/gui/drawables/gradientdrawable.h @@ -74,6 +74,7 @@ private: bool mOpaqueOverBounds; bool mOpaqueOverShape; ColorStateList*mTint; + int mTintMode; int mDensity; std::vectormAttrSize; std::vectormAttrGradient; diff --git a/src/gui/drawables/ninepatchdrawable.cc b/src/gui/drawables/ninepatchdrawable.cc index bd46f4a4..0dac1bfb 100755 --- a/src/gui/drawables/ninepatchdrawable.cc +++ b/src/gui/drawables/ninepatchdrawable.cc @@ -6,13 +6,13 @@ namespace cdroid{ //https://github.com/soramimi/QtNinePatch/blob/master/NinePatch.cpp NinePatchDrawable::NinePatchState::NinePatchState(){ - mTint=nullptr; + mTint = nullptr; mBaseAlpha=1.0f; - mDither=true; - mTint=nullptr; - mTintMode=DEFAULT_TINT_MODE; - mChangingConfigurations=0; - mAutoMirrored=false; + mDither = true; + mTint = nullptr; + mTintMode = DEFAULT_TINT_MODE; + mChangingConfigurations = 0; + mAutoMirrored =false; mPadding.set(0,0,0,0); mOpticalInsets.set(0,0,0,0); } @@ -187,19 +187,19 @@ Drawable* NinePatchDrawable::mutate() { } bool NinePatchDrawable::onStateChange(const std::vector& stateSet){ - if (mNinePatchState->mTint != nullptr ){//&& mNinePatchState->mTintMode != nullptr) { - //mTintFilter = updateTintFilter(mTintFilter, mNinePatchState->mTint, mNinePatchState->mTintMode); + if (mNinePatchState->mTint && mNinePatchState->mTintMode != TintMode::NONOP) { + mTintFilter = updateTintFilter(mTintFilter, mNinePatchState->mTint, mNinePatchState->mTintMode); return true; } return false; } bool NinePatchDrawable::isStateful()const{ - return Drawable::isStateful() || (mNinePatchState->mTint != nullptr && mNinePatchState->mTint->isStateful()); + return Drawable::isStateful() || (mNinePatchState->mTint && mNinePatchState->mTint->isStateful()); } bool NinePatchDrawable::hasFocusStateSpecified()const { - return mNinePatchState->mTint != nullptr && mNinePatchState->mTint->hasFocusStateSpecified(); + return mNinePatchState->mTint && mNinePatchState->mTint->hasFocusStateSpecified(); } std::shared_ptrNinePatchDrawable::getConstantState(){ diff --git a/src/gui/drawables/shapedrawable.cc b/src/gui/drawables/shapedrawable.cc index 7a6c30e0..2754174e 100755 --- a/src/gui/drawables/shapedrawable.cc +++ b/src/gui/drawables/shapedrawable.cc @@ -4,6 +4,8 @@ namespace cdroid{ ShapeDrawable::ShapeState::ShapeState(){ mAlpha=255; mShape=nullptr; + mTint =nullptr; + mTintMode =TintMode::NONOP; mIntrinsicWidth=0; mIntrinsicHeight=0; mPadding.set(0,0,0,0); @@ -13,8 +15,10 @@ ShapeDrawable::ShapeState::ShapeState(const ShapeState&orig){ mIntrinsicWidth = orig.mIntrinsicWidth; mIntrinsicHeight = orig.mIntrinsicHeight; mPadding=orig.mPadding; - mAlpha = orig.mAlpha; - mShape = orig.mShape?orig.mShape->clone():nullptr; + mAlpha = orig.mAlpha; + mShape = orig.mShape?orig.mShape->clone():nullptr; + mTint = new ColorStateList(*orig.mTint); + mTintMode =orig.mTintMode; } Drawable* ShapeDrawable::ShapeState::newDrawable(){ @@ -23,6 +27,7 @@ Drawable* ShapeDrawable::ShapeState::newDrawable(){ ShapeDrawable::ShapeState::~ShapeState(){ delete mShape; + delete mTint; } int ShapeDrawable::ShapeState::getChangingConfigurations()const{ @@ -152,7 +157,7 @@ void ShapeDrawable::setIntrinsicHeight(int height){ } void ShapeDrawable::updateLocalState(){ - //mTintFilter = updateTintFilter(mTintFilter, mShapeState.mTint, mShapeState.mTintMode); + mTintFilter = updateTintFilter(mTintFilter, mShapeState->mTint, mShapeState->mTintMode); } Drawable*ShapeDrawable::mutate(){ diff --git a/src/gui/widget/tablayout.cc b/src/gui/widget/tablayout.cc index 51a5c5cc..f30588d3 100755 --- a/src/gui/widget/tablayout.cc +++ b/src/gui/widget/tablayout.cc @@ -499,7 +499,7 @@ void TabLayout::animateToTab(int newPosition){ if (startScrollX != targetScrollX) { ensureScrollAnimator(); - mScrollAnimator->setIntValues({startScrollX, targetScrollX}); + mScrollAnimator->setFloatValues({(float)startScrollX, (float)targetScrollX}); mScrollAnimator->start(); } @@ -514,7 +514,7 @@ void TabLayout::ensureScrollAnimator(){ mScrollAnimator->setDuration(ANIMATION_DURATION); mScrollAnimator->addUpdateListener(ValueAnimator::AnimatorUpdateListener([this](ValueAnimator&anim) { PropertyValuesHolder*ip=anim.getValues()[0]; - scrollTo(ip->getAnimatedValue().get(), 0); + scrollTo(ip->getAnimatedValue().get(), 0); })); } } diff --git a/src/gui/widget/view.cc b/src/gui/widget/view.cc index 2a6c87e6..0f2c5ea6 100755 --- a/src/gui/widget/view.cc +++ b/src/gui/widget/view.cc @@ -59,6 +59,13 @@ public: } }; +View::TransformationInfo::TransformationInfo(){ + mMatrix=identity_matrix(); + mInverseMatrix=identity_matrix(); + mAlpha=1.f; + mTransitionAlpha =1.f; +} + class ScrollabilityCache:public Runnable{ private: static constexpr float OPAQUE[] = { 255 }; @@ -5395,7 +5402,6 @@ int View::resolveSizeAndState(int size, int measureSpec, int childMeasuredState) void View::ensureTransformationInfo(){ if (mTransformationInfo == nullptr) { mTransformationInfo = new TransformationInfo(); - mTransformationInfo->mMatrix =identity_matrix(); } } @@ -5413,7 +5419,7 @@ static inline float sdot(float a,float b,float c,float d){ Matrix View::getMatrix() { ensureTransformationInfo(); //mRenderNode.getMatrix(matrix); - Matrix matrix=mTransformationInfo->mMatrix;//identity_matrix(); + Matrix matrix=mTransformationInfo->mMatrix; matrix.translate(mTranslationX,mTranslationY); matrix.scale(mScaleX,mScaleY); @@ -5465,7 +5471,7 @@ void View::setY(float y){ void View::setScaleX(float x){ invalidateViewProperty(true,false); - mScaleX = x; + mScaleX = (x==.0f)?.00001f:x;//scale cant be zero invalidateViewProperty(false,true); } @@ -5475,7 +5481,7 @@ float View::getScaleX()const{ void View::setScaleY(float y){ invalidateViewProperty(true,false); - mScaleY = y; + mScaleY = (y==.0f)?.00001f:y;//scale cant be zero invalidateViewProperty(false,true); } diff --git a/src/gui/widget/view.h b/src/gui/widget/view.h index f76151a5..9f81511f 100755 --- a/src/gui/widget/view.h +++ b/src/gui/widget/view.h @@ -69,6 +69,7 @@ public: Matrix mInverseMatrix; float mAlpha = 1.f; float mTransitionAlpha = 1.f; + TransformationInfo(); }; protected: //PFLAGS in mPrivateFlags