本文介绍了通过 URL 更改 Framelayout 的背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用url
来为framelayout
设置background
?
is it possible to use a url
to set background
for framelayout
?
示例:我想使用像 this 这样的 URL 作为我的 framelayout
背景.
Example:i want to use the URL like this as my framelayout
background.
我真正想要实现的是,当我的活动加载时,它将从我的域中获取图像并将其用作我的框架布局背景图像.我能够做到这一点,但我使用了一个图像视图和一个按钮.
what i really want to achieve is that when my activity loads it will fetch the image from my domain and use it as my framelayout background image. i was able to do this but i used a imageview and a button.
以下是我使用的代码:
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" />
推荐答案
感兴趣的东西,(我从来没有尝试过,但应该可以)
Something interested, (I never try this but it should work)
FrameLayout fm = (FrameLayout)findViewById(R.id.FrameLayout01);
Drawable drw = ImageOperations(this,url,filename)
fm.setBackgroundDrawable(drw)
使用
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;
}
试试这个,让我知道会发生什么..
Try this and let me know what happen..
这篇关于通过 URL 更改 Framelayout 的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!