2013-08-02 142 views
7

中顺时针旋转图像我有一个要求,我有一个ImageView和一个按钮。当我点击一个按钮在android

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

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

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

之后还当我点击该按钮,图像便会旋转。但是位置被改变并且不全屏显示。

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

因此有人可以帮助我吗?如果你能给我一个示例代码或一个非常感兴趣的链接。

这里是我想的代码,

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; 

     } 
    } 


} 

谢谢提前。

回答

21

尼斯

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(); 

五月有用的这一个:

投票最多;)

编码快乐:)继续转动:)

+0

嗨PRATIK,感谢您的答复。动画效果很好,但我的要求稍有不同。当我点击按钮时,我需要将图像旋转90度。你能帮我解决这个问题吗?谢谢。 –

+0

您可以开始动画'Button'的onClick()' –

+0

您也可以按照您的要求更改'android:toDegrees =“90”''。 谢谢。 :)接受如果有用和做投票;) –

相关问题