Computer >> 컴퓨터 >  >> 프로그래밍 >> Android

안드로이드 앱에서 Marquee(흐르는 텍스트) 효과 구현하는 방법

Marquee 효과는 화면 폭에 담기 어려운 긴 텍스트가 한 줄로 자동으로 흐르며 스크롤되는 애니메이션입니다. 뉴스 티커나 공지사항 배너처럼 좁은 공간에서 긴 문장을 계속 보여줘야 할 때 유용하게 활용됩니다. 이번 글에서는 안드로이드 앱에서 TextView에 Marquee 효과를 적용하는 방법을 단계별로 살펴보겠습니다.

1단계 — 새 프로젝트 생성

Android Studio를 실행한 뒤 File → New Project 메뉴로 이동하고, 새 프로젝트 생성에 필요한 정보를 모두 입력해 프로젝트를 만듭니다.

2단계 — 레이아웃 파일(activity_main.xml) 작성

res/layout/activity_main.xml 파일에 아래 코드를 추가합니다.

<?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">
   <TextView
      android:padding="4dp"
      android:id="@+id/textMarquee"
      android:textSize="24sp"
      android:textStyle="bold|italic"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:singleLine="true"
      android:ellipsize="marquee"
      android:marqueeRepeatLimit="marquee_forever"
      android:scrollHorizontally="true"/>
</RelativeLayout>

Marquee 효과의 핵심은 다음 세 가지 속성입니다.

  • android:singleLine="true" — 텍스트를 한 줄로 제한합니다.
  • android:ellipsize="marquee" — 텍스트가 잘릴 때 marquee 애니메이션이 적용되도록 지정합니다.
  • android:marqueeRepeatLimit="marquee_forever" — 스크롤이 멈추지 않고 무한히 반복되도록 설정합니다.

3단계 — MainActivity.java 작성

src/MainActivity.java 파일에 아래 코드를 추가합니다.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
   TextView textMarquee;
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      textMarquee = findViewById(R.id.textMarquee);
      textMarquee.setText("Be the change that you wish to see in the world, Be yourself; everyone else is already taken.");
      textMarquee.setSelected(true);
   }
}

여기서 가장 중요한 부분은 바로 setSelected(true) 호출입니다. TextView가 선택(selected) 상태가 아니면 marquee 애니메이션이 시작되지 않기 때문에, 이 코드를 반드시 추가해야 텍스트가 흐르기 시작합니다.

4단계 — 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 Studio에서 프로젝트의 액티비티 파일 중 하나를 연 후 툴바의 Run 아이콘을 클릭하고, 목록에서 본인의 모바일 기기를 선택하세요. 그러면 기기 화면에서 텍스트가 끊임없이 흐르는 것을 확인할 수 있습니다.

안드로이드 앱에서 Marquee(흐르는 텍스트) 효과 구현하는 방법

참고 사항

위 예제는 구버전 support library(android.support.v7.app.AppCompatActivity)를 기준으로 작성되었습니다. 최신 Android Studio 프로젝트라면 AndroidX의 androidx.appcompat.app.AppCompatActivity를 사용하면 되며, 나머지 코드는 동일하게 동작합니다. 또한 Kotlin 프로젝트에서는 findViewById 대신 View Binding을 활용하면 더 간결하게 코드를 작성할 수 있습니다.