Android 加载长图

有时候我们需要加载一个长图放到ScrollView中,但是在ImageView中不论那种ScaleType都不能合适的展示图片,需要手动适配一下,做法很简单,留着以后用到的时候找出来直接用就好了


<ImageView
android:id="@+id/imageView_pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@mipmap/pic" />


/**
 * 根据图片大小按比例适配全屏
*
 * @param imageView
* @param picWidth
* @param picHeight
*/
public static void fitImage(Activity activity, ImageView imageView, float picWidth, float picHeight) {
    WindowManager wm = activity.getWindowManager();
    int width = wm.getDefaultDisplay().getWidth();
    float height = (float) width / picWidth * picHeight;
    ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
    layoutParams.height = (int) height;
    imageView.setLayoutParams(layoutParams);
}

 

posted @ 2017-04-27 23:26  猴子1  阅读(467)  评论(0编辑  收藏  举报