ImageView宽度填满屏幕,高度自适应

你的名字 2022-06-17 04:45 397阅读 0赞

原文链接:http://blog.csdn.net/easion_zms/article/details/50263409#comments

在CSS中轻松设置width=100%就可以使得图片宽度充满屏幕,高度自适应,那么在Android里面怎样才能实现这种效果呢?

使用默认的ImageView

首先试一下默认的ImageView的效果,布局文件如下activity_main_2.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
  3. <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/test" android:background="#FF0"/>
  4. </LinearLayout>

MainActivity2.Java

  1. public final class MainActivity2 extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.main_activity_2);
  6. final ImageView imageView = (ImageView)findViewById(R.id.image);
  7. ViewTreeObserver vto2 = imageView.getViewTreeObserver();
  8. vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  9. @Override
  10. public void onGlobalLayout() {
  11. imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
  12. Log.e("TestMeasure","w=" + imageView.getWidth() + " h=" + imageView.getHeight());
  13. }
  14. });
  15. }
  16. }

test.jpg(正方形998*998)

Center

可以看到,布局文件设置ImageView充满了屏幕,高度自适应,那运行后是我们需要的效果吗?

Center 1

结果令人失望,虽然ImageView的宽度充满了屏幕,但是图片并没有填满屏幕,这是为什么呢?

原因分析

回头看看ActivityMain2.java中的打印我们看到了ImageView的宽高信息:

  1. TestMeasure w=1080 h=998

ImageView的高度是和图片的高度一样的,998px。ImageView的宽度和屏幕宽度一样,1080px。

由于ImageView默认的scaleType是fitCenter,将图片按比例扩大(或缩放)到视图的宽(或高)然后居中显示,所以图片宽度没有充满屏幕的原因就是,ImageView的高度不够,它限制了图片的放大倍数。

自定义ImageView

那如何才能将ImageView的高度放大,使得图片宽度刚好充满屏幕呢?答案肯定在onMeasure方法中。

  1. public class ResizableImageView extends ImageView {
  2. public ResizableImageView(Context context) {
  3. super(context);
  4. }
  5. public ResizableImageView(Context context, AttributeSet attrs) {
  6. super(context, attrs);
  7. }
  8. @Override
  9. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
  10. Drawable d = getDrawable();
  11. if(d!=null){
  12. // ceil not round - avoid thin vertical gaps along the left/right edges
  13. int width = MeasureSpec.getSize(widthMeasureSpec);
  14. //高度根据使得图片的宽度充满屏幕计算而得
  15. int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
  16. setMeasuredDimension(width, height);
  17. }else{
  18. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  19. }
  20. }
  21. }

使用自定义的控件显示图片

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
  2. <org.sample.ResizableImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/test" android:background="#FF0"/>
  3. </LinearLayout>

Center 2

发表评论

表情:
评论列表 (有 0 条评论,397人围观)

还没有评论,来说两句吧...

相关阅读