2016-10-12 32 views
0

我一直试图实现Facebook图像加载宽度和高度,基于主页中所有屏幕大小的宽度和高度。比较屏幕宽度,高度和负载动态图像,而不会丢失宽高比,以适应所有屏幕设备

下面我已经发布了代码,到目前为止我已经尝试过。

我已经在两个方面尝试过:

第一种方式:

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); 
    } 
} 

但我没有得到不同尺寸图像的预期结果,它不会超过高密度像素图像的高度。

方式二:

获取屏幕宽度和高度,并做一些计算:

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

我解决了方式这个问题:

第一种方式:

我已经做了一些改变与ImageView.I除去imageview.Using ImageView的widthheight包裹内容外部类使用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的宽度和高度。因此,我使用宽度和高度处理适配器中的所有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

相关问题