【问题标题】:How to put a dynamic background image into a FrameLayout如何将动态背景图像放入 FrameLayout
【发布时间】:2014-01-03 02:25:32
【问题描述】:

我想提前谢谢你。我是制作 Android 应用程序的新手(实际上这是我的第一个 Android 应用程序)。在应用程序中,我让用户选择他/她的一张照片并将其作为背景放置。我收集图像并使用 putExtra 方法将其发送到正确的 Activity。

这就是我处理图像的方式:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0){
             Uri selectedImage = data.getData();
             InputStream is;
             try {
                 is = getContentResolver().openInputStream(selectedImage);
                 BufferedInputStream bis = new BufferedInputStream(is);
                 Bitmap imagen = BitmapFactory.decodeStream(bis);
                 final float densityMultiplier = getResources().getDisplayMetrics().density;        

                 int h= (int) (100*densityMultiplier);
                 int w= (int) (h * imagen.getWidth()/((double) imagen.getHeight()));
                 imagen=Bitmap.createScaledBitmap(imagen, w, h, true);

                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
                 imagen.compress(Bitmap.CompressFormat.JPEG, 100, baos);   
                 byte[] b = baos.toByteArray(); 

                 imagenCodificada = Base64.encodeToString(b, Base64.DEFAULT);

             } 
             catch (FileNotFoundException e) {}
        }
    }

这就是我发送它的方式:

i.putExtra("Preferencia5", imagenCodificada)

在活动中,我将图像解码为位图,但我不知道如何将其作为背景放入我的 FrameLayout。

在活动中:

  byte[] b = Base64.decode(imagen, Base64.DEFAULT);
  Bitmap imDecodificada = BitmapFactory.decodeByteArray(b, 0, b.length);

这是我布局的 .xml 的开始:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/framelayout">

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

非常感谢。

【问题讨论】:

    标签: android image layout dynamic background


    【解决方案1】:

    使用带有BitmapDrawable参数的layout.setBackgroundDrawable

    【讨论】:

    • 嗨 pskink,但我有一个问题(实际上真的很傻)我如何获得变量“布局”?谢谢
    • findViewById(R.id.framelayout)
    • 非常感谢,完美运行^_^。我还有一个问题,我知道这不是线程的主题,但是,你知道如何获取用户的手机背景并将其放入活动背景中吗?非常感谢。
    • 没有像“用户手机背景”这样的东西,因为每个用户都可以拥有不同的家庭启动器应用程序
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 2022-01-25
    • 2017-07-18
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    相关资源
    最近更新 更多