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)
MATH(EXPR LEN0 "${LEN0}-${LEN1}")
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}")
install(FILES ${PROJECT_SOURCE_DIR}/${each_file} DESTINATION "${CMAKE_BINARY_DIR}/include/gui/${FILE_PATH}")
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
// whatever the current propertyName is
if (mProperty) {
//setValues({PropertyValuesHolder::ofInt(mProperty, values)});
setValues({PropertyValuesHolder::ofInt(mProperty, values)});
} else {
//setValues({PropertyValuesHolder::ofInt(mPropertyName, values)});
setValues({PropertyValuesHolder::ofInt(mPropertyName, values)});
}
} else {
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
// whatever the current propertyName is
if (mProperty) {
//setValues({PropertyValuesHolder::ofFloat(mProperty, values)});
setValues({PropertyValuesHolder::ofFloat(mProperty, values)});
} else {
//setValues({PropertyValuesHolder::ofFloat(mPropertyName, values)});
setValues({PropertyValuesHolder::ofFloat(mPropertyName, values)});
}
} else {
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;
Property*mProperty;
public:
PropertyValuesHolder(){
mProperty=nullptr;
}
void setPropertyName(const std::string& propertyName){
mPropertyName=propertyName;
}
const std::string getPropertyName()const{
return mPropertyName;
}
PropertyValuesHolder();
PropertyValuesHolder(Property*prop);
PropertyValuesHolder(const std::string&name);
void setPropertyName(const std::string& propertyName);
const std::string getPropertyName()const;
virtual void setFraction(void*target,float fraction)=0;
virtual ~PropertyValuesHolder(){
delete mProperty;
}
virtual ~PropertyValuesHolder();
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>
@ -76,20 +74,18 @@ public:
template<typename T>
class PropertyValuesHolderImpl:public PropertyValuesHolder{
public:
typedef std::function < void(void*target,float fraction,const T&value) > PropertySetter;
protected:
Evaluator<T>* mEvaluator;
std::vector<T>mDataSource;
T mStartValue;
T mEndValue;
T mAnimateValue;
PropertySetter mPropSetter;
public:
PropertyValuesHolderImpl(){
mEvaluator=nullptr;
mPropSetter=nullptr;
}
PropertyValuesHolderImpl(const std::string&name):PropertyValuesHolder(name){}
PropertyValuesHolderImpl(Property*prop):PropertyValuesHolder(prop){}
PropertyValuesHolderImpl(const T& startValue, const T& endValue)
: mStartValue(startValue), mEndValue(endValue) {}
void setValues(const T* dataSource, int length) {
@ -98,9 +94,6 @@ public:
void setValues(const std::vector<T>&values){
mDataSource=values;
}
void setPropertySetter(PropertySetter setter){
mPropSetter=setter;
}
void setFraction(void*target,float fraction)override{
if (mDataSource.size()==0) mEvaluator->evaluate(&mAnimateValue, mStartValue, mEndValue, fraction);
else if (fraction <= 0.0f) mAnimateValue=mDataSource.front();
@ -111,7 +104,6 @@ public:
fraction -= lowIndex;
mEvaluator->evaluate(&mAnimateValue, mDataSource[lowIndex], mDataSource[lowIndex + 1], fraction);
}
if(mPropSetter)mPropSetter(target,fraction,mAnimateValue);
}
T getAnimatedValue()const{return mAnimateValue;}
};
@ -119,16 +111,16 @@ public:
#if 1
class IntPropertyValuesHolder:public PropertyValuesHolderImpl<int>{
public:
IntPropertyValuesHolder():PropertyValuesHolderImpl<int>(){
mEvaluator=new IntEvaluator();
}
IntPropertyValuesHolder();
IntPropertyValuesHolder(const std::string&);
IntPropertyValuesHolder(Property*);
};
class FloatPropertyValuesHolder:public PropertyValuesHolderImpl<float>{
public:
FloatPropertyValuesHolder():PropertyValuesHolderImpl<float>(){
mEvaluator=new FloatEvaluator();
}
FloatPropertyValuesHolder();
FloatPropertyValuesHolder(const std::string&);
FloatPropertyValuesHolder(Property*);
};
class ColorPropertyValuesHolder:public PropertyValuesHolderImpl<uint32_t>{
public:

View File

@ -174,16 +174,16 @@ void ProgressBar::doRefreshProgress(int id, int progress, bool fromUser,bool cal
if (isPrimary && animate) {
FloatPropertyValuesHolder*prop=nullptr;
if(mAnimator==nullptr){
prop=new FloatPropertyValuesHolder();
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 = ObjectAnimator::ofFloat(this,"progress",{scale});
mAnimator->setAutoCancel(true);
mAnimator->setDuration(PROGRESS_ANIM_DURATION);
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});
mAnimator->start();
} else {

View File

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

View File

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