通过URL改变的FrameLayout的背景图片 [英] change background image of Framelayout via URL
本文介绍了通过URL改变的FrameLayout的背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是它可能使用网址
设置背景
为的FrameLayout
?
例:
我想使用如这的是我的的FrameLayout
背景。
Example:
i want to use the URL like this as my framelayout
background.
我真正想要实现的是,当我的活动负荷将取回的图像从我的域名,并用它作为我的FrameLayout的背景图片。我能做到这一点,但我用的ImageView和一个按钮。
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.
下面是code,我使用:
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 codeS:
<?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)
使用转换图像URL中可绘制
Convert your image url in Drawable using
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的背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文