だから私はこの方法でURLから画像をロードしています
public Drawable loadImage(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.out.println(e.getMessage()+" oh noo ");
return null;
}
}
しかし、画像の幅をその親と同じにしたいです(つまり、高さと幅が100%のルート要素です)。XML のみを使用してそれを行う方法が見つかりませんでした。常に端にパディングが残ります。そして、それは画像が画面の同じ比率を持っていないためであることがわかりました. たとえば、画面は 16:9、画像は 4:3 です。Android は、この違いを埋めるためにパディングを作成します。
画像をロードするときにプログラムでそれを行う方法を知っていますか? そして、デバイスが回転しても画面の幅と同じ大きさを維持するために、この画像が必要です(したがって、ゲインを計算する必要があると思います)
このコードを使用して画面のサイズを取得しています
public Size getScreenSize(){
Size screenSize = new Size();
DisplayMetrics metrics = new DisplayMetrics();
this.activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenSize.height = metrics.heightPixels;
screenSize.width = metrics.widthPixels;
return screenSize;
}
public class Size{
public double height;
public double width;
}
@EDIT1
これは、ドローアブルをビューに挿入する方法です。ビューはアクティビティの XML で既に宣言されています
ImageView picture = (ImageView) findViewById(R.id.picture);
picture.setImageDrawable(loadImage(url));
@EDIT2
これが私がアクティビティのレイアウトとそのスタイルを書いた方法です
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<!-- style="@style/globalHeader" -->
<LinearLayout
style="@style/globalHeader"
>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="18sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="14sp"
/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/picture"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="#f00"
>
</ImageView>
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
/>
<TextView
android:id="@+id/details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
これが私が達成したい外観です
https://docs.google.com/a/uniriotec.br/file/d/0B2abPynX9PhkODhRVEkwcUphY28/edit?pli=1