modify view and tablayout,add some properties support

This commit is contained in:
侯歌 2022-01-05 16:07:13 +08:00
parent ac24024c4b
commit e78527897f
4 changed files with 124 additions and 13 deletions

View File

@ -19,11 +19,27 @@ TabLayout::TabLayout(int w,int h):HorizontalScrollView(w,h){
TabLayout::TabLayout(Context*context,const AttributeSet&atts)
:HorizontalScrollView(context,atts){
initTabLayout();
mTabStrip->setSelectedIndicatorHeight(atts.getDimensionPixelSize("tabIndicatorHeight",-1));
mTabStrip->setSelectedIndicatorColor(atts.getColor("tabIndicatorColor",0));
setTabIndicatorGravity(atts.getGravity("tabIndicatorGravity",0));
mTabPaddingStart = mTabPaddingTop=mTabPaddingEnd=mPaddingBottom=atts.getDimensionPixelSize("tabPadding",0);
mTabPaddingStart = atts.getDimensionPixelSize("tabPaddingStart",mTabPaddingStart);
mTabPaddingEnd = atts.getDimensionPixelSize("tabPaddingEnd",mTabPaddingEnd);
mTabPaddingTop = atts.getDimensionPixelSize("tabPaddingTop",mTabPaddingTop);
mTabPaddingBottom= atts.getDimensionPixelSize("tabPaddingBottom",mTabPaddingBottom);
mTabTextSize =atts.getDimensionPixelSize("tabTextSize",mTabTextSize);
mTabTextColors=context->getColorStateList(atts.getString("tabTextColor"));
mTabBackgroundResId=atts.getString("tabBackground");
mMode = atts.getInt("tabMode",std::map<const std::string,int>{{"scrollable",0},{"fixed",1}},1);
mTabGravity =atts.getGravity("tabGravity",0);
mInlineLabel=atts.getBoolean("tabInlineLabel",false);
}
void TabLayout::initTabLayout(){
AttributeSet atts;
mMode = MODE_SCROLLABLE;
mInlineLabel =false;
mTabPaddingStart= mTabPaddingTop = 0;
mTabPaddingEnd = mTabPaddingBottom=0;
mTabTextSize = 20;
@ -32,7 +48,8 @@ void TabLayout::initTabLayout(){
mScrollAnimator = nullptr;
mViewPager = nullptr;
mPagerAdapter =nullptr;
mAdapterChangeListener = nullptr;
mAdapterChangeListener= nullptr;
mTabSelectedIndicator = nullptr;
mRequestedTabMinWidth = INVALID_WIDTH;
mRequestedTabMaxWidth = INVALID_WIDTH;
mTabTextMultiLineSize =2;
@ -211,6 +228,48 @@ int TabLayout::getTabGravity()const{
return mTabGravity;
}
Drawable*TabLayout::getSelectedTabIndicator()const{
return mTabSelectedIndicator;
}
void TabLayout::setSelectedTabIndicator(Drawable*d){
if(mTabSelectedIndicator!=d){
delete mTabSelectedIndicator;
mTabSelectedIndicator=d;
mTabStrip->postInvalidateOnAnimation();
}
}
void TabLayout::setSelectedTabIndicator(const std::string&res){
setSelectedTabIndicator(getContext()->getDrawable(res));
}
int TabLayout::getTabIndicatorGravity()const{
return mTabIndicatorGravity;
}
void TabLayout::setTabIndicatorGravity(int gravity){
if(mTabIndicatorGravity!=gravity){
mTabIndicatorGravity =gravity;
mTabStrip->postInvalidateOnAnimation();
}
}
bool TabLayout::isInlineLabel()const{
return mInlineLabel;
}
void TabLayout::setInlineLabel(bool v){
if(mInlineLabel==v)return;
mInlineLabel=v;
for(int i=0;i<mTabStrip->getChildCount();i++){
View*child=mTabStrip->getChildAt(i);
if(dynamic_cast<TabLayout::TabView*>(child)){
TabLayout::TabView*tv=(TabLayout::TabView*)child;
//tv->updateBackgroundDrawable(getContext());
}
}
}
void TabLayout::setTabTextColors(ColorStateList* textColor) {
if (mTabTextColors != textColor) {
@ -416,7 +475,7 @@ void TabLayout::updateTabViewLayoutParams(LinearLayout::LayoutParams* lp){
}
int dpToPx(int dps) {
return dps;//Math.round(getResources().getDisplayMetrics().density * dps);
return dps;//Math.round(getDisplayMetrics().density * dps);
}
void TabLayout::onMeasure(int widthMeasureSpec, int heightMeasureSpec){
@ -657,7 +716,7 @@ int TabLayout::getDefaultHeight() {
break;
}
}
return hasIconAndText ? DEFAULT_HEIGHT_WITH_TEXT_ICON : DEFAULT_HEIGHT;
return hasIconAndText&&!isInlineLabel() ? DEFAULT_HEIGHT_WITH_TEXT_ICON : DEFAULT_HEIGHT;
}
int TabLayout::getTabMinWidth() {
@ -793,9 +852,10 @@ TabLayout::TabView::TabView(Context* context,const AttributeSet&atts,TabLayout*p
/*if (mTabBackgroundResId != 0) {
//ViewCompat.setBackground(this, AppCompatResources.getDrawable(context, mTabBackgroundResId));
}*/
//ViewCompat.setPaddingRelative(this, mTabPaddingStart, mTabPaddingTop, mTabPaddingEnd, mTabPaddingBottom);
setBackgroundResource(parent->mTabBackgroundResId);
setPaddingRelative(parent->mTabPaddingStart, parent->mTabPaddingTop, parent->mTabPaddingEnd, parent->mTabPaddingBottom);
setGravity(Gravity::CENTER);
setOrientation(VERTICAL);
setOrientation(parent->mInlineLabel?HORIZONTAL:VERTICAL);
setClickable(true);
//ViewCompat.setPointerIcon(this,PointerIconCompat.getSystemIcon(getContext(), PointerIconCompat.TYPE_HAND));
}
@ -1260,10 +1320,30 @@ void TabLayout::SlidingTabStrip::animateIndicatorToPosition(int position, int du
void TabLayout::SlidingTabStrip::draw(Canvas& canvas) {
LinearLayout::draw(canvas);
// Thick colored underline below the current selection
int indicatorHeight=mSelectedIndicatorHeight;
int indicatorTop =0;
int indicatorBottom=0;
switch(mParent->getTabIndicatorGravity()&Gravity::VERTICAL_GRAVITY_MASK){
case Gravity::NO_GRAVITY:
indicatorTop =getHeight()-indicatorHeight;
indicatorBottom=getHeight();
break;
case Gravity::CENTER_VERTICAL:
indicatorTop =(getHeight()-indicatorHeight)/2;
indicatorBottom =(getHeight()+indicatorHeight)/2;
break;
case Gravity::TOP:
indicatorTop =0;
indicatorBottom =indicatorHeight;
break;
case Gravity::FILL_VERTICAL:
indicatorTop =0;
indicatorBottom=getHeight();
break;
}
if (mIndicatorLeft >= 0 && mIndicatorRight > mIndicatorLeft) {
canvas.set_color(mSelectedIndicatorColor);
canvas.rectangle(mIndicatorLeft, getHeight() - mSelectedIndicatorHeight,
mIndicatorRight-mIndicatorLeft, mSelectedIndicatorHeight);
canvas.rectangle(mIndicatorLeft,indicatorTop, mIndicatorRight-mIndicatorLeft,indicatorBottom-indicatorTop);
canvas.fill();
}
}

View File

@ -54,7 +54,8 @@ public:
private:
static constexpr int CUSTOM_ID_TEXT =0;
static constexpr int CUSTOM_ID_ICON =1;
TabLayout*mParent;
friend class TabLayout;
TabLayout *mParent;
Tab* mTab;
TextView* mTextView;
ImageView* mIconView;
@ -196,13 +197,14 @@ protected:
Drawable* mTabSelectedIndicator;
float mTabTextSize;
float mTabTextMultiLineSize;
int mTabBackgroundResId;
std::string mTabBackgroundResId;
int mTabMaxWidth;
int mTabGravity;
int mTabIndicatorAnimationDuration;
int mTabIndicatorGravity;
int mMode;
bool mInlineLabel;
std::vector<OnTabSelectedListener> mSelectedListeners;
OnTabSelectedListener mCurrentVpSelectedListener;
bool inlineLabel;
@ -252,6 +254,13 @@ public:
int getTabMode()const;
void setTabGravity(int gravity);
int getTabGravity()const;
int getTabIndicatorGravity()const;
void setTabIndicatorGravity(int);
Drawable* getSelectedTabIndicator()const;
void setSelectedTabIndicator(Drawable*d);
void setSelectedTabIndicator(const std::string&res);
bool isInlineLabel()const;
void setInlineLabel(bool);
void setTabTextColors(ColorStateList* textColor);
ColorStateList* getTabTextColors()const;
void setTabTextColors(int normalColor, int selectedColor);

View File

@ -603,6 +603,27 @@ void View::setPadding(int left, int top, int right, int bottom){
LOGV("%p padding=%d,%d-%d-%d",this,left,top,right,bottom);
}
void View::setPaddingRelative(int start,int top,int end,int bottom){
mPrivateFlags2 &= ~PFLAG2_PADDING_RESOLVED;//resetResolvedPaddingInternal();
mUserPaddingStart = start;
mUserPaddingEnd =end;
mLeftPaddingDefined =true;
mRightPaddingDefined =true;
switch(getLayoutDirection()){
case LAYOUT_DIRECTION_RTL:
mUserPaddingLeftInitial =end;
mUserPaddingRightInitial =start;
internalSetPadding(end,top,start,bottom);
break;
case LAYOUT_DIRECTION_LTR:
default:
mUserPaddingLeftInitial =start;
mUserPaddingRightInitial=end;
internalSetPadding(start,top,end,bottom);
break;
}
}
void View::recomputePadding() {
internalSetPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
}
@ -2188,7 +2209,6 @@ void View::draw(Canvas&canvas){
drawDefaultFocusHighlight(canvas);
if (debugDraw()) debugDrawFocus(canvas);
// we're done...
return;
}
@ -2198,7 +2218,8 @@ void View::draw(Canvas&canvas){
* (this is an uncommon case where speed matters less,
* this is why we repeat some of the tests that have been
* done above)
*//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool drawTop = false;
bool drawBottom = false;
@ -2549,8 +2570,8 @@ bool View::draw(Canvas&canvas,ViewGroup*parent,long drawingTime){
canvas.set_source(cache,0,0);
canvas.paint_with_alpha(alpha);
}
while(restoreTo>0) {
canvas.restore();restoreTo--;//ToCount(restoreTo);
while(restoreTo-->0) {
canvas.restore();//ToCount(restoreTo);
}
if (a != nullptr && !more) {

View File

@ -565,6 +565,7 @@ public:
Insets getOpticalInsets();
void setOpticalInsets(const Insets& insets);
void setPadding(int left, int top, int right, int bottom);
void setPaddingRelative(int start,int top,int end,int bottom);
bool isPaddingResolved()const;
virtual void resolvePadding();
virtual void onRtlPropertiesChanged(int layoutDirection);