implement android:src=“@drawable/image” programmatically in Android

前端 未结 6 1064
广开言路
广开言路 2021-02-03 18:24

I am trying to set the foreground image on an image button. After some research, I came across this code sample:



        
6条回答
  •  一生所求
    2021-02-03 19:00

    Try this:

    ImageButton btn = (ImageButton)findViewById(R.id.button1);
    btn.setImageResource(R.drawable.newimage);
    

    where newimage is the image name in drawable folder.

    EDITED
    try this:

    ImageButton btn = (ImageButton)findViewById(R.id.button1);
    btn.setImageBitmap(bm);
    

    where bm is bitmap extracted from server.

    EDITED AGAIN
    I see you receive a Drawable; well, do this:

    normalImage = Drawable.createFromStream(code);
    Bitmap bm = ((BitmapDrawable)normalImage).getBitmap();
    ImageButton btn = (ImageButton)findViewById(R.id.button1);
    btn.setImageBitmap(bm);
    

提交回复
热议问题