2013-08-02 7 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

も私は、ボタン画像が回転しますクリックしたとき。位置は変更され、フルスクリーンで表示されません。

私はボタンをクリックすると画像が時計回りに回転し、フルスクリーンで表示されます。もう一度私はボタンの画像をクリックして時計を回転させ、フルスクリーンで表示する必要があります。同様にボタンをクリックすると画像が回転する必要があります。

誰かが私を助けることができますか?あなたが私に非常に多くのappriciatedされるサンプルコードまたはリンクを与えることができる場合。ここで

<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> 

main.xmlは、これは私のメインの活動で、私がしようとしているコードである "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(); 

月参考この1:

投票アップ;)

ハッピーコーディング:)返信用:)

+0

こんにちはPratikは、感謝を回転してください。アニメーションは完璧に動作しますが、私の要求は少し異なります。ボタンをクリックすると、画像を90度回転させる必要があります。これに私を助けてくれますか?ありがとう。 –

+0

'Button'の' onClick() 'アニメーションを開始することができます –

+0

あなたの要件に従って' android:toDegrees = "90" 'を変更することもできます。おかげさまで :)有用であれば同意して投票してください;) –

関連する問題