安卓:我的按钮图像被放大的应用程序? [英] Android : my button image is enlarged in the app?
问题描述
我用一个按钮,图像为背景,这个图片的大小为30x29,但它的调整和扩大X2(我认为)。下面是我的按钮的XML code:
I use a button with an image as its background, this image has a size of 30x29, but it's resized and enlarged x2 (I think). Here is the XML code of my button :
<Button
android:id="@+id/buttonBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="10dp"
android:background="@drawable/bouton_back"
android:maxHeight="30dp"
android:maxWidth="30dp" />
和Java中:
backButton = (Button) findViewById(R.id.buttonBack);
有什么不好呢?我做同样的与其他按键并没有问题... ...谢谢你。
What is wrong with that ? I do the same with other buttons and there is no problem... thank you.
推荐答案
您在使用多种密度您提供的资源 bouton_back
?使用 DP
单位大小的按钮是不够的;您还必须提供图像的多个大小:
Are you supplying bouton_back
in your resources using multiple densities? Using dp
units to size your button isn't sufficient; you must also provide multiple sizes of your images:
- LDPI:120DPI
- MDPI:160dpi
- 华电国际:240dpi
- xhdpi:320dpi
转换公式如下:
px = dp * (dpi / 160)
其中,像素
是以像素为单位的最终图像大小, DP
的密度无关单位所需的大小,和 DPI
是目标密度。
Where px
is the final image size in pixels, dp
is the desired size in density-independent units, and dpi
is the target density.
简化这个公式,在你的 MDPI
图像为基准的使用像素大小:
Simplifying this formula, using the size in pixels of your mdpi
images as the baseline:
ldpi = mdpi * 0.75
hdpi = mdpi * 1.5
xhdpi = mdpi * 2.0
让我们回到你的样品code,如果你想有一个按钮,通过30dp是30dp,你应该为每个密度提供的图像:
Going back to your sample code, if you want a button that is 30dp by 30dp, you should be providing images for each density:
- LDPI:23像素点¯x23像素
- MDPI:30PX点¯x30PX
- 华电国际:×45像素点¯x×45像素
- xhdpi:60像素点¯x60像素
这篇关于安卓:我的按钮图像被放大的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!