Why does setImageBitmap not affect custom ImageButton?

I found the this link, which says how I could make a glow effect on my ImageButton . Therefore, whenever the user clicks the ImageButton button, the onTouchEvent() function is onTouchEvent() and there I call the setImageBitmap() function. The problem is that this function ( setImageBitmap() ) does not seem to have any effect.

What works:

1) So far I have expanded 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 the Activity create a Pen instance (custom ImageButton ), define setGlow() and call setImageBitmap() from the Pen instance.

//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 does not work:

1) All of the above (ImageButton extension in android), but this time I'm calling setImageBitmap from inside onTouchEvent (). For this case, I define the setGlow function 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 the xml file I:

  ..... <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); } // .... } 
+8
android bitmap android-bitmap android-drawable bitmapimage
source share
3 answers

I built the project at the time of publication, but it works great. It sets the bitmap correctly. Therefore, perhaps the getGlow () method does not work. Make some magazines below

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

then check the return is correct.

+3
source share

I think this could be a classic memory issue. You can try using a startup icon bitmap if you want to check if this is valid:

 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 a problem, check out this link.

+5
source share

You cannot do this because you will lose memory if you put setImageBitmap (setGlow (R.drawable.pen)) in onTouchEvent.

onTouchEvent will run the servo once per second. And you will create bitmaps of servals in memory per second. You will run out of memory before you set up ImageBitmap successfully.

+5
source share

All Articles