如何在android中应用9个补丁图像

时间:2013-09-20 14:39:29

标签: android android-imageview nine-patch

我的图像宽高比为7:2。
目前,我正在scaleType="fitXY"使用android:width="match_parent"android:height="wrap_content"
但是由于不规则的缩放,图像中的文本是倾斜的,我没有得到高质量的图像。可以使用9个补丁图像来解决我的其他解决方法的问题吗?

1 个答案:

答案 0 :(得分:1)

您确实可以使用9补丁图像,但是您需要拉伸图像的非文本部分,以便文本不会拉伸。这很可能会在大比例图像中显示可能很小的文本而产生不良结果。

更好的方法是使用无文本的9patch图像并将其用作TextView的背景,并使用textView.setTextSize(x)缩放文本以适合图像。

例如

    TextView theText = new TextView(this);
    theText.setBackgroundResource(R.drawable.yourninepatch);
    float scale = c.getResources().getDisplayMetrics().density;
    int screenWidth = c.getResources().getDisplayMetrics().widthPixels;
    float fontSize = screenWidth / 26 / scale;
    theText.setTextSize(fontSize);
相关问题