2016-08-26 5 views
-3

내 사용자 정의 모양의 Button에 ColorStateList을 구현했습니다. 물론 단추를 누르고 누를 때 단색을 변경합니다.API 21 아래에서 GradiantDrawable.setdColor()를 설정하는 방법은 무엇입니까?

GradiantDrawable의 색상을 API 21 이상으로 설정했기 때문에 API 21 이상에서는 제대로 작동하지만 API 21 이하에서는 제대로 작동하지 않습니다.

그래서 어떻게 21 미만의 API에 대해 동일한 효과를 보관할 수 있습니까?

이 내 방법은 현재의 모습입니다 : API 21 이하 전화기를 들어 ColorFilteronTouchListener 를 사용하여 해결

public void SelectorEffect(View v) { 

    ColorStateList colorStateList = null; 
    GradientDrawable gd = null; 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 

     if (v.getId() == R.id.login_btn) { 

      colorStateList = getPressedColorSelector(ContextCompat.getColor(this, R.color.arion_light), ContextCompat.getColor(this, R.color.arion_light2)); 
      gd = (GradientDrawable) getApplicationContext().getResources().getDrawable(R.drawable.arion_buttonshape_login); 

     } else if (v.getId() == R.id.createAccount_btn) { 
      colorStateList = getPressedColorSelector(ContextCompat.getColor(this, R.color.arion_blue), ContextCompat.getColor(this, R.color.arion_light2)); 
      gd = (GradientDrawable) getApplicationContext().getResources().getDrawable(R.drawable.arion_buttonshape_create); 

     } 


     gd.setColor(colorStateList); 


    } else { 

     //Code for phones below API 21: 

    } 

} 

답변

0

내가 내보기에 onTouchListener를 사용 ACTION_DOWN을 수신; 그때에 지정된 색으로 해당 뷰에 ColorFilter을 적용 PorterDuff.Mode.SRC_ATOP ACTION_UP 경우 : 난 그냥 null로 내 colorfilter를 설정하고 버튼을 다시

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 


     if (view.getId() == R.id.login_btn) { 

      colorStateList = getPressedColorSelector(); 
      gd = (GradientDrawable) super.getView().getBackground(); 
      gd.setColor(colorStateList); 

     } else if (view.getId() == R.id.createAccount_btn) { 

      colorStateList = getPressedColorSelector(); 
      gd = (GradientDrawable) super.getView().getBackground(); 
      gd.setColor(colorStateList); 


     } 


    } else { 

     /* 
     BELOW API 21 SOLUTION: 
     */ 

     button.getView().setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 

       if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 

        button.getView().getBackground().setColorFilter(selectedColor, PorterDuff.Mode.SRC_ATOP); 
        return true; 

       } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) { 


        button.getView().getBackground().setColorFilter(null); 
        return true; 
       } 


       return false; 
      } 
     }); 

    } 

} 


//The colorStateList only for API >=21 
private ColorStateList getPressedColorSelector() { 

    return new ColorStateList(
      new int[][] 
        { 
          new int[]{android.R.attr.state_pressed}, 
          new int[]{-android.R.attr.state_activated}, 
          new int[]{} 
        }, 
      new int[] 
        { 
          selectedColor, 
          defaultColor, 
          defaultColor, 
        } 
    ); 
} 
을 이전 색을 가져옵니다
관련 문제