When click a button rotate image clockwise in android(当点击一个按钮在android中顺时针旋转图像)
问题描述
I have a requirement where I have an ImageView and a button.
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
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"
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:
<?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:
/* 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();
这篇关于当点击一个按钮在android中顺时针旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当点击一个按钮在android中顺时针旋转图像
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01