在Android中点击按钮时如何顺时针旋转图像

7

我有一个要求,我有一个ImageView和一个按钮。

http://i1289.photobucket.com/albums/b509/salikclub/Rotate-Last-Start_zps0d2fced8.png

当我点击按钮时,我想旋转图像。我需要全屏显示图像,但是当我点击按钮时,图像会旋转,但不会在全屏状态下显示。请参见下面的链接。

http://i1289.photobucket.com/albums/b509/salikclub/Rotate-Last1_zps2ccb1326.png

即使在此之后,当我点击按钮时,图像仍将旋转。但位置已更改,并且未以全屏状态显示。

我的要求是,当我点击按钮时,图像将顺时针旋转并在全屏状态下显示。再次点击按钮,图像必须顺时针旋转并显示在全屏状态下。同样,当我点击按钮时,图像必须旋转。

因此,可以有人帮助我吗?如果您可以给我示例代码或链接,那将非常感激。

这是我正在尝试的代码,

main.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:scaleType="fitXY"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true"
        android:src="@drawable/matterhorn"
        />

    <Button
        android:id="@+id/btnRotate"
        android:layout_width="65dp"
        android:layout_height="35dp"
        android:layout_gravity="bottom|left"
        android:layout_marginLeft="190dp"
        android:layout_marginBottom="15dp"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:drawableLeft="@drawable/btn_icon_rotate"
        >
    </Button>

</merge>

这是我的主活动 "MainActivity.java"

package com.imageview.rotate;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Matrix;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class MainActivity extends Activity implements OnClickListener{

    private Button btnRotate;
    private ImageView imgview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imgview = (ImageView) findViewById(R.id.imgView);

        btnRotate = (Button) findViewById(R.id.btnRotate);
        btnRotate.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.btnRotate:
            Matrix matrix=new Matrix();
            imgview.setScaleType(ScaleType.MATRIX);   //required
            matrix.postRotate((float) 180f, imgview.getDrawable().getBounds().width()/2, imgview.getDrawable().getBounds().height()/2);
            imgview.setImageMatrix(matrix);


            break;

        }
    }


}

感谢提前阅读。
谢谢。
2个回答

23

anim 文件夹中创建 button_rotate.xml

<?xml version="1.0" encoding="utf-8"?>

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromDegrees="0"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360" />

现在在Java文件中创建动画:

/* Get ImageView Object */
ImageView iv = (ImageView) view.findViewById(R.id.refresh_action_view);

/* Create Animation */
Animation rotation = AnimationUtils.loadAnimation(context, R.anim.refresh_button_anim);
rotation.setRepeatCount(Animation.INFINITE);

/* start Animation */
iv.startAnimation(rotation);

停止动画:

iv.clearAnimation();

嗨,Pratik,感谢您的回复。动画效果非常完美,但我的需求略有不同。当我点击按钮时,我需要将图像旋转90度。您能帮助我吗?谢谢。 - Ahamed Salik
你可以在 ButtononClick() 中启动动画。 - Pratik Butani
你也可以根据你的需求更改 android:toDegrees="90"。谢谢 :) 如果有用请接受并点赞 ;) - Pratik Butani
嗨,Pratik。感谢您的回复。现在方向从横屏变为竖屏了。但是我无法停止动画。iv.stopAnimation(rotation); 不起作用。那么我该如何停止它呢? - Ahamed Salik
这个可以用在按钮上吗?我想让按钮旋转180度,然后再回到0度。虽然我使用了相同的OnClick()代码,但它仍然没有反应。 - Milee
我有一个ImageView,当点击按钮时可以旋转360度,在动画完成后停止,再次点击后重复动画。你可以帮我吗? - Kevan Aghera

0

如果我理解正确,您想在按钮点击时将ImageView顺时针旋转90度。

如果您查看ImageView可能的旋转角度,实际上有5种状态:

0 -- 90 -- 180 -- 270 -- 360 ,然后回到0

这里的诀窍是使用450取模,并在完成一个周期(即达到360)时将“起始”角度重置为0

我创建了此Kotlin扩展函数,可在ImageView上使用RotateAnimation执行顺时针/逆时针旋转

var currentRotation = 0f

...

fun ImageView.animatedRotate(currentRotation: Float, clockwise: Boolean = true): Float {
    // 0f here is configurable, you can restart the rotation cycle
    // with your choice of initial angle
    val fromRotation = if (currentRotation.absoluteValue == 360f) 0f else currentRotation
    val rotateDegrees = if (clockwise) 90f else -90f
    val toRotation = (fromRotation + rotateDegrees) % 450f
    Timber.d("Rotating from $fromRotation to $toRotation")
    val rotateAnimation = RotateAnimation(
        fromRotation,
        toRotation,
        width / 2f,
        height / 2f
    ).apply {
        duration = 400 // configurable
        fillAfter = true
    }
    startAnimation(rotateAnimation)
    return toRotation
}

在按钮的 onClickListener 中调用此方法,如下所示:

button.setOnClickListener {
    currentRotation = imageView.animatedRotate(currentRotation)
}

请注意,在最后返回了toRotation,这成为了currentRotation的新值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接