在app的文章中,常常会夹杂着一些特别长的长图。在阅读的时候须要滑动好久才能看图片下方的文字,所以对于长图只展现图片上面一部分,而且能够展开这个功能是很重要的。java
利用scaleType
的matrix
属性以及直接改变图片的高度来实现图片的收起与展开。android
scaleType
属性介绍center
:保持原图的大小,显示在ImageView
的中心。当原图的size
大于ImageView
的size
,超过部分裁剪处理;centerInside
:以原图彻底显示为目的,将图片的内容完整居中显示,经过按比例缩小原图的size
宽(高)等于或小于ImageView
的宽(高)。若是原图的size
自己就小于ImageView
的size
,则原图的size
不做任何处理,居中显示在ImageView
;centerCrop
:以填满整个ImageView
为目的,将原图的中心对准ImageView
的中心,等比例放大原图,直到填满ImageView
为止(指的是ImageView
的宽和高都要填满),原图超过ImageView
的部分做裁剪处理;matrix
:不改变原图的大小,从ImageView
的左上角开始绘制原图,原图超过ImageView
的部分做裁剪处理;fitCenter
:把原图按比例扩大或缩小到ImageView
的高度,居中显示;fitEnd
:把原图按比例扩大(缩小)到ImageView
的高度,显示在ImageView
的下部分位置;fitStart
:把原图按比例扩大(缩小)到ImageView
的高度,显示在ImageView
的上部分位置;fitXY
:把原图按照指定的大小在View
中显示,拉伸显示图片,不保持原比例,填满ImageView
根据以上属性介绍,能够知道matrix属性是咱们要的。设计模式
<ImageView android:id="@+id/iv_long_picture" android:layout_width="match_parent" android:layout_height="@dimen/dp_146" android:layout_below="@id/tv_main_content_question" android:adjustViewBounds="true" android:scaleType="matrix" android:src="@color/color_333333" />
<TextView android:id="@+id/tv_expand_collapse" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/iv_long_picture" android:layout_marginBottom="@dimen/dp_16" android:layout_marginTop="@dimen/dp_10" android:drawableEnd="@drawable/down_icon" android:drawablePadding="@dimen/dp_7" android:text="@string/expand_all" android:textColor="@color/color_99" android:textSize="@dimen/sp_14" android:textStyle="bold" android:visibility="gone" />
复制代码
使用Glide
加载的图片,app
Glide.with(this)
.load(mainContentBean.getAccessory().get(0))
.into(ivLongPicture);
复制代码
直接经过设置imageView
的高度来实现图片的展开与收起,框架
tvExpandCollapse.setOnClickListener(new View.OnClickListener() {
boolean expanded = false;
@Override
public void onClick(View v) {
if (expanded) {
// 收起
ViewGroup.LayoutParams params = ivLongPicture.getLayoutParams();
params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
params.height = DensityUtil.dip2px(MainContentActivity.this, 146);
ivLongPicture.setLayoutParams(params);
expanded = false;
tvExpandCollapse.setText(R.string.expand_all);
tvExpandCollapse.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.down_icon, 0);
scMainContent.smoothScrollTo(0, 0);
} else {
// 展开
ViewGroup.LayoutParams params = ivLongPicture.getLayoutParams();
params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
params.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
ivLongPicture.setLayoutParams(params);
expanded = true;
tvExpandCollapse.setText(R.string.collapse_all);
tvExpandCollapse.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.upper_icon, 0);
}
}
});
复制代码
根据以上的思路以及代码实现,普通的长图确实可以作到“展开”和“收起”功能。ide
可是对于原图宽度超过手机宽度的图片来讲,宽度并无显示彻底!布局
对于Glide
版本4.0
以上,若是宽度过大,会等比例缩放至宽度等于ImageView
的宽度,所以并不会有问题,可是咱们的项目用Glide
版本是3.7
的,并且不容易升级,故此方法不可行。学习
查阅了Glide
的文档,了解了Glide
能够在图片下载完成后对图片进行一些操做,操做完成以后的图片天然就成了ImageView
认为的原图了。this
所以,能够在加载以前将宽度过大的图片等比例缩放,缩放完成后再加载到ImageView中去。spa
Glide.with(this)
.load(mainContentBean.getAccessory().get(0))
.asBitmap()
.listener(new RequestListener<String, Bitmap>() {
@Override
public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
int imageWidth = resource.getWidth();
int imageHeight = resource.getHeight();
WindowManager manager = (WindowManager) MainContentActivity.this
.getSystemService(Context.WINDOW_SERVICE);
// 屏幕宽度减去margin值
int width = manager.getDefaultDisplay().getWidth() - DensityUtil.dip2px(MainContentActivity.this, 32);
float scaleRate = width * 1.0f / imageWidth;
//设置matrix
Matrix matrix = new Matrix();
//设置放缩比例
matrix.setScale(scaleRate, scaleRate);
ivLongPicture.setImageMatrix(matrix);
if (imageHeight * scaleRate > DensityUtil.dip2px(MainContentActivity.this, 146)) {
tvExpandCollapse.setVisibility(View.VISIBLE);
} else {
tvExpandCollapse.setVisibility(View.GONE);
}
return false;
}
})
.into(ivLongPicture);
复制代码
ImageView
的scaleType
属性的各个属性值须要了解;Glide
版本之间的差别须要了解;ImageView
如何根据scaleType
进行图片切割的须要了解(以后有时间阅读源码);Glide
是一个庞然大物,也是一个很值得学习的框架,须要熟悉掌握(以后有时间阅读源码)Android的优点在于开源,开源的好处在于易于学习,容易更改。对于开源的框架,仅仅是掌握是不够的,还须要好好的了解框架设计的一些设计模式,框架的优缺点等。