Pergunta

I have created XML file in drawable folder as code below:

XML Name is : background_bitmap_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:src="@drawable/background"
android:tileMode="disabled" />

Now, I want set the image to this xml file dynamically[through code].

I am making an app in which I have to set different background in each activity and those images are coming from online.

I did not found any solution to set the image src to this xml file as bitmap.

however I have another alternative to set background to the root layout as below:

bitmap = BitmapFactory.decodeFile(FILE_PATH + "/" + SettingsScreen.BACKGROUND_IMAGE);
d = new BitmapDrawable(cntx.getResources(), bitmap);

But I need to set the image resource to the above XML file so that Image does not get stretched if the size is very lesser than the device size.

Any help to set image resource to the xml file?

Foi útil?

Solução

you can try this one also,

URL url_value = new URL("your url");

ImageView img = (ImageView)v.findViewById(R.id.backgorundImage);
if (profile != null) {
    Bitmap mIcon1 =
        BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
    img.setImageBitmap(mIcon1);
}

and you can set ImageView to full activity. In this example images gets downloaded from internet and converted to bitmap and set to ImageView.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top