【问题标题】:change background image of Framelayout via URL通过 URL 更改 Framelayout 的背景图像
【发布时间】:2011-12-21 06:53:15
【问题描述】:

是否可以使用urlbackground 设置为framelayout

示例: 我想使用像this 这样的URL 作为我的framelayout 背景。

我真正想要实现的是,当我的活动加载时,它将从我的域中获取图像并将其用作我的框架布局背景图像。我能够做到这一点,但我使用了一个图像视图和一个按钮。

下面是我使用的代码:

public class MyAppActivity extends Activity {

ImageView imView;
    String imageUrl= "http://www.mydomain.com/resource/images/";

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
Button bt3= (Button)findViewById(R.id.postbutton);
bt3.setOnClickListener(getImgListener);

}
View.OnClickListener getImgListener = new View.OnClickListener()
{

      public void onClick(View view) {

           downloadFile(imageUrl+"image"+".png");
           Log.i("im url",imageUrl+"image"+".png");
      }

};


Bitmap bmImg;
void downloadFile(String fileUrl){
      URL myFileUrl =null;          
      try {
           myFileUrl= new URL(fileUrl);
      } catch (MalformedURLException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
      }
      try {
           HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
           conn.setDoInput(true);
           conn.connect();
           InputStream is = conn.getInputStream();

           bmImg = BitmapFactory.decodeStream(is);
           imView.setImageBitmap(bmImg);
      } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
      }
 }

 }

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/rakistaradiobg"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="98dp"
    android:layout_gravity="bottom|center"
    android:scaleType="fitXY"
    android:src="@drawable/btnbg" />

<Button
    android:id="@+id/postbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:text="Button" />

【问题讨论】:

    标签: android layout background-image android-framelayout


    【解决方案1】:

    感兴趣的东西,(我从未尝试过,但它应该可以)

    FrameLayout fm = (FrameLayout)findViewById(R.id.FrameLayout01);      
    Drawable drw = ImageOperations(this,url,filename)
    fm.setBackgroundDrawable(drw)
    

    在 Drawable 中使用

    转换你的图片 url
    private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
            try {
                InputStream is = (InputStream) this.fetch(url);
                Drawable d = Drawable.createFromStream(is, saveFilename);
                return d;
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return null;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    
        public Object fetch(String address) throws MalformedURLException,IOException {
            URL url = new URL(address);
            Object content = url.getContent();
            return content;
        }
    

    试试这个,让我知道会发生什么..

    【讨论】:

    • 它给了我一个错误,它说:SKImageDecoder: :Factory Returned null 这是什么意思??
    • setBackgroundDrawable() 在 API 16 中已弃用。如何将图像设置为 FrameLayout 的背景
    • @Hemant - 你仍然可以使用它,因为它具有向后兼容性。
    【解决方案2】:

    您可以使用WebView 来显示网站内容。 There is 也是在安卓开发者网站上使用WebView 的精彩教程

    【讨论】:

      【解决方案3】:

      这是您可以从 URL 获取位图中的图像的代码

       Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
      

      并使用This 可以将位图转换为可绘制...

      在获得可绘制后应用到布局中

      framelyt.setBackgroundDrawable(d);
      

      【讨论】:

        【解决方案4】:
        private RelativeLayout myRelativeLauout;
        
        myRelativeLauout = (RelativeLayout)findViewById(R.id.rlBg);  
        new LoadBackground("http://www.tmonews.com/wp-content/uploads/2012/10/androidfigure.jpg",
                    "androidfigure").execute();
        
        
        private class LoadBackground extends AsyncTask<String, Void, Drawable> {
        
            private String imageUrl , imageName;
        
            public LoadBackground(String url, String file_name) {
                this.imageUrl = url;
                this.imageName = file_name;
            }
        
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
        
            @Override
            protected Drawable doInBackground(String... urls) {
        
                try {
                    InputStream is = (InputStream) this.fetch(this.imageUrl);
                    Drawable d = Drawable.createFromStream(is, this.imageName);
                    return d;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    return null;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
            private Object fetch(String address) throws MalformedURLException,IOException {
                URL url = new URL(address);
                Object content = url.getContent();
                return content;
            }
        
        
            @Override
            protected void onPostExecute(Drawable result) {
                super.onPostExecute(result);
                myRelativeLauout.setBackgroundDrawable(result);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-03-11
          • 1970-01-01
          • 2011-06-21
          • 1970-01-01
          • 2021-06-17
          • 2023-03-12
          • 1970-01-01
          相关资源
          最近更新 更多