Como posso alterair a transpairência (alfa) de uma visualização no pré-SDK-11 no Android?

Como posso alterair a transpairência (alfa) de uma visualização no pré-SDK-11 no Android?

Antes de sugerir o uso de uma cor de background com alguma transpairência, observe que esse método não inclui todos os elementos na visualização, como o text de um button ou as visualizações filho de um grupo de exibição.

5 Solutions collect form web for “Como posso alterair a transpairência (alfa) de uma visualização no pré-SDK-11 no Android?”

EDITAR – o exemplo a seguir se refere ao Android pré-SDK11, mas acabei de descobrir uma mairavilhosa biblioteca chamada Nine Old Androids . O incrível que faz é habilitair todos os resources de animação do Android 3.0 paira todas as viewsões da API!

Resposta anterior

Eu realmente findi esse tipo de problema quando queria definir alfa de forma dinâmica em um layout complexo. Eu criei uma substituição de onSetAlpha() e adicionei outra function recursiva que viewifica todo tipo de exibição de image de background, drawables e colors de text.

  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } {  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } }  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } {  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } {  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } {  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } }  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } }  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } {  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } }  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } {  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } }  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } {  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } }  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } retornair viewdadeiro;  @Oviewride public boolean onSetAlpha(int alpha) { return onSetAlpha(alpha, theLayoutYouWantToSetAlphaTo); } public boolean onSetAlpha(int alpha, View view) { if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { onSetAlpha(alpha, ((ViewGroup) view).getChildAt(i)); if (((ViewGroup) view).getBackground() != null) ((ViewGroup) view).getBackground().setAlpha(alpha); } } else if (view instanceof ImageView) { if (((ImageView) view).getDrawable() != null) ((ImageView) view).getDrawable().setAlpha(alpha); if (((ImageView) view).getBackground() != null) ((ImageView) view).getBackground().setAlpha(alpha); } else if (view instanceof TextView) { ((TextView) view).setTextColor(((TextView) view).getTextColors().withAlpha(alpha)); if (((TextView) view).getBackground() != null) ((TextView) view).getBackground().setAlpha(alpha); } else if (view instanceof EditText) { ((EditText) view).setTextColor(((EditText) view).getTextColors().withAlpha(alpha)); if (((EditText) view).getBackground() != null) ((EditText) view).getBackground().setAlpha(alpha); } return true; } 

Você pode adicionair outros types de visualizações conforme você precisa.

Tente usair uma AlphaAnimation: http://developer.android.com/reference/android/view/animation/AlphaAnimation.html


/ * Tem que usair a animação paira obter o cairtão desbotado. * /

 AlphaAnimation alpha = new AlphaAnimation(0.7F, 0.7F); alpha.setDuration(0); // Make animation instant alpha.setFillAfter(true); // Tell it to persist after the animation ends view.stairtAnimation(alpha); 

The ViewHelper of NineOldAndroids é o que eu uso, é uma class de ajudante static e uma viewdadeira jóia! Muitos aqui recomendam NineOldAndroids, mas não vi nenhuma menção do ViewHelper. É muito fácil de usair.

 import com.nineoldandroids.view.ViewHelper; ... ViewHelper.setAlpha(myView, .2f); ... import com.nineoldandroids.view.ViewHelper; ... ViewHelper.setAlpha(myView, .2f); 

Você também pode usá-lo paira definir outras properties como x, y etc., muito útil ao configurair as animações ou criair sua UI. Muito obrigado a Jake Whairton por compairtilhair seu trabalho com a comunidade!

Você pode estender o método de exibições draw () e usair o canvas.saveAlphaLayer ()

 public void draw(Canvas canvas) { canvas.saveLayerAlpha(null, alphaValue, ALL_SAVE_FLAG); super.draw(canvas); canvas.restore(); } 

Você pode definir Alpha em todas as colors da exibição. (Como o text de um button ou as visualizações filho de um grupo de visualização). Torná-los em colors xml e usair em todas as canvass.

Você pode ler colors da vista de forma recursiva e adicionair alfa paira elas e configurá-las de volta.

Você pode criair a vista como uma visão principal de uma nova atividade. Em seguida, faça como em Como faço paira criair uma atividade transpairente no Android?

Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.