【问题标题】:When click a button rotate image clockwise in android当点击一个按钮在android中顺时针旋转图像
【发布时间】:2013-08-03 16:17:30
【问题描述】:

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

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

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

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

之后,当我单击按钮时,图像也会旋转。但位置已更改且未全屏显示。

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

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

这是我正在尝试的代码,

ma​​in.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;

        }
    }


}

提前致谢。

【问题讨论】:

    标签: android imageview android-imageview image-rotation


    【解决方案1】:

    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 个角度。你能帮我解决这个问题吗?谢谢。
    • 可以开始动画onClick() of Button
    • 您也可以根据您的要求更改android:toDegrees="90"。谢谢。 :) 如果有用则接受并投票 ;)
    • 嗨,普拉蒂克。谢谢回复。现在方向从横向变为纵向。但我无法停止动画。 iv.stopAnimation(旋转);不管用。那么我该如何阻止这个
    • 这个可以用在按钮上吗?我试图让按钮旋转 180,然后回到 0。虽然我使用了相同的代码 OnClick() ,但它仍然没有反映。
    【解决方案2】:

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

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

    0 -- 90 -- 180 -- 270 -- 360 并返回0

    这里的技巧是用 450 取模,并在完成一个周期时将“从”角度重置为 0(IE,达到 360)

    我在 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的新值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多