0

インターネットから画像を抽出し、それらを LinearLayout の背景として適用する必要があります - 可能ですか? これに適した方法はまだ見たことがありません。

4

3 に答える 3

0

を使用して画像をロードしBitmapFactory(必要に応じてサイズを変更します)、を使用BitmapDrawableして適用しますLinearLayout.setBackgroundDrawable()

于 2012-04-19T13:41:13.507 に答える
0

リニアレイアウトの背景としてイメージビューを設定します。つまり:

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView android:id="@+id/myimageview"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"/>
</LinearLayout>

次に、Java で次のようにします。

ImageView mImageView = (ImageView)findViewById(R.id.myimageview);

Bitmap bmImg;

URL myFileUrl = put in your url here;          
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);
       mImageView.setImageBitmap(bmImg);
} catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
}

お役に立てれば。また、このリファレンスを参照してください(画像ダウンロード コードを入手した場所)。

于 2012-04-19T13:46:35.883 に答える
0

もちろん。

画像の InputStream を取得するだけです:

InputStream is = (InputStream) new URL(url).getContent();

ストリームから Drawable を取得します。

Drawable d = Drawable.createFromStream(is, "src name");

次に、LinearLayout の Background Drawable を設定します。

linearLayout.setBackgroundDrawable(d);

これにより、実際にはストリームから直接画像が設定されます。ASyncTask を使用してバックグラウンドでドローアブルをプルダウンし、後で設定することもできます: http://developer.android.com/reference/android/os/AsyncTask.html

遅延ローダーについても調査することをお勧めします: http://evancharlton.com/thoughts/lazy-loading-images-in-a-listview

于 2012-04-19T13:48:52.530 に答える