2013-08-02 140 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」''。 謝謝。 :)接受如果有用和做投票;) –

相關問題