我一直试图实现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
}
这样
,非常高密度像素图像没有在主页显示。
任何人都可以帮助我。谢谢。