33import android .content .res .Resources ;
44import android .graphics .Bitmap ;
55import android .graphics .Canvas ;
6+ import android .graphics .ColorFilter ;
67import android .graphics .ColorMatrix ;
78import android .graphics .ColorMatrixColorFilter ;
9+ import android .graphics .Paint ;
810import android .graphics .drawable .BitmapDrawable ;
911import android .graphics .drawable .ColorDrawable ;
1012import android .graphics .drawable .Drawable ;
@@ -198,7 +200,7 @@ private static Drawable createStyleDrawable(Drawable src, int style, float value
198200 }
199201
200202 Drawable disable = src .getConstantState ().newDrawable ().mutate ();
201- disable = createAlphaDrawable (pressed , 0.5f );
203+ disable = createAlphaDrawable (disable , 0.5f );
202204
203205 StateListDrawable drawable = new StateListDrawable ();
204206 drawable .addState (new int []{android .R .attr .state_pressed }, pressed );
@@ -208,27 +210,20 @@ private static Drawable createStyleDrawable(Drawable src, int style, float value
208210 }
209211
210212 private static Drawable createAlphaDrawable (Drawable drawable , float alpha ) {
211- if (Build .VERSION .SDK_INT <= Build .VERSION_CODES .KITKAT
212- && !(drawable instanceof ColorDrawable )) {
213- Bitmap bitmap = Bitmap .createBitmap (drawable .getIntrinsicWidth (), drawable .getIntrinsicHeight (), Bitmap .Config .ARGB_8888 );
214- Canvas myCanvas = new Canvas (bitmap );
215- drawable .setAlpha ((int ) (alpha * 255 ));
216- drawable .setBounds (0 , 0 , drawable .getIntrinsicWidth (), drawable .getIntrinsicHeight ());
217- drawable .draw (myCanvas );
218- return new BitmapDrawable (Resources .getSystem (), bitmap );
213+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .LOLLIPOP ) {
214+ DrawableWrapperBefore21 drawableWrapper = new DrawableWrapperBefore21 (drawable );
215+ drawableWrapper .setAlphaFix ((int ) (alpha * 255 ));
216+ return drawableWrapper ;
219217 }
220218 drawable .setAlpha ((int ) (alpha * 255 ));
221219 return drawable ;
222220 }
223221
224222 private static Drawable createDarkDrawable (Drawable drawable , float alpha ) {
225- if (Build .VERSION .SDK_INT <= Build .VERSION_CODES .KITKAT && !(drawable instanceof ColorDrawable )) {
226- Bitmap bitmap = Bitmap .createBitmap (drawable .getIntrinsicWidth (), drawable .getIntrinsicHeight (), Bitmap .Config .ARGB_8888 );
227- Canvas myCanvas = new Canvas (bitmap );
228- drawable .setColorFilter (getDarkColorFilter (alpha ));
229- drawable .setBounds (0 , 0 , drawable .getIntrinsicWidth (), drawable .getIntrinsicHeight ());
230- drawable .draw (myCanvas );
231- return new BitmapDrawable (Resources .getSystem (), bitmap );
223+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .LOLLIPOP ) {
224+ DrawableWrapperBefore21 drawableWrapper = new DrawableWrapperBefore21 (drawable );
225+ drawableWrapper .setColorFilterFix (getDarkColorFilter (alpha ));
226+ return drawableWrapper ;
232227 }
233228 drawable .setColorFilter (getDarkColorFilter (alpha ));
234229 return drawable ;
@@ -509,4 +504,60 @@ private static class LazyHolder {
509504 private static final OnUtilsTouchListener INSTANCE = new OnUtilsTouchListener ();
510505 }
511506 }
507+
508+ static class DrawableWrapperBefore21 extends ShadowUtils .DrawableWrapper {
509+
510+ private BitmapDrawable mBitmapDrawable = null ;
511+
512+ //低版本ColorDrawable.setColorFilter无效,这里直接用画笔画上
513+ private Paint mColorPaint = null ;
514+
515+ public DrawableWrapperBefore21 (Drawable drawable ) {
516+ super (drawable );
517+ if (drawable instanceof ColorDrawable ) {
518+ mColorPaint = new Paint (Paint .ANTI_ALIAS_FLAG | Paint .DITHER_FLAG );
519+ mColorPaint .setColor (((ColorDrawable ) drawable ).getColor ());
520+ }
521+ }
522+
523+ @ Override
524+ public void setColorFilter (ColorFilter cf ) {
525+ //低版本StateListDrawable.selectDrawable会重置ColorFilter
526+ }
527+
528+ public void setColorFilterFix (ColorFilter cf ) {
529+ super .setColorFilter (cf );
530+ if (mColorPaint != null ) {
531+ mColorPaint .setColorFilter (cf );
532+ }
533+ }
534+
535+ @ Override
536+ public void setAlpha (int alpha ) {
537+ //低版本StateListDrawable.selectDrawable会重置Alpha
538+ }
539+
540+ public void setAlphaFix (int alpha ) {
541+ super .setAlpha (alpha );
542+ if (mColorPaint != null ) {
543+ mColorPaint .setColor (((ColorDrawable ) getWrappedDrawable ()).getColor ());
544+ }
545+ }
546+
547+ @ Override
548+ public void draw (Canvas canvas ) {
549+ if (mBitmapDrawable == null ) {
550+ Bitmap bitmap = Bitmap .createBitmap (getBounds ().width (), getBounds ().height (), Bitmap .Config .ARGB_8888 );
551+ Canvas myCanvas = new Canvas (bitmap );
552+ if (mColorPaint != null ) {
553+ myCanvas .drawRect (getBounds (), mColorPaint );
554+ } else {
555+ super .draw (myCanvas );
556+ }
557+ mBitmapDrawable = new BitmapDrawable (Resources .getSystem (), bitmap );
558+ mBitmapDrawable .setBounds (getBounds ());
559+ }
560+ mBitmapDrawable .draw (canvas );
561+ }
562+ }
512563}
0 commit comments