如何不按比例调整图像大小?

How to re-size an image not proportionaly?

我正在尝试制作一款类似于 "click & kill" 的游戏,并且我正在尝试为要杀死的角色制作一个健康栏。 我正在使用一个简单的图像(一个红色矩形),我想在单击后减少健康栏。我尝试过的方法有效,但问题不仅在于降低,还在于高度。所以结果真的很惨。首先,这是我的 XML(例如我只显示一个):

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="33">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFF00"
        android:id="@+id/hole4"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/healthBar4"
        android:src="@drawable/health_bar"/>
</RelativeLayout>

所以这没什么不好的(我认为)我离开 android:adjustViewBounds="true" 因为我认为问题出在这里。 接下来是我的 Activity :

final int healthBarHeightInitial = healthBar4.getLayoutParams().height;
final int healthBarWidthInitial = healthBar4.getLayoutParams().width;
healthBar4.requestLayout();

//ivHole4 is my ImageView I get the click to leave some life to the character
ivHole4.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        //If character die (works great).
        if(choixAction.ChoixAction(CharaHole4,outil)){
            Log.d("Perso","Character is die");
            mAvancement +=1;
            ivHole4.setImageResource(0);
            CharaHole4 = null;
            placeChara.setHole4(true);
            healthBar4.setVisibility(View.GONE);
            healthBar4.getLayoutParams().height = healthBarHeightInitial;
            healthBar4.getLayoutParams().width = healthWidthInitial;
        }
        //if character don't die (here is the problem !)
        else {
            healthBar4.getLayoutParams().width = healthBar4.getWidth()/2; //This is works great
            healthBar4.getLayoutParams().height = healthBarHeightInitial; //This is do nothing, the height is /2 too.
            healthBar4.requestLayout();
        }

    }
});

我希望有人知道如何不按比例更改图像大小。 谢谢提前。

使用 Picasso Library 并设置裁剪中心。

您在 XML 布局中的 ImageView 需要将比例类型设置为 fitXY 以允许它在不保持比例的情况下展开。

android:scaleType="fitXY"