override TextView::verifyDrawable/invalidateDrawable...

This commit is contained in:
侯歌 2022-03-30 17:54:58 +08:00
parent a9f5f18b90
commit 6d0f6c0b59
5 changed files with 105 additions and 33 deletions

View File

@ -395,7 +395,7 @@ void View::initView(){
}
View::~View(){
if(mParent)onDetachedFromWindow();
if(isAttachedToWindow())onDetachedFromWindow();
if(mBackground)mBackground->setCallback(nullptr);
delete mForegroundInfo;
delete mPerformClick;

View File

@ -146,7 +146,7 @@ void ViewGroup::initFromAttributes(Context*ctx,const AttributeSet&atts){
setMotionEventSplittingEnabled(atts.getBoolean("splitMotionEvents"));
if(atts.getBoolean("animateLayoutChanges",false))
setLayoutTransition(new LayoutTransition());
setLayoutTransition(new LayoutTransition());
setLayoutMode(0);
setTouchscreenBlocksFocus(atts.getBoolean("touchscreenBlocksFocus",false));
}

View File

@ -4,7 +4,7 @@
#include <widget/textview.h>
#include <widget/imageview.h>
#include <widget/viewpager.h>
#include <animation/animator.h>
namespace cdroid{
class TabLayout:public HorizontalScrollView{

View File

@ -34,20 +34,24 @@ bool TextView::Drawables::hasMetadata()const{
TextView::Drawables::Drawables(Context*ctx){
mIsRtlCompatibilityMode= false;
mHasTint = mHasTintMode= mOverride =false;
mTintList=nullptr;
mShowing[0]=mShowing[1]=mShowing[2]=mShowing[3]=nullptr;
mDrawableStart=mDrawableEnd=mDrawableError=mDrawableTemp=nullptr;
mDrawableLeftInitial=mDrawableRightInitial=nullptr;
mDrawableSizeTop=mDrawableSizeBottom=mDrawableSizeLeft=0;
mDrawableSizeRight=mDrawableSizeStart=mDrawableSizeEnd=0;
mDrawableSizeError=mDrawableSizeTemp=0;
mDrawableWidthTop=mDrawableWidthBottom=mDrawableHeightLeft=0;
mDrawableHeightRight=mDrawableHeightStart=mDrawableHeightEnd=0;
mDrawableHeightError=mDrawableHeightTemp=mDrawablePadding=0;
mTintList= nullptr;
mShowing[0] = mShowing[1] = nullptr;
mShowing[2] = mShowing[3] = nullptr;
mDrawableStart = mDrawableEnd = nullptr;
mDrawableError = mDrawableTemp = nullptr;
mDrawableLeftInitial= mDrawableRightInitial=nullptr;
mDrawableSizeTop = mDrawableSizeBottom = mDrawableSizeLeft=0;
mDrawableSizeRight = mDrawableSizeStart = mDrawableSizeEnd=0;
mDrawableSizeError = mDrawableSizeTemp = 0;
mDrawableWidthTop = mDrawableWidthBottom = mDrawableHeightLeft =0;
mDrawableHeightRight= mDrawableHeightStart = mDrawableHeightEnd =0;
mDrawableHeightError= mDrawableHeightTemp = mDrawablePadding =0;
mCompoundRect.set(0,0,0,0);
}
TextView::Drawables::~Drawables(){
for(int i=0;i<3;i++)delete mShowing[i];
for(int i=0;i<4;i++){
delete mShowing[i];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -817,9 +821,8 @@ void TextView::onMeasure(int widthMeasureSpec, int heightMeasureSpec){
std::vector<Drawable*>TextView::getCompoundDrawables(){
std::vector<Drawable*>ret;
for(int i=0;i<4;i++){
ret.push_back(mDrawables->mShowing[i]);
}
if(mDrawables)
ret.assign(mDrawables->mShowing,mDrawables->mShowing+4);
return ret;
}
@ -1013,6 +1016,88 @@ void TextView::drawableHotspotChanged(float x,float y){
}
}
bool TextView::verifyDrawable(Drawable* who)const {
bool verified = View::verifyDrawable(who);
if (!verified && mDrawables) {
for (int i=0;i<4;i++){
if (who == mDrawables->mShowing[i]) {
return true;
}
}
}
return verified;
}
void TextView::jumpDrawablesToCurrentState(){
View::jumpDrawablesToCurrentState();
if (mDrawables != nullptr) {
for (int i=0;i<4;i++){
Drawable* dr = mDrawables->mShowing[i];
if (dr != nullptr) {
dr->jumpToCurrentState();
}
}
}
}
void TextView::invalidateDrawable(Drawable& drawable){
bool handled = false;
if (verifyDrawable(&drawable)) {
Rect dirty = drawable.getBounds();
int scrollX = mScrollX;
int scrollY = mScrollY;
// IMPORTANT: The coordinates below are based on the coordinates computed
// for each compound drawable in onDraw(). Make sure to update each section
// accordingly.
if (mDrawables != nullptr) {
if (&drawable == mDrawables->mShowing[Drawables::LEFT]) {
int compoundPaddingTop = getCompoundPaddingTop();
int compoundPaddingBottom = getCompoundPaddingBottom();
int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop;
scrollX += mPaddingLeft;
scrollY += compoundPaddingTop + (vspace - mDrawables->mDrawableHeightLeft) / 2;
handled = true;
} else if (&drawable == mDrawables->mShowing[Drawables::RIGHT]) {
int compoundPaddingTop = getCompoundPaddingTop();
int compoundPaddingBottom = getCompoundPaddingBottom();
int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop;
scrollX += (mRight - mLeft - mPaddingRight - mDrawables->mDrawableSizeRight);
scrollY += compoundPaddingTop + (vspace - mDrawables->mDrawableHeightRight) / 2;
handled = true;
} else if (&drawable == mDrawables->mShowing[Drawables::TOP]) {
int compoundPaddingLeft = getCompoundPaddingLeft();
int compoundPaddingRight = getCompoundPaddingRight();
int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft;
scrollX += compoundPaddingLeft + (hspace - mDrawables->mDrawableWidthTop) / 2;
scrollY += mPaddingTop;
handled = true;
} else if (&drawable == mDrawables->mShowing[Drawables::BOTTOM]) {
int compoundPaddingLeft = getCompoundPaddingLeft();
int compoundPaddingRight = getCompoundPaddingRight();
int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft;
scrollX += compoundPaddingLeft + (hspace - mDrawables->mDrawableWidthBottom) / 2;
scrollY += (mBottom - mTop - mPaddingBottom - mDrawables->mDrawableSizeBottom);
handled = true;
}
}
if (handled) {
dirty.offset(scrollX,scrollY);
invalidate(dirty);
}
}
if (!handled) {
View::invalidateDrawable(drawable);
}
}
void TextView::updateTextColors(){
bool inval = false;
const std::vector<int>&drawableState = getDrawableState();
@ -1525,21 +1610,6 @@ void TextView::onDraw(Canvas& canvas) {
dr->mShowing[Drawables::BOTTOM]->draw(canvas);
canvas.restore();
}
int animateCount=0;
for(int i=0;i<4;i++){
Drawable*d=dr->mShowing[i];
if(dynamic_cast<AnimatedRotateDrawable*>(d)){
AnimatedRotateDrawable*ad=(AnimatedRotateDrawable*)d;
if(ad->isRunning())animateCount++;
}
if(dynamic_cast<AnimationDrawable*>(d)){
AnimationDrawable*ad=(AnimationDrawable*)d;
if(ad->isRunning())animateCount++;
break;
}
}
if(animateCount)
postInvalidateOnAnimation();
}
// Text
int extendedPaddingTop = getExtendedPaddingTop();

View File

@ -142,6 +142,7 @@ protected:
void setEditable(bool b);
int getFontSize()const;
void drawableStateChanged()override;
bool verifyDrawable(Drawable* who)const override;
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)override;
virtual void onDraw(Canvas& canvas) override;
virtual int getHorizontalOffsetForDrawables()const;
@ -238,7 +239,8 @@ public:
void drawableHotspotChanged(float x,float y)override;
void setCompoundDrawableTintMode(int tintMode);
int getCompoundDrawableTintMode();
void jumpDrawablesToCurrentState()override;
void invalidateDrawable(Drawable& drawable)override;
void setCompoundDrawables(Drawable* left,Drawable* top,Drawable* right,Drawable*bottom);
void setCompoundDrawablesWithIntrinsicBounds(Drawable* left,Drawable* top,Drawable* right,Drawable*bottom);
void setCompoundDrawablesWithIntrinsicBounds(const std::string& left, const std::string& top,