是否可以使用url
来设置background
的framelayout
?通过URL更改Framelayout的背景图像
例如: 我想使用像this这样的URL作为我的framelayout
背景。
我真正想要实现的是,当我的活动加载它将从我的域中获取图像并将其用作我的framelayout背景图像。我能够做到这一点,但我用了一个imageview和一个按钮。
下面是我使用的代码:
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" />
好了,测试它,做工精细,你可以使用此代码.. – user370305 2011-12-21 07:26:10
它给了我一个错误它说:SKImageDecoder :: Factory返回null这是什么意思? – HeartlessArchangel 2011-12-22 01:45:25
是说没有找到页面 – HeartlessArchangel 2011-12-22 05:58:50