IT_Programming/Android_Java

[android] 애니메이션 - 비밀번호창 좌우로 7번 흔들기

JJun ™ 2011. 8. 1. 01:56



 출처: http://pgm-progger.blogspot.kr/2011/07/android-7.html



이 샘플소스는 안드로이드 설치폴더에 android-sdk-windows/samples/android-8/ApiDemos 위치에 원본소스가 있습니다.

비밀번호 입력창에 비밀번호를 입력하고, 로그인 버튼을 누르면, 비밀번호 입력창을 좌우로 7번 흔듭니다.

패스워드가 안맞을때 이런식으로 처리하면 시각적으로 의미를 전달할 수 있으니, 

효과적으로 의미를 전달할 수 있습니다.

먼저 원하시는 패키지 경로에 Animation1.java 액티비티 클래스 파일을 추가합니다.

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.korsoft.Test016;

// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import net.korsoft.Test016.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

/*
 * 비밀번호 입력창에 비밀번호를 입력하고, 로그인 버튼을 누르면,
 * 비밀번호 입력창을 좌우로 7번 흔듭니다.
 * 패스워드가 안맞을때 이런식으로 처리하면 시각적으로 의미를
 * 전달할 수 있으니, 효과적으로 의미를 전달할 수 있습니다.
 */
public class Animation1 extends Activity implements View.OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation_1);

        View loginButton = findViewById(R.id.login);
        loginButton.setOnClickListener(this);
    }

    public void onClick(View v) {
        // loadAnimation 메소드에 /res/anim/shake.xml 에 정의된 애니메이션,
     // 즉 좌우로 7번 흔들거리도록 정의된 xml 파일대로 애니메이션을 지정합니다.
     // 그리고 패스워드 입력창 객체의  startAnimation 멤소드에,
     // 정의한 애니메이션 객체를 넘겨서, 해당 동작을 하도록 설정하고 있습니다.
     Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
        findViewById(R.id.pw).startAnimation(shake);
    }

}

/res/layout/animation_1.xml 액티비티 레이아웃 xml 파일을 추가합니다.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"
    android:padding="10dip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:text="@string/animation_1_instructions"
    />
    
    <EditText android:id="@+id/pw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:singleLine="true"
        android:password="true"
    />

    <Button android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/googlelogin_login"
    />

</LinearLayout>

/res/values/strings.xml 문자열 리소스 파일을 설정합니다.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Test016Activity!</string>
    <string name="app_name">Test016</string>
    <string name="animation_1_instructions">Please enter your password:</string>
    <string name="googlelogin_login">Login</string>
</resources>

/res/anim/shake.xml 애니메이션 설정 파일을 추가합니다.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="10" android:duration="1000" android:interpolator="@anim/cycle_7" />

/res/anim/cycle_7.xml 애니메이션 설정파일을 설정합니다.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />

AndroidManifest.xml 파일을 설정합니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="net.korsoft.Test016"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        
        <activity android:name=".Animation1" android:label="Views/Animation/Shake">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

그럼 모두 즐프하세요 ^^