Android
 Computer >> コンピューター >  >> プログラミング >> Android

Androidでスムーズな画像回転アニメーションを実装する方法

このチュートリアルでは、Androidアプリで画像をスムーズに回転させるアニメーションの作成方法を、サンプルコードとともにわかりやすく解説します。回転アニメーションはXMLファイルで定義し、ボタンタップをきっかけにImageViewへ適用するシンプルな構成です。

ステップ1:新規プロジェクトの作成

Android Studioを起動し、「File」→「New Project」から新しいプロジェクトを作成します。必要な項目をすべて入力して、プロジェクトのセットアップを完了させてください。

ステップ2:レイアウトファイル(activity_main.xml)の編集

res/layout/activity_main.xmlに以下のコードを追加します。画面中央にImageViewを配置し、その下に回転を開始するためのButtonを設置しています。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <ImageView android:id="@+id/imageView"
      android:layout_width="wrap_content"
      android:layout_height="250dp"
      android:layout_centerInParent="true"
      android:src="@drawable/image"/>
   <Button
      android:id="@+id/btnRotate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/imageView"
      android:layout_marginTop="50dp"
      android:layout_centerInParent="true"
      android:layout_marginBottom="10dp"
      android:text="回転" />
</RelativeLayout>

ステップ3:回転アニメーション用リソースファイルの作成

resフォルダ内に「anim」という名前の新しいフォルダを作成します。続けて、animフォルダを右クリックしてアニメーションリソースファイル「rotate.xml」を作成し、以下のコードを記述してください。ここでは、画像の中心(pivotX/pivotY=50%)を軸に、0度から360度まで10秒(duration=10000ミリ秒)かけて一周回転するアニメーションを定義しています。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
   <rotate android:fromDegrees="0"
      android:toDegrees="360"
      android:pivotX="50%"
      android:pivotY="50%"
      android:duration="10000" />
</set>

ステップ4:MainActivity.javaの編集

src/MainActivity.javaに以下のコードを追加します。ボタンがクリックされると、AnimationUtils.loadAnimation()でrotate.xmlを読み込み、imageView.startAnimation()によってアニメーションを開始します。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
   ImageView imageView;
   Button button;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      imageView = findViewById(R.id.imageView);
      button = findViewById(R.id.btnRotate);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.rotate);
            imageView.startAnimation(animation);
         }
      });
   }
}

ステップ5:AndroidManifest.xmlの確認

androidManifest.xmlは以下のようになります。特別な権限は不要で、通常のアクティビティ宣言だけで動作します。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

アプリの実行

それでは、実際にアプリを起動して動作を確認してみましょう。ここでは、実機のAndroidスマートフォンをパソコンに接続しているものとして説明します。Android Studioからアプリを実行するには、プロジェクト内のいずれかのアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックします。デバイス選択画面で自分のスマートフォンを選択すると、端末にアプリの初期画面が表示されます。

Androidでスムーズな画像回転アニメーションを実装する方法

  1. FacebookでAndroidアプリを作成する方法|開発者サイトでの設定手順を徹底解説

    この記事では、FacebookでAndroidアプリを作成する方法について詳しく解説します。AndroidアプリとFacebookを連携させるには、Facebook開発者サイトでFacebookアプリを作成し、Facebook App IDを取得する必要があります。以下の手順に従って、順番に進めていきましょう。 準備:Facebook開発者サイトで新しいアプリを追加する まず、https://developers.facebook.com/ にアクセスし、「新しいアプリを追加」をクリックしてアプリの作成を開始します。 ステップ1:アプリ名とメールアドレスを入力する 指定されたフィールドに、作

  2. 【Android】ImageViewで画像をアスペクト比を維持したまま拡大縮小して表示する方法

    AndroidでImageViewの画像をアスペクト比を維持したまま拡大縮小する方法 この記事では、AndroidアプリでImageViewに表示した画像を、アスペクト比(縦横比)を維持しながら拡大・縮小して表示する方法を、サンプルコードとともにわかりやすく解説します。 ステップ1:新しいプロジェクトを作成する Android Studioを起動し、メニューから「File」→「New Project」を選択します。プロジェクト名や保存場所など必要な情報を入力して、新規プロジェクトを作成してください。 ステップ2:レイアウトファイルにImageViewを追加する res/layout/acti