2016-10-12 8 views
0

ホームページのすべての画面サイズの幅と高さに基づいて、幅と高さのフェイスブックイメージをロードしようとしています。画面の幅と高さを比較し、すべての画面デバイスに合わせてアスペクト比を失うことなく動的イメージをロード

以下、これまでに試したコードを掲載しました。

私は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 


} 

、非常に高濃度画素の画像は、ホームページに表示されていません。

誰でもこれを手伝うことができます。ありがとうございます。

答えて

1

私は2つのの方法でこの問題を解決:私はImageView.Iといくつかの変更は、コンテンツをラップするImageViewのwidthheightをimageview.Usingため、外部クラスを削除していた

最初の方法adjustviewboundsを使用して、すべてのスクリーンデバイスに合ったイメージビューを作成できます。

<ImageView 
    android:id="@+id/iv_posted_img_home" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:adjustViewBounds="true" 
    android:scaleType="fitStart" 
    android:src="@drawable/load_image" 
    android:layout_below="@+id/tv_user_posted_msg_post_items_home" 
    android:contentDescription="@string/cont_desc"/> 

注:幅と高さwrapcontentを使用してがエラーのうち、メモリが発生します。

第二の方法:

このソリューションでは、メモリエラーのうち、超えてはならないための最も安全なソリューションです。

jsonレスポンスでは、imageview.Soの幅と高さを取得しています。アダプタの幅と高さを使用して、すべてのimageviewを処理しています。

アダプターコード:詳細については

if(rowItem.getWidthImage() != null){ 

        if(rowItem.getWidthImage().equals(null) || rowItem.getWidthImage().equals("")){ 

         Log.e("InvalidImage", "Test"); 

        }else { 

         Log.e("imageWidth", ""+rowItem.getWidthImage()); 
         Log.e("imageHeight", ""+rowItem.getHeightImage()); 

         int imageWidth = Integer.parseInt(rowItem.getWidthImage()); 
         int imageHeight = Integer.parseInt(rowItem.getHeightImage()); 


         DisplayMetrics dm = new DisplayMetrics(); 
         ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm); 

         int width = dm.widthPixels; 
         int height = width * imageHeight/imageWidth; 

         params = new LinearLayout.LayoutParams(width, height); 

         holder.ivPostedImageNew.setLayoutParams(params); 
         holder.llPostedImage.addView(holder.ivPostedImageNew); 


         Glide.with(context).load(rowItem.getPosteduserpostimage()).placeholder(R.drawable.golive_load_image).error(R.drawable.bg_golive).into(holder.ivPostedImageNew); 


        } 


       } 

here

を参照してください。
関連する問題