android - 将 ImageView 缩放到全屏

标签 android android-layout android-imageview gesture pinchzoom

我有一个 xml。其中有一个相对布局。在该布局上有一些 TextFields。在相对布局的中间有一个 ImageView。我想捏缩放 imageView,以便它甚至可以缩放到全屏。现在我只能在 ImageView 内部进行缩放。就像 ImageView 是一个屏幕,缩放只发生在它里面。但我想相对于整个屏幕进行缩放。当图像被缩放时,我希望 TextFields 保持在那里的位置,但当图像被缩放时它们应该被覆盖。有什么办法吗?谢谢。

最佳答案

在 res 的 anim 文件夹中创建 zoom_in.xml 并编写以下代码

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >

<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="1"
    android:fromYScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="3"
    android:toYScale="3" >
</scale>

在你的代码中写下下面的代码

public class ZoomInActivity extends Activity implements AnimationListener {

ImageView imgPoster;
Button btnStart;

// Animation
Animation animZoomIn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_zoom_in);

    imgPoster = (ImageView) findViewById(R.id.imgLogo);
    btnStart = (Button) findViewById(R.id.btnStart);

    // load the animation
    animZoomIn = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.zoom_in);

    // set animation listener
    animZoomIn.setAnimationListener(this);

    // button click event
    btnStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // start the animation
            imgPoster.startAnimation(animZoomIn);
        }
    });

}

@Override
public void onAnimationEnd(Animation animation) {
    // Take any action after completing the animation

    // check for zoom in animation
    if (animation == animZoomIn) {          
    }

}

@Override
public void onAnimationRepeat(Animation animation) {
    // TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation animation) {
    // TODO Auto-generated method stub

}

}

而你的 activity_zoom_in.xml 是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center">

<ImageView android:id="@+id/imgLogo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/man_of_steel"
    android:layout_centerInParent="true"/>

<Button android:id="@+id/btnStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start Animation"
    android:layout_marginTop="30dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="20dp"/>

</RelativeLayout>

关于android - 将 ImageView 缩放到全屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18201949/

相关文章:

带有 SSO 的 Android-facebookSDK

java - android.view.InflateException : Binary XML file line #2: Error inflating class <unknown>?

android-layout - 由于某种奇怪的原因,按钮被转换到复选框?

android - ImageView 额外填充问题

android - 从服务器检索图像的可能方法

android - 将 JSON 解析为 ListView 友好的输出

android - WiFi 状态变化

android - ImageView 缩放 : fix height variable width with maxWidth

java - 如何在点击默认键盘按钮时启用触觉?

Android:创建一个不失真缩放的正方形网格