modify valueanimator,propertyvaluesholder

This commit is contained in:
侯歌 2021-09-07 18:12:21 +08:00
parent faf8720d31
commit 20280c1c0f
7 changed files with 139 additions and 59 deletions

View File

@ -63,7 +63,6 @@ foreach( each_file ${allfiles} )
STRING(LENGTH ${FILE_PATH} LEN1) STRING(LENGTH ${FILE_PATH} LEN1)
MATH(EXPR LEN0 "${LEN0}-${LEN1}") MATH(EXPR LEN0 "${LEN0}-${LEN1}")
STRING(SUBSTRING ${each_file} 0 ${LEN0} FILE_PATH) STRING(SUBSTRING ${each_file} 0 ${LEN0} FILE_PATH)
message("${each_file}==>${FILE_PATH}")
file(COPY ${PROJECT_SOURCE_DIR}/${each_file} DESTINATION "${CMAKE_BINARY_DIR}/include/gui/${FILE_PATH}") file(COPY ${PROJECT_SOURCE_DIR}/${each_file} DESTINATION "${CMAKE_BINARY_DIR}/include/gui/${FILE_PATH}")
install(FILES ${PROJECT_SOURCE_DIR}/${each_file} DESTINATION "${CMAKE_BINARY_DIR}/include/gui/${FILE_PATH}") install(FILES ${PROJECT_SOURCE_DIR}/${each_file} DESTINATION "${CMAKE_BINARY_DIR}/include/gui/${FILE_PATH}")
endforeach(each_file) endforeach(each_file)

View File

@ -71,9 +71,9 @@ void ObjectAnimator::setIntValues(const std::vector<int>&values){
// No values yet - this animator is being constructed piecemeal. Init the values with // No values yet - this animator is being constructed piecemeal. Init the values with
// whatever the current propertyName is // whatever the current propertyName is
if (mProperty) { if (mProperty) {
//setValues({PropertyValuesHolder::ofInt(mProperty, values)}); setValues({PropertyValuesHolder::ofInt(mProperty, values)});
} else { } else {
//setValues({PropertyValuesHolder::ofInt(mPropertyName, values)}); setValues({PropertyValuesHolder::ofInt(mPropertyName, values)});
} }
} else { } else {
ValueAnimator::setIntValues(values); ValueAnimator::setIntValues(values);
@ -85,9 +85,9 @@ void ObjectAnimator::setFloatValues(const std::vector<float>&values){
// No values yet - this animator is being constructed piecemeal. Init the values with // No values yet - this animator is being constructed piecemeal. Init the values with
// whatever the current propertyName is // whatever the current propertyName is
if (mProperty) { if (mProperty) {
//setValues({PropertyValuesHolder::ofFloat(mProperty, values)}); setValues({PropertyValuesHolder::ofFloat(mProperty, values)});
} else { } else {
//setValues({PropertyValuesHolder::ofFloat(mPropertyName, values)}); setValues({PropertyValuesHolder::ofFloat(mPropertyName, values)});
} }
} else { } else {
ValueAnimator::setFloatValues(values); ValueAnimator::setFloatValues(values);

View File

@ -0,0 +1,79 @@
#include <animation/propertyvaluesholder.h>
namespace cdroid{
PropertyValuesHolder::PropertyValuesHolder(){
mProperty=nullptr;
}
PropertyValuesHolder::PropertyValuesHolder(Property*prop){
mProperty=prop;
}
PropertyValuesHolder::PropertyValuesHolder(const std::string&name){
mPropertyName=name;
mProperty=nullptr;
}
void PropertyValuesHolder::setPropertyName(const std::string& propertyName){
mPropertyName=propertyName;
}
const std::string PropertyValuesHolder::getPropertyName()const{
return mPropertyName;
}
PropertyValuesHolder::~PropertyValuesHolder(){
delete mProperty;
}
PropertyValuesHolder* PropertyValuesHolder::ofInt(const std::string&name,const std::vector<int>&values){
IntPropertyValuesHolder*ip=new IntPropertyValuesHolder(name);
ip->setValues(values);
return ip;
}
PropertyValuesHolder* PropertyValuesHolder::ofInt(Property*prop,const std::vector<int>&values){
IntPropertyValuesHolder*ip=new IntPropertyValuesHolder(prop);
ip->setValues(values);
return ip;
}
PropertyValuesHolder* PropertyValuesHolder::ofFloat(const std::string&name,const std::vector<float>&values){
FloatPropertyValuesHolder*fp=new FloatPropertyValuesHolder(name);
fp->setValues(values);
return fp;
}
PropertyValuesHolder* PropertyValuesHolder::ofFloat(Property*prop,const std::vector<float>&values){
FloatPropertyValuesHolder*fp=new FloatPropertyValuesHolder(prop);
fp->setValues(values);
return fp;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
IntPropertyValuesHolder::IntPropertyValuesHolder():PropertyValuesHolderImpl<int>(){
mEvaluator=new IntEvaluator();
}
IntPropertyValuesHolder::IntPropertyValuesHolder(const std::string&name):PropertyValuesHolderImpl<int>(name){
mEvaluator=new IntEvaluator();
}
IntPropertyValuesHolder::IntPropertyValuesHolder(Property*prop):PropertyValuesHolderImpl<int>(prop){
mEvaluator=new IntEvaluator();
}
FloatPropertyValuesHolder::FloatPropertyValuesHolder():PropertyValuesHolderImpl<float>(){
mEvaluator=new FloatEvaluator();
}
FloatPropertyValuesHolder::FloatPropertyValuesHolder(const std::string&name):PropertyValuesHolderImpl<float>(name){
mEvaluator=new FloatEvaluator();
}
FloatPropertyValuesHolder::FloatPropertyValuesHolder(Property*prop):PropertyValuesHolderImpl<float>(prop){
mEvaluator=new FloatEvaluator();
}
}//endof namespace

View File

@ -18,19 +18,17 @@ protected:
std::string mPropertyName; std::string mPropertyName;
Property*mProperty; Property*mProperty;
public: public:
PropertyValuesHolder(){ PropertyValuesHolder();
mProperty=nullptr; PropertyValuesHolder(Property*prop);
} PropertyValuesHolder(const std::string&name);
void setPropertyName(const std::string& propertyName){ void setPropertyName(const std::string& propertyName);
mPropertyName=propertyName; const std::string getPropertyName()const;
}
const std::string getPropertyName()const{
return mPropertyName;
}
virtual void setFraction(void*target,float fraction)=0; virtual void setFraction(void*target,float fraction)=0;
virtual ~PropertyValuesHolder(){ virtual ~PropertyValuesHolder();
delete mProperty; static PropertyValuesHolder*ofInt(const std::string&name,const std::vector<int>&);
} static PropertyValuesHolder*ofInt(Property*,const std::vector<int>&);
static PropertyValuesHolder*ofFloat(const std::string&name,const std::vector<float>&);
static PropertyValuesHolder*ofFloat(Property*prop,const std::vector<float>&);
}; };
template <typename T> template <typename T>
@ -76,20 +74,18 @@ public:
template<typename T> template<typename T>
class PropertyValuesHolderImpl:public PropertyValuesHolder{ class PropertyValuesHolderImpl:public PropertyValuesHolder{
public:
typedef std::function < void(void*target,float fraction,const T&value) > PropertySetter;
protected: protected:
Evaluator<T>* mEvaluator; Evaluator<T>* mEvaluator;
std::vector<T>mDataSource; std::vector<T>mDataSource;
T mStartValue; T mStartValue;
T mEndValue; T mEndValue;
T mAnimateValue; T mAnimateValue;
PropertySetter mPropSetter;
public: public:
PropertyValuesHolderImpl(){ PropertyValuesHolderImpl(){
mEvaluator=nullptr; mEvaluator=nullptr;
mPropSetter=nullptr;
} }
PropertyValuesHolderImpl(const std::string&name):PropertyValuesHolder(name){}
PropertyValuesHolderImpl(Property*prop):PropertyValuesHolder(prop){}
PropertyValuesHolderImpl(const T& startValue, const T& endValue) PropertyValuesHolderImpl(const T& startValue, const T& endValue)
: mStartValue(startValue), mEndValue(endValue) {} : mStartValue(startValue), mEndValue(endValue) {}
void setValues(const T* dataSource, int length) { void setValues(const T* dataSource, int length) {
@ -98,9 +94,6 @@ public:
void setValues(const std::vector<T>&values){ void setValues(const std::vector<T>&values){
mDataSource=values; mDataSource=values;
} }
void setPropertySetter(PropertySetter setter){
mPropSetter=setter;
}
void setFraction(void*target,float fraction)override{ void setFraction(void*target,float fraction)override{
if (mDataSource.size()==0) mEvaluator->evaluate(&mAnimateValue, mStartValue, mEndValue, fraction); if (mDataSource.size()==0) mEvaluator->evaluate(&mAnimateValue, mStartValue, mEndValue, fraction);
else if (fraction <= 0.0f) mAnimateValue=mDataSource.front(); else if (fraction <= 0.0f) mAnimateValue=mDataSource.front();
@ -111,7 +104,6 @@ public:
fraction -= lowIndex; fraction -= lowIndex;
mEvaluator->evaluate(&mAnimateValue, mDataSource[lowIndex], mDataSource[lowIndex + 1], fraction); mEvaluator->evaluate(&mAnimateValue, mDataSource[lowIndex], mDataSource[lowIndex + 1], fraction);
} }
if(mPropSetter)mPropSetter(target,fraction,mAnimateValue);
} }
T getAnimatedValue()const{return mAnimateValue;} T getAnimatedValue()const{return mAnimateValue;}
}; };
@ -119,16 +111,16 @@ public:
#if 1 #if 1
class IntPropertyValuesHolder:public PropertyValuesHolderImpl<int>{ class IntPropertyValuesHolder:public PropertyValuesHolderImpl<int>{
public: public:
IntPropertyValuesHolder():PropertyValuesHolderImpl<int>(){ IntPropertyValuesHolder();
mEvaluator=new IntEvaluator(); IntPropertyValuesHolder(const std::string&);
} IntPropertyValuesHolder(Property*);
}; };
class FloatPropertyValuesHolder:public PropertyValuesHolderImpl<float>{ class FloatPropertyValuesHolder:public PropertyValuesHolderImpl<float>{
public: public:
FloatPropertyValuesHolder():PropertyValuesHolderImpl<float>(){ FloatPropertyValuesHolder();
mEvaluator=new FloatEvaluator(); FloatPropertyValuesHolder(const std::string&);
} FloatPropertyValuesHolder(Property*);
}; };
class ColorPropertyValuesHolder:public PropertyValuesHolderImpl<uint32_t>{ class ColorPropertyValuesHolder:public PropertyValuesHolderImpl<uint32_t>{
public: public:

View File

@ -174,16 +174,16 @@ void ProgressBar::doRefreshProgress(int id, int progress, bool fromUser,bool cal
if (isPrimary && animate) { if (isPrimary && animate) {
FloatPropertyValuesHolder*prop=nullptr; FloatPropertyValuesHolder*prop=nullptr;
if(mAnimator==nullptr){ if(mAnimator==nullptr){
prop=new FloatPropertyValuesHolder(); mAnimator = ObjectAnimator::ofFloat(this,"progress",{scale});
prop->setPropertySetter([&](void*target,float fraction,float v){
setVisualProgress(ID_PRIMARY, v);
LOGV("setVisualProgress(%d)->%f",ID_PRIMARY,v);
});
mAnimator = ObjectAnimator::ofPropertyValuesHolder(this,{prop});
mAnimator->setAutoCancel(true); mAnimator->setAutoCancel(true);
mAnimator->setDuration(PROGRESS_ANIM_DURATION); mAnimator->setDuration(PROGRESS_ANIM_DURATION);
mAnimator->setInterpolator(new DecelerateInterpolator()); mAnimator->setInterpolator(new DecelerateInterpolator());
}else prop=(FloatPropertyValuesHolder*)mAnimator->getValues()[0]; mAnimator->addUpdateListener([this](ValueAnimator&anim){
FloatPropertyValuesHolder*fp=(FloatPropertyValuesHolder*)anim.getValues(0);
setVisualProgress(ID_PRIMARY,fp->getAnimatedValue());
});
}
prop=(FloatPropertyValuesHolder*)mAnimator->getValues(0);
prop->setValues({scale}); prop->setValues({scale});
mAnimator->start(); mAnimator->start();
} else { } else {

View File

@ -86,7 +86,7 @@ protected:
virtual void onVisualProgressChanged(int id, float progress); virtual void onVisualProgressChanged(int id, float progress);
virtual void onDraw(Canvas&canvas)override; virtual void onDraw(Canvas&canvas)override;
public: public:
ProgressBar(Context*ctx,const AttributeSet&attrs); ProgressBar(Context*ctx,const AttributeSet&attrs);
ProgressBar(int width, int height); ProgressBar(int width, int height);
~ProgressBar(); ~ProgressBar();
void setMin(int value); void setMin(int value);

View File

@ -25,10 +25,11 @@ TEST_F(ANIMATOR,callback){
TEST_F(ANIMATOR,ofInt){ TEST_F(ANIMATOR,ofInt){
IntPropertyValuesHolder iprop; IntPropertyValuesHolder iprop;
iprop.setValues({0,100}); iprop.setValues({0,100});
iprop.setPropertySetter([](void*target,float fraction,int v){
LOGD("fraction=%f value=%d",fraction,v);
});
ValueAnimator*anim=ValueAnimator::ofPropertyValuesHolder({&iprop}); ValueAnimator*anim=ValueAnimator::ofPropertyValuesHolder({&iprop});
anim->addUpdateListener([this](ValueAnimator&anim){
IntPropertyValuesHolder*ip=(IntPropertyValuesHolder*)anim.getValues(0);
LOGD("value=%d",ip->getAnimatedValue());
});
for(int i=0;i<=10;i++){ for(int i=0;i<=10;i++){
anim->setCurrentFraction((float)i/10.f); anim->setCurrentFraction((float)i/10.f);
} }
@ -37,10 +38,11 @@ TEST_F(ANIMATOR,ofInt){
TEST_F(ANIMATOR,ofFloat){ TEST_F(ANIMATOR,ofFloat){
FloatPropertyValuesHolder fprop; FloatPropertyValuesHolder fprop;
fprop.setValues({0,100}); fprop.setValues({0,100});
fprop.setPropertySetter([](void*target,float fraction,float v){
LOGD("fraction=%f value=%f",fraction,v);
});
ValueAnimator*anim=ValueAnimator::ofPropertyValuesHolder({&fprop}); ValueAnimator*anim=ValueAnimator::ofPropertyValuesHolder({&fprop});
anim->addUpdateListener([this](ValueAnimator&anim){
FloatPropertyValuesHolder*fp=(FloatPropertyValuesHolder*)anim.getValues(0);
LOGD("value=%f",fp->getAnimatedValue());
});
for(int i=0;i<=10;i++){ for(int i=0;i<=10;i++){
anim->setCurrentFraction((float)i/10.f); anim->setCurrentFraction((float)i/10.f);
} }
@ -50,11 +52,12 @@ TEST_F(ANIMATOR,loopdrivered){
App app; App app;
IntPropertyValuesHolder iprop; IntPropertyValuesHolder iprop;
iprop.setValues({0,100}); iprop.setValues({0,100});
iprop.setPropertySetter([](void*target,float fraction,int v){
LOGD("fraction=%f value=%d",fraction,v);
});
ValueAnimator*anim=ValueAnimator::ofPropertyValuesHolder({&iprop}); ValueAnimator*anim=ValueAnimator::ofPropertyValuesHolder({&iprop});
anim->addUpdateListener([this](ValueAnimator&anim){
IntPropertyValuesHolder*fp=(IntPropertyValuesHolder*)anim.getValues(0);
LOGD("value=%d",fp->getAnimatedValue());
});
anim->setDuration(2000); anim->setDuration(2000);
anim->start(); anim->start();
app.exec(); app.exec();
@ -66,26 +69,28 @@ TEST_F(ANIMATOR,translate){
TextView*tv=new TextView("Hello World!",120,30); TextView*tv=new TextView("Hello World!",120,30);
tv->setBackgroundColor(0xFF111111); tv->setBackgroundColor(0xFF111111);
w->addView(tv); w->addView(tv);
IntPropertyValuesHolder xprop; IntPropertyValuesHolder xprop;
xprop.setPropertyName("x"); xprop.setPropertyName("x");
xprop.setValues({0,100,300}); xprop.setValues({0,100,300});
xprop.setPropertySetter([&](void*target,float fraction,int v){
tv->setPos(v,tv->getTop());
});
IntPropertyValuesHolder yprop; IntPropertyValuesHolder yprop;
yprop.setPropertyName("y"); yprop.setPropertyName("y");
yprop.setValues({0,200,200}); yprop.setValues({0,200,200});
yprop.setPropertySetter([&](void*target,float fraction,int v){
tv->setPos(tv->getLeft(),v);
});
ColorPropertyValuesHolder cprop; ColorPropertyValuesHolder cprop;
cprop.setValues({0xFF000000,0xFFFF8844}); cprop.setValues({0xFF000000,0xFFFF8844});
cprop.setPropertySetter([&](void*target,float fraction,uint32_t v){
tv->setBackgroundColor(v);
});
ValueAnimator*anim=ValueAnimator::ofPropertyValuesHolder({&xprop,&yprop,&cprop}); ValueAnimator*anim=ValueAnimator::ofPropertyValuesHolder({&xprop,&yprop,&cprop});
anim->addUpdateListener([tv](ValueAnimator&anim){
IntPropertyValuesHolder*xp=(IntPropertyValuesHolder*)anim.getValues(0);
IntPropertyValuesHolder*yp=(IntPropertyValuesHolder*)anim.getValues(1);
ColorPropertyValuesHolder*cp=(ColorPropertyValuesHolder*)anim.getValues(2);
tv->setPos(xp->getAnimatedValue(),tv->getTop());
tv->setPos(tv->getLeft(),yp->getAnimatedValue());
tv->setBackgroundColor(cp->getAnimatedValue());
});
anim->setDuration(5000); anim->setDuration(5000);
anim->start(); anim->start();
app.exec(); app.exec();
@ -96,15 +101,20 @@ TEST_F(ANIMATOR,scale){
TextView*tv=new TextView("Hello World!",120,30); TextView*tv=new TextView("Hello World!",120,30);
tv->setBackgroundColor(0xFF111111); tv->setBackgroundColor(0xFF111111);
w->addView(tv); w->addView(tv);
FloatPropertyValuesHolder fprop; FloatPropertyValuesHolder fprop;
fprop.setPropertyName("scale"); fprop.setPropertyName("scale");
fprop.setValues({0,2.0}); fprop.setValues({0,2.0});
fprop.setPropertySetter([&](void*target,float fraction,float scale){
ValueAnimator*anim=ValueAnimator::ofPropertyValuesHolder({&fprop});
anim->addUpdateListener([tv](ValueAnimator&anim){
FloatPropertyValuesHolder*fp=(FloatPropertyValuesHolder*)anim.getValues(0);
float scale=fp->getAnimatedValue();
tv->setScaleX(scale); tv->setScaleX(scale);
tv->setScaleY(scale); tv->setScaleY(scale);
}); });
ValueAnimator*anim=ValueAnimator::ofPropertyValuesHolder({&fprop});
anim->setDuration(5000); anim->setDuration(5000);
anim->start(); anim->start();
app.exec(); app.exec();
} }