[英]When click a button rotate image clockwise in android
I have a requirement where I have an ImageView and a button.我有一个要求,我有一个 ImageView 和一个按钮。
http://i1289.photobucket.com/albums/b509/salikclub/Rotate-Last-Start_zps0d2fced8.png http://i1289.photobucket.com/albums/b509/salikclub/Rotate-Last-Start_zps0d2fced8.png
I want to rotate the image when I click the button.我想在单击按钮时旋转图像。 I need the image with full screen.
我需要全屏图像。 but when I click the button image will be rotate, but not shown in the full screen.
但是当我单击按钮时,图像会旋转,但不会全屏显示。 Please see the below link.
请参阅以下链接。
http://i1289.photobucket.com/albums/b509/salikclub/Rotate-Last1_zps2ccb1326.png http://i1289.photobucket.com/albums/b509/salikclub/Rotate-Last1_zps2ccb1326.png
After that also when I clicked the button image will rotate.在那之后,当我点击按钮图像也会旋转。 but positions is changed and not shown in full screen.
但位置已更改且未全屏显示。
My requrement is, when I click the button image will be roatate clockwise and will show in full screen.我的要求是,当我单击按钮时,图像将顺时针旋转并全屏显示。 Again I click the button image must be rotate clock wise and show in full screen.
我再次单击按钮图像必须顺时针旋转并全屏显示。 Likewise when I click the button image must be rotate.
同样,当我单击按钮图像时必须旋转。
Therefore can someone help me?因此,有人可以帮助我吗? If you can give me a sample code or a link that will be very much appriciated.
如果你能给我一个示例代码或一个非常有用的链接。
Here is the code which I'm trying,这是我正在尝试的代码,
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>
This is my main activity "MainActivity.java"这是我的主要活动“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;
}
}
}
Thanks in advance.提前致谢。
Create button_rotate.xml
in anim
folder: 在
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" />
Now create animation in Java file: 现在在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);
For stop animation: 对于停止动画:
iv.clearAnimation();
If I understood correctly — you want to rotate the ImageView
90 degrees clockwise, on a button click .如果我理解正确的话——你想将
ImageView
顺时针旋转 90 度,点击按钮。
If you look at the possible rotation angles for an ImageView
to be in, there are actually 5 states:如果查看
ImageView
可能的旋转角度,实际上有 5 种状态:
0 -- 90 -- 180 -- 270 -- 360
and back to 0
0 -- 90 -- 180 -- 270 -- 360
然后回到0
The trick here is to take modulo with 450 and reset 'from' angle to 0
when finishing one cycle (IE, reached 360)这里的技巧是对 450 取模,并在完成一个循环后将“从”角度重置为
0
(即,达到 360)
I created this extension function in Kotlin on an ImageView
that could be used to perform clockwise / anti-clockwise rotation using RotateAnimation
我在 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
}
Call this method in button's onClickListener as follows:在按钮的 onClickListener 中调用这个方法如下:
button.setOnClickListener {
currentRotation = imageView.animatedRotate(currentRotation)
}
Note how toRotation
is returned in the end which becomes the new value for currentRotation
注意最后如何返回
toRotation
,它成为currentRotation
的新值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.