Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does setImageBitmap has no effect on custom ImageButton?

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);
    }
    // ....
} 
like image 283
pokche Avatar asked Apr 25 '16 23:04

pokche


1 Answers

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.

like image 148
jobbert Avatar answered Sep 27 '22 23:09

jobbert