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

Androidで画像をスムーズに回転させる方法【RotateAnimation活用ガイド】

本記事では、Androidアプリで画像をスムーズに回転させる方法を、実際のコード例とともにわかりやすく解説します。Android SDKに組み込まれているRotateAnimationクラスを使えば、複雑な処理を書くことなく簡単に滑らかな回転アニメーションを実装できます。

実装手順

ステップ1:新規プロジェクトを作成する

まず、Android Studioで新しいプロジェクトを作成します。メニューから「File」→「New Project」を選択し、必要な項目をすべて入力してプロジェクトを作成してください。

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

次に、res/layout/activity_main.xmlに以下のコードを追加します。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   android:id = "@+id/parent"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:gravity = "center"
   android:orientation = "vertical">
   <TextView
      android:id = "@+id/text"
      android:textSize = "18sp"
      android:textAlignment = "center"
      android:text = "rorate image"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content" />
   <ImageView
      android:id = "@+id/rotateImage"
      android:layout_width = "match_parent"
      android:layout_marginTop = "10dp"
      android:layout_height = "wrap_content"
      android:src = "@mipmap/ic_launcher"
      android:layerType = "software" />
</LinearLayout>

このレイアウトでは、TextViewImageViewを縦方向に配置しています。テキストビューをタップすると、画像が反時計回りに回転する仕組みです。

ステップ3:MainActivity.javaを実装する

続いて、src/MainActivity.javaに以下のコードを追加します。

package com.example.andy.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   int view = R.layout.activity_main;
   TextView text;
   ImageView image;
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(view);
      text = findViewById(R.id.text);
      image = findViewById(R.id.rotateImage);
      text.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            RotateAnimation rotate = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f,          Animation.RELATIVE_TO_SELF, 0.5f);
            rotate.setDuration(5000);
            rotate.setInterpolator(new LinearInterpolator());
            image.startAnimation(rotate);
         }
      });
}
}

コードのポイント解説

ここでは、Androidにあらかじめ用意されているRotateAnimationクラスを使用しています。画像を回転させるためのコア部分は以下の通りです。

RotateAnimation rotate = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(5000);
rotate.setInterpolator(new LinearInterpolator());
image.startAnimation(rotate);

それぞれの設定内容は次のとおりです。

  • RotaateAnimationの引数: 「開始角度(0度)」「終了角度(180度)」に加え、回転の中心点を指定しています。RELATIVE_TO_SELF, 0.5fとすることで、画像自身の中央を軸に回転します。
  • setDuration(5000): アニメーションの所要時間を5000ミリ秒(5秒)に設定しています。
  • LinearInterpolator: 補間処理に線形補間を指定することで、速度に緩急のない滑らかで一定の回転を実現します。

動作確認

それでは、アプリを実行してみましょう。実機のAndroidスマートフォンをPCに接続していることを前提としています。Android Studioからアプリを起動するには、プロジェクト内のアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックします。接続したモバイルデバイスを選択すると、端末に以下のような初期画面が表示されます。

Androidで画像をスムーズに回転させる方法【RotateAnimation活用ガイド】

ユーザーがテキストビューをクリックすると、画像が次のように180度まで滑らかに回転します。

Androidで画像をスムーズに回転させる方法【RotateAnimation活用ガイド】

まとめ

RotateAnimationクラスを使えば、数行のコードだけで画像の回転アニメーションを実装できます。durationやinterpolatorの値を調整すれば、より自然な演出も可能なので、ローディング表示やボタンの装飾など、さまざまな場面で活用してみてください。

  1. HTMLで画像をレスポンシブ対応にする方法【初心者向け解説】

    Webページをスマートフォンやタブレットなど、さまざまな画面サイズに対応させるには、画像をレスポンシブ対応にすることが重要です。HTMLで画像をレスポンシブにするには、CSSで2つのプロパティを設定するだけで実現できます。レスポンシブ画像の基本:2つのプロパティまず、<img>タグを使って画像を配置します。そのうえで、以下の2つのプロパティを指定しましょう。height: auto; … 画像の高さを幅に合わせて自動的に調整します。max-width: 100%; … 画像が親要素(画面幅)より大きくならないよう制限します。具体的には、次のようにインラインスタイルとして記述します。

  2. Androidスマホで画像をクリップボードにコピー&ペーストする方法【アプリ別の共有テクニックも解説】

    コピーアンドペーストは、パソコンやスマートフォンで最も頻繁に使われる機能のひとつです。同じ内容を何度も入力する手間を省き、作業効率を大幅に向上させてくれます。パソコンであれば、テキストはもちろん、画像、動画、音声ファイル、ドキュメントなど、ほぼすべてのデータを簡単にコピー&ペーストできます。 近年、スマートフォンは高性能化が進み、パソコンでできることのほとんどをこなせるようになりました。その結果、日常的な作業をスマホで行う人が増え続けています。だからこそ、コピーアンドペースト機能においてパソコンとの格差があるのは不公平ですよね。 そこで嬉しいお知らせです。Androidスマートフォンでも、画像