ホームページのすべての画面サイズの幅と高さに基づいて、幅と高さのフェイスブックイメージをロードしようとしています。画面の幅と高さを比較し、すべての画面デバイスに合わせてアスペクト比を失うことなく動的イメージをロード
以下、これまでに試したコードを掲載しました。
私は2つの方法でそれを試してみました:
最初の方法:
home_activity_layout.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
>
<LinearLayout
android:id="@+id/rl_vertical_list"
android:visibility="visible"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/post_items_layout_middle_home"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp" >
<com.steve.thirdparty.ScalableImageView
android:id="@+id/iv_posted_img_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:src="@drawable/golive_load_image"
android:layout_below="@+id/tv_user_posted_msg_post_items_home"
android:contentDescription="@string/cont_desc"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
ScalableImageView.java:
public class ScalableImageView extends ImageView {
public ScalableImageView(Context context) {
super(context);
}
public ScalableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScalableImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
if (d != null) {
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = w * d.getIntrinsicHeight()/d.getIntrinsicWidth();
Log.e("Width", ""+w);
Log.e("Height", ""+h);
setMeasuredDimension(w, h);
}
else super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
しかし、異なる次元のimage.itで予想される結果が得られず、高密度ピクセル画像の高さを超えません。
第二の方法:
画面の幅と高さを取得し、いくつかの計算を実行します。このように
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
float screenHeight= displayMetrics.heightPixels/displayMetrics.density;
float screenWidth = displayMetrics.widthPixels/displayMetrics.density;
if(ImageHeight > ImageWidth)
{
float scale = ImageWidth/ImageHeight
float screenWidth = 320 (for example)
float getHeight = screenWidth /scale
}
、非常に高濃度画素の画像は、ホームページに表示されていません。
誰でもこれを手伝うことができます。ありがとうございます。