2016-08-24 22 views
0

我有这样的形象:Android - 是否有可能阻止背景变得更小,但允许它调整自己的大小?

Image

这是它如何出现在小型设备:

Image

它看起来有点湿软。我希望它不会调整它的大小,按原样显示它,不会显示不适合屏幕的图像部分。像这样

Image

如果您有没有注意到,这款手机的在PIC屏幕上方具有wan't 调整,它只是把尽可能符合从中央图像画面的背景。它切割不适合屏幕的部分,并从中心获取尽可能多的部分。

当我使用它的平板电脑,它完美的作品,因为片有一个大的屏幕尺寸:

enter image description here

我怎样才能做到这一点?

目前,这是我的代码:的

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_sign_in" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.alibdeir.irecipify.SignInActivity" 
    android:background="@drawable/food_wallpaper"> 

</android.support.constraint.ConstraintLayout> 
+2

问题不在于它不缩放,而在于您试图将其缩放到不同的长宽比。这会带来不好的结果(除非它们相对接近)。选择适当的宽高比的图像,并使用它。另外,如果你想缩放,你不应该使用背景 - 使用设置为父布局大小的图像视图,这将允许你使用scaleType。 –

+0

@GabeSechan谢谢,通过将scaleType设置为centercrop可以很好地工作。请把这个作为答案。 –

回答

0

我通过制作ScaleImageView java类固定它:

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 

/** 
* Created by Ab-Me on 25-Aug-16. 
* This is the imageView for the background image 
*/ 
@SuppressWarnings("all") 
public class ScaleImageView extends ImageView { 
    private ImageChangeListener imageChangeListener; 
    private boolean scaleToWidth = false; // this flag determines if should measure height manually dependent of width 

    public ScaleImageView(Context context) { 
     super(context); 
     init(); 
    } 

    public ScaleImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public ScaleImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    private void init() { 
     this.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
    } 

    @Override 
    public void setImageBitmap(Bitmap bm) { 
     super.setImageBitmap(bm); 
     if (imageChangeListener != null) 
      imageChangeListener.changed((bm == null)); 
    } 

    @Override 
    public void setImageDrawable(Drawable d) { 
     super.setImageDrawable(d); 
     if (imageChangeListener != null) 
      imageChangeListener.changed((d == null)); 
    } 

    @Override 
    public void setImageResource(int id) { 
     super.setImageResource(id); 
    } 

    public interface ImageChangeListener { 
     // a callback for when a change has been made to this imageView 
     void changed(boolean isEmpty); 
    } 

    public ImageChangeListener getImageChangeListener() { 
     return imageChangeListener; 
    } 

    public void setImageChangeListener(ImageChangeListener imageChangeListener) { 
     this.imageChangeListener = imageChangeListener; 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

     int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
     int heightMode = MeasureSpec.getMode(heightMeasureSpec); 
     int width = MeasureSpec.getSize(widthMeasureSpec); 
     int height = MeasureSpec.getSize(heightMeasureSpec); 

     /** 
     * if both width and height are set scale width first. modify in future if necessary 
     */ 

     if (widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST) { 
      scaleToWidth = true; 
     } else if (heightMode == MeasureSpec.EXACTLY || heightMode == MeasureSpec.AT_MOST) { 
      scaleToWidth = false; 
     } else 
      throw new IllegalStateException("width or height needs to be set to match_parent or a specific dimension"); 

     if (getDrawable() == null || getDrawable().getIntrinsicWidth() == 0) { 
      // nothing to measure 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
      return; 
     } else { 
      if (scaleToWidth) { 
       int iw = this.getDrawable().getIntrinsicWidth(); 
       int ih = this.getDrawable().getIntrinsicHeight(); 
       int heightC = width * ih/iw; 
       if (height > 0) 
        if (heightC > height) { 
         // dont let hegiht be greater then set max 
         heightC = height; 
         width = heightC * iw/ih; 
        } 

       this.setScaleType(ScaleType.CENTER_CROP); 
       setMeasuredDimension(width, heightC); 

      } else { 
       // need to scale to height instead 
       int marg = 0; 
       if (getParent() != null) { 
        if (getParent().getParent() != null) { 
         marg += ((RelativeLayout) getParent().getParent()).getPaddingTop(); 
         marg += ((RelativeLayout) getParent().getParent()).getPaddingBottom(); 
        } 
       } 

       int iw = this.getDrawable().getIntrinsicWidth(); 
       int ih = this.getDrawable().getIntrinsicHeight(); 

       width = height * iw/ih; 
       height -= marg; 
       setMeasuredDimension(width, height); 
      } 

     } 
    } 

} 

这是确定的简单复制和粘贴这个类,它是与使用图书馆)在你的XML

然后(你的父母必须的RelativeLayout)

<!-- This is the UI of the class you created --> 
<com.your.package.name.ScaleImageView 
    android:id="@+id/background" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true" 
    android:adjustViewBounds="true" 
    android:scaleType="centerCrop" 
    android:src="@drawable/food_wallpaper" 
    tools:ignore="ContentDescription" /> 

android:scaleType="centerCrop"使图像裁剪到中心。 alignX属性使图像全屏。

-1

代替

android:background="@drawable/food_wallpaper" 

尝试

android:src="@drawable/food_wallpaper" 
+0

这不会改变结果 –

-1

尝试在你的XML添加

android:scaleType="fitXY" 
+0

'android:scaleType =“fitXY”'代码会缩放图像以适合屏幕 – androidXP