1

a newbie question.

I'm trying to change an alpha value of a bitmap item inside a layer-list.

my drawable xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap android:src="@drawable/img_btn" android:gravity="center"/>
    </item>
    <item>
        <bitmap android:src="@drawable/image_frame" android:gravity="center"/>
    </item>
</layer-list>

my MainActivity is as follows:

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainApp.show(this);
        addListeners();
        changeAlpha(128);
    }

    private void changeAlpha(int opc){
        ImageView img = (ImageView) findViewById(R.id.img_A);
        LayerDrawable layer = (LayerDrawable) img.getDrawable();
        BitmapDrawable bg = (BitmapDrawable) layer.getDrawable(0);
        bg.setAlpha(opc);
    }

    private void addListeners() {
        ImageView img = (ImageView) findViewById(R.id.img_A);

        View.OnClickListener imageAClickListener = new View.OnClickListener() {
            public void onClick(View view) {
                changeAlpha(255);
            }
        };

        img.setOnClickListener(imageAClickListener);
    }

    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(Definitions.CONFIRM_EXIT)
                .setCancelable(false)
                .setPositiveButton(Definitions.YES, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        MainActivity.this.finish();
                    }
                })
                .setNegativeButton(Definitions.NO, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

the first call to changeAlpha (from onCreate) works just fine, but when I call the method from the click event it does nothing (without failing)

What am I missing?

Parth Doshi
  • 4,200
  • 15
  • 79
  • 129
Shlomi Schwartz
  • 8,693
  • 29
  • 109
  • 186

2 Answers2

2

in some case,you should callimg.setImageDrawable(layer),it will cause repain.

Dartan Li
  • 217
  • 2
  • 5
2

try adding

img.invalidate();

to last line of your changeAlpha() method.

Shaunak
  • 17,377
  • 5
  • 53
  • 84
  • cool! I hope you understand what the problem was. A repaint was needed. When you set it in onCreate, the image is automatically rendered first time. But later when you modify it you have to have it repainted. Classic java has a nice methode called repaint(). Android takes a more passive approach i guess :P :P :P – Shaunak Dec 25 '11 at 12:25
  • Thanks again for elaborating, I kind'a guessed it was the issue. – Shlomi Schwartz Dec 25 '11 at 15:59
  • I would appreciate if you could look at http://stackoverflow.com/q/8630365/1115237 – Shlomi Schwartz Dec 28 '11 at 12:57