fix view.setScaleX/Y skip zero scaler

This commit is contained in:
侯歌 2021-12-07 15:09:55 +08:00
parent 75708c4208
commit 2b8b3153c5
12 changed files with 77 additions and 35 deletions

View File

@ -261,7 +261,7 @@ void BitmapDrawable::onBoundsChange(const Rect&r){
}
bool BitmapDrawable::onStateChange(const std::vector<int>&){
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){
RefPtr<SurfacePattern>pats= 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);

View File

@ -29,14 +29,14 @@ int ColorDrawable::ColorState::getChangingConfigurations()const{
ColorDrawable::ColorDrawable(int color){
mColorState=std::make_shared<ColorState>();
mMutated=false;
mMutated=false;
mTintFilter=nullptr;
setColor(color);
}
ColorDrawable::ColorDrawable(std::shared_ptr<ColorState> state){
mColorState = state;
mMutated=false;
mMutated=false;
}
std::shared_ptr<Drawable::ConstantState>ColorDrawable::getConstantState(){
@ -83,7 +83,7 @@ void ColorDrawable::setAlpha(int alpha){
}
bool ColorDrawable::onStateChange(const std::vector<int>&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){

View File

@ -1,7 +1,33 @@
#include <drawables/colorfilters.h>
#include <cdlog.h>
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();

View File

@ -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{

View File

@ -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;
}

View File

@ -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;

View File

@ -74,6 +74,7 @@ private:
bool mOpaqueOverBounds;
bool mOpaqueOverShape;
ColorStateList*mTint;
int mTintMode;
int mDensity;
std::vector<int>mAttrSize;
std::vector<int>mAttrGradient;

View File

@ -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<int>& 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_ptr<Drawable::ConstantState>NinePatchDrawable::getConstantState(){

View File

@ -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(){

View File

@ -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<int>(), 0);
scrollTo(ip->getAnimatedValue().get<float>(), 0);
}));
}
}

View File

@ -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);
}

View File

@ -69,6 +69,7 @@ public:
Matrix mInverseMatrix;
float mAlpha = 1.f;
float mTransitionAlpha = 1.f;
TransformationInfo();
};
protected:
//PFLAGS in mPrivateFlags