我有这张图片:
这就是它出现在小型设备上的方式:
看起来有点软弱。我希望它不会调整大小,按原样显示,不显示不适合屏幕的图像部分。喜欢这个
如果您没有注意到,上面图片中的手机屏幕背景不会调整大小,它只需要从中心图像适合屏幕。它可以切割不适合屏幕的部件,并从中心获取所需的部件。
当我在平板电脑上使用它时,它完美无缺,因为平板电脑的屏幕尺寸很大:
我该怎么做?
目前,这是我的代码:
<?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>
答案 0 :(得分: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 :(得分:-1)
而不是
android:src="@drawable/food_wallpaper"
试
keyword
答案 2 :(得分:-1)
尝试添加xml
android:scaleType="fitXY"