ログイン画面の背景画像のJSON
画像URLを解析するスプラッシュ画面の時点で解析しました。ログイン画面のサンプルコードは次のとおりです。XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/loginLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_bg" <!-- I want to change this background dynamically. -->
android:focusableInTouchMode="true"
android:gravity="center"
tools:context=".activity.LoginActivity" >
<ScrollView
android:id="@+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- .... Here is edit text for login inputs and buttuns for singnup and login. -->
</LinearLayout>
</ScrollView>
</RelativeLayout>
上記では、背景に静止画像を配置しましたRelativeLayout
が、画像のURLに応じてバックグラウンドを変更可能にしたいです。
前もって感謝します。
URL画像をビットマップに変換してからビットマップ画像をDrawableに変換し、RelativeLayoutに設定する必要があります。
まず、URL画像をビットマップに変換します。サンプルコードを参照してください。
Bitmap myImage = getBitmapFromURL("http://looksok.files.wordpress.com/2011/12/me.jpg");
相対レイアウトリファレンスを取得
RelativeLayout rLayout=(RelativeLayout)findViewById(R.id.relativeLayout);
BitmapDrawable(obj)は、ビットマップオブジェクトをドローアブルオブジェクトに変換します。
Drawable dr = new BitmapDrawable(myImage);
rLayout.setBackgroundDrawable(dr);
URL画像からビットマップへの変換方法
public Bitmap getBitmapFromURL(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加