问题
升级28后,FloatingActionButton显示异常,图片非常小
原因
SDK28,初始化逻辑:
public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.shadowPadding = new Rect();
this.touchArea = new Rect();
TypedArray a = ThemeEnforcement.obtainStyledAttributes(context, attrs, styleable.FloatingActionButton, defStyleAttr, style.Widget_Design_FloatingActionButton, new int[0]);
this.backgroundTint = MaterialResources.getColorStateList(context, a, styleable.FloatingActionButton_backgroundTint);
this.backgroundTintMode = ViewUtils.parseTintMode(a.getInt(styleable.FloatingActionButton_backgroundTintMode, -1), (Mode)null);
this.rippleColor = MaterialResources.getColorStateList(context, a, styleable.FloatingActionButton_rippleColor);
this.size = a.getInt(styleable.FloatingActionButton_fabSize, -1);
this.customSize = a.getDimensionPixelSize(styleable.FloatingActionButton_fabCustomSize, 0);
this.borderWidth = a.getDimensionPixelSize(styleable.FloatingActionButton_borderWidth, 0);
float elevation = a.getDimension(styleable.FloatingActionButton_elevation, 0.0F);
float hoveredFocusedTranslationZ = a.getDimension(styleable.FloatingActionButton_hoveredFocusedTranslationZ, 0.0F);
float pressedTranslationZ = a.getDimension(styleable.FloatingActionButton_pressedTranslationZ, 0.0F);
this.compatPadding = a.getBoolean(styleable.FloatingActionButton_useCompatPadding, false);
this.maxImageSize = a.getDimensionPixelSize(styleable.FloatingActionButton_maxImageSize, 0);
MotionSpec showMotionSpec = MotionSpec.createFromAttribute(context, a, styleable.FloatingActionButton_showMotionSpec);
MotionSpec hideMotionSpec = MotionSpec.createFromAttribute(context, a, styleable.FloatingActionButton_hideMotionSpec);
a.recycle();
this.imageHelper = new AppCompatImageHelper(this);
this.imageHelper.loadFromAttributes(attrs, defStyleAttr);
this.expandableWidgetHelper = new ExpandableWidgetHelper(this);
this.getImpl().setBackgroundDrawable(this.backgroundTint, this.backgroundTintMode, this.rippleColor, this.borderWidth);
this.getImpl().setElevation(elevation);
this.getImpl().setHoveredFocusedTranslationZ(hoveredFocusedTranslationZ);
this.getImpl().setPressedTranslationZ(pressedTranslationZ);
this.getImpl().setMaxImageSize(this.maxImageSize);
this.getImpl().setShowMotionSpec(showMotionSpec);
this.getImpl().setHideMotionSpec(hideMotionSpec);
this.setScaleType(ScaleType.MATRIX);
}
SDK26,初始化逻辑:
public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
ThemeUtils.checkAppCompatTheme(context);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.FloatingActionButton, defStyleAttr,
R.style.Widget_Design_FloatingActionButton);
mBackgroundTint = a.getColorStateList(R.styleable.FloatingActionButton_backgroundTint);
mBackgroundTintMode = ViewUtils.parseTintMode(a.getInt(
R.styleable.FloatingActionButton_backgroundTintMode, -1), null);
mRippleColor = a.getColor(R.styleable.FloatingActionButton_rippleColor, 0);
mSize = a.getInt(R.styleable.FloatingActionButton_fabSize, SIZE_AUTO);
mBorderWidth = a.getDimensionPixelSize(R.styleable.FloatingActionButton_borderWidth, 0);
final float elevation = a.getDimension(R.styleable.FloatingActionButton_elevation, 0f);
final float pressedTranslationZ = a.getDimension(
R.styleable.FloatingActionButton_pressedTranslationZ, 0f);
mCompatPadding = a.getBoolean(R.styleable.FloatingActionButton_useCompatPadding, false);
a.recycle();
mImageHelper = new AppCompatImageHelper(this);
mImageHelper.loadFromAttributes(attrs, defStyleAttr);
mMaxImageSize = (int) getResources().getDimension(R.dimen.design_fab_image_size);
getImpl().setBackgroundDrawable(mBackgroundTint, mBackgroundTintMode,
mRippleColor, mBorderWidth);
getImpl().setElevation(elevation);
getImpl().setPressedTranslationZ(pressedTranslationZ);
}
对比一下,就找到了出问题的地方,如下:
this.setScaleType(ScaleType.MATRIX);
解决
xml中设置scaleType已经失效,只能在代码中重置了。
原:
android:scaleType="center"
修改为:
fab.setScaleType(ImageView.ScaleType.CENTER);