【问题标题】:Android: Changing program background with image from galleryAndroid:使用图库中的图像更改程序背景
【发布时间】:2015-07-18 13:44:51
【问题描述】:

所以我希望用户可以选择用他自己的图片从图库中更改程序背景。我有一个按钮,我将图像保存为位图并尝试使用 button.setBackgroundDrawable() 来执行此操作,但它将图片放在按钮上,因此只能看到按钮文本并且它也不会使图片全屏.这是我的一些代码:

if (bitmap != null) {
                bitmap.recycle();
            }
            InputStream stream = getContentResolver().openInputStream(data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();

            BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
            button.setBackgroundDrawable(ob);

我有这样的 xml 设置:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context="com.example.proba.MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:onClick="pickImage"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="106dp"
    android:layout_marginTop="192dp"
    android:text="Button" />

提前谢谢你。

【问题讨论】:

    标签: android


    【解决方案1】:

    你可以看看at this,因为你是从图库中获取图片的,你可以给你的根布局一个id,然后设置像rootlayout.setBackgroundDrawable(ob);这样的背景

    【讨论】:

    • 非常感谢,它成功了。我将不得不进行一些测试,但看起来这是解决方案。
    • 好吧,我是这个网站的新手。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 2012-07-14
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多