8

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);
    }
    // ....
} 
Community
  • 1
  • 1
pokche
  • 1,141
  • 12
  • 36

3 Answers3

5

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.

jobbert
  • 3,297
  • 27
  • 43
  • I am not sure if this is out of memory issue since catch I do do (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } – pokche May 04 '16 at 23:59
5

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.

tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
  • for this not to happen I tried creating a static boolean variable and initialized it to true and changed the variable to false after setImageBitmap() was called – pokche May 05 '16 at 00:03
3

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.

zz-m
  • 446
  • 5
  • 15
  • Yes It worked for me too .. automatically .. but the problem still persist with my original project ... the question was the dumb down version of my project though – pokche May 04 '16 at 23:58
  • In original project I have viewgroup that contains [ viewgroup1 and canvas ]. The viewgroup1 contains buttonImage (pen) that wanted to glow when user touch the button – pokche May 05 '16 at 00:06