2016-06-30 5 views
1

廃止予定のコードを使用しないように、下のクラスを書き直す方法を理解できない。ここに私の完全なクラスがあります。新しいメソッドに準拠するために書き直し、廃止予定のメソッドを取り除く

import android.content.Context; 
import android.graphics.Color; 
import android.graphics.ColorFilter; 
import android.graphics.LightingColorFilter; 
import android.graphics.drawable.Drawable; 
import android.graphics.drawable.LayerDrawable; 
import android.util.AttributeSet; 
import android.widget.Button; 

public class BgButtonStyle extends Button { 

    public BgButtonStyle(Context context) { 
     super(context); 
    } 

    public BgButtonStyle(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public BgButtonStyle(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public void setBackgroundDrawable(Drawable d) { 
     // Replace the original background drawable (e.g. image) with a LayerDrawable that 
     // contains the original drawable. 
     BgButtonStyleBackgroundDrawable layer = new BgButtonStyleBackgroundDrawable(d); 
     super.setBackgroundDrawable(layer); 
    } 

    /** 
    * The stateful LayerDrawable used by this button. 
    */ 
    protected class BgButtonStyleBackgroundDrawable extends LayerDrawable { 
     // The color filter to apply when the button is pressed 
     protected ColorFilter _pressedFilter = new LightingColorFilter(Color.LTGRAY, 1); 
     // Alpha value when the button is disabled 
     protected int _disabledAlpha = 100; 

     public BgButtonStyleBackgroundDrawable(Drawable d) { 
      super(new Drawable[]{d}); 
     } 

     @Override 
     protected boolean onStateChange(int[] states) { 
      boolean enabled = false; 
      boolean pressed = false; 

      for (int state : states) { 
       if (state == android.R.attr.state_enabled) 
        enabled = true; 
       else if (state == android.R.attr.state_pressed) 
        pressed = true; 
      } 

      mutate(); 
      if (enabled && pressed) { 
       setColorFilter(_pressedFilter); 
      } else if (!enabled) { 
       setColorFilter(null); 
       setAlpha(_disabledAlpha); 
      } else { 
       setColorFilter(null); 
      } 

      invalidateSelf(); 

      return super.onStateChange(states); 
     } 

     @Override 
     public boolean isStateful() { 
      return true; 
     } 
    } 
} 

は私がボタンにこのクラスを使用しているので、ボタンの背景色が、それはボタンが押されたときに、私はonPressed効果を確認することができますフィルタを適用するものに関係なく。私は非推奨setBackgroundDrawableからそれを更新するかどうかはわかりません、とLayerDrawable

答えて

0

ネヴァーマインド

、私は馬鹿です。私は単純にsetBackgroundDrawableメソッドをsetBackgroundに置き換えました。すべては現在期待通りに動作します。

+1

ソースコードには、新しいメソッドを指すコメントブロック(およびリンク)が表示されます(または、廃止されたメソッドは単に新しいメソッドを参照するだけです)。 –

+0

@MarkKeen - 絶対に正しい。明白なここを見下ろす、ありがとう! – Phil

関連する問題