一、整体介绍
ImageView的ScaleType都有很多介绍了,android:adjustViewBounds="true" 设置这个属性,在源码里可以看到。
@android.view.RemotableViewMethod
public void setAdjustViewBounds(boolean adjustViewBounds) {
mAdjustViewBounds = adjustViewBounds;
if (adjustViewBounds) {
setScaleType(ScaleType.FIT_CENTER);
}
}
这里很直观setScaleType(ScaleType.FIT_CENTER);其他ScaleType就不用考虑了,默认就是它了。
这个属性适应情景:当宽高有且仅有一个设置为wrapContent的时候是有用的。
作用:根据drawable的宽高比例,调制ImageView的设置为WrapContent的那个宽或者高。
二、demo测试
1、ImageView设置高为wrapContent,宽为固定值
2、Seek bar动态调节宽度为确定值
2、button切换android:adjustViewBounds的属性
代码如下
布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:background="@android:color/black"
android:scaleType="fitCenter"
android:src="@drawable/map" />
<SeekBar
android:id="@+id/seeKBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal" />
<Button
android:id="@+id/switch_adjustBounds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal" />
</FrameLayout>
代码
package qin.xue.imageviewscaletype;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
/**
* Created by qinxue on 2018/4/26.
*/
public class MainActivity extends Activity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
Button button;
ImageView imageView;
SeekBar seekBar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_layout);
imageView = findViewById(R.id.image);
button = findViewById(R.id.switch_adjustBounds);
seekBar = findViewById(R.id.seeKBar);
seekBar.setOnSeekBarChangeListener(this);
seekBar.setMax(getResources().getDisplayMetrics().widthPixels);
seekBar.setProgress(imageView.getWidth());
button.setOnClickListener(this);
setButtonText(imageView.getAdjustViewBounds());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.switch_adjustBounds:
switchAdjustBuounds();
break;
}
}
private void switchAdjustBuounds() {
imageView.setAdjustViewBounds(!imageView.getAdjustViewBounds());
setButtonText(imageView.getAdjustViewBounds());
}
private void setButtonText(boolean isAdjust) {
button.setText("AdjustViewBounds = " + imageView.getAdjustViewBounds());
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
ViewGroup.LayoutParams params = imageView.getLayoutParams();
params.width = progress;
imageView.setLayoutParams(params);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
三、测试
当android:adjustViewBounds="true" 时,图片高度会根据宽度的变化自适应,如图
当当android:adjustViewBounds="false" 时高度不会自适应,表现为fitCenter,高度保持为Drawable的高度不变,如图。
demo地址:https://github.com/xueqin123/AdjustViewBounds