简体   繁体   English

Android:在图像按钮上有拉伸/缩放问题

[英]Android: Having problems with stretching/scaling on an image button

I have been struggling with what I think is something simple. 我一直在努力解决我认为简单的问题。 I am setting an image on an image button using a class. 我正在使用类在图像按钮上设置图像。 I want the image to fit the constraints of the screen, but without stretching. 我希望图像适合屏幕的约束,但不需要拉伸。 That is, if it's too wide to fit the screen, I want the height to be scaled to accommodate so that it doesn't get stretched . 也就是说,如果它太宽而不适合屏幕,我希望将高度缩放以适应,以便它不会被拉伸 And if the height is too tall, I want the image height to be reduced, thereby shrinking the width too. 如果高度太高,我希望图像高度减小,从而缩小宽度。

I have seen this question asked a couple times and I have tried every bit of the advice, but it makes absolutely no difference for me. 我已经看过几次问这个问题了,我已经尝试了一些建议,但这对我来说绝对没有区别。 I think this may have something to do with usnig an ImageButton instead of an ImageView? 我认为这可能与使用ImageButton而不是ImageView有关吗?

Here are some relevant chunks of code (badly broken) ... 以下是一些相关的代码块(严重破坏)......

In my class: 在我班上:

          ImageView img = (ImageButton) dialog.findViewById(R.id.fullsizeimage);
          img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);  //Changing ScaleType here has no effect
          img.setImageBitmap(useThisBitmap);

And here is the xml layout contents ... 这是xml布局内容......

 <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<ImageButton
    android:id="@+id/fullsizeimage"
    android:background="@null"
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent"
    android:adjustViewBounds="true"
    />

 </RelativeLayout>

Some of that junk in the XML file is probably wrong. XML文件中的一些垃圾可能是错误的。 But I have changed it so many times, and it has ZERO effect. 但是我已经改变了很多次,它有零效果。

Any and all advise is greatly appreciated. 非常感谢任何和所有建议。

I think that ImageButton will always stretch the background image in order to fill its whole background. 我认为ImageButton将始终拉伸背景图像以填充其整个背景。 An ImageView can be easily made clickable on its own, have you tried that? ImageView可以很容易地自己点击,你尝试过吗?

A more complicated option is to use a custom view which can draw your image on canvas any way you like and can have onTouch listener. 一个更复杂的选项是使用自定义视图,它可以以任何您喜欢的方式在画布上绘制图像,并且可以具有onTouch侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM