Home » Android » android – Why does setImageBitmap has no effect on custom ImageButton?

android – Why does setImageBitmap has no effect on custom ImageButton?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I found this link that states how I could make the glow effect on my ImageButton. So whenever the user press the ImageButton the onTouchEvent() function is called and there I call the setImageBitmap() function. The problem is this function (setImageBitmap()) seems to have no effect at all.

What works:

1) So far I extended the ImageButton as below

// Pen.java
public class Pen extends ImageButton {
    public Pen(Context context, AttributeSet attrs) {
        super(context, attrs);  
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        System.out.println("Now I am setting  Pen");
        return true;
    }


}

Then inside Activity I create Pen (custom ImageButton) instance, define setGlow() and call setImageBitmap() from instance of Pen.

//MainActivity.java

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Pen myImgbutton; 
        myImgbutton = (Pen) findViewById(R.id.pen);

        myImgbutton.setImageBitmap(setGlow(R.drawable.pen));

    }
}

public Bitmap setGlow(int resourceId) {
      ......
}

What doesn’t work:

1) All same as above ( extending a ImageButton in android ), but this time I am calling the
setImageBitmap function from inside onTouchEvent(). For this case I have define setGlow function in inside Pen.java

// Pen.java
public class Pen extends ImageButton {
    public Pen(Context context, AttributeSet attrs) {
        super(context, attrs);  
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        System.out.println("Now I am setting  Pen");
        // Here I say to apply the glow effect to the image
        setImageBitmap(setGlow(R.drawable.pen)); 
        return true;
    }

    public Bitmap setGlow(int resourceId) {
       ....
    }
}

Then in xml file I do :

  .....

   <com.example.testsetimagebmp.Pen
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pen"
        android:background="@drawable/pen" />

   ....

Finally in MainActivity

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    // ....
} 
How to&Answers:

I built the project as you post, but it works fine. It sets the Bitmap correctly. So maybe the getGlow() method doesn’t work well. Make some Logs as below

public Bitmap setGlow(int resourceId) {
    Bitmap bm = BitmapFactory.decodeResource(getResources(),resourceId);
    Log.e("tag", "Size: " + bm.getByteCount());
    return bm;
}

then check whether the return is right.

Answer:

I think this might be the classic out of memory issue. You can try to use the bitmap of the launcher icon if you want to check whether this is true:

public class Pen extends ImageButton {
    public Pen(Context context, AttributeSet attrs) {
        super(context, attrs);  
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        System.out.println("Now I am setting  Pen");
        // Here I say to apply the glow effect to the image
        setImageBitmap(setGlow(R.mipmap.ic_launcher)); 
        return true;
    }

    public Bitmap setGlow(int resourceId) {
       ....
    }
}

If this is the problem check this link.

Answer:

You can’t do that because you will out of memory if you put setImageBitmap(setGlow(R.drawable.pen)) in onTouchEvent.

onTouchEvent will be triggered serval times per second.And you will create serval bitmaps in memory per second.It will out of memory before you setImageBitmap successfully.