Thứ Tư, 11 tháng 9, 2013

Android Shake animation

http://www.androiddevelopersolution.com/2013/01/android-shake-animation.html

Android Shake animation

Hello Droid Friends,

This is my another android blog in which I am going to explain the Animation
effect(shake Animation ) in android. When you fill wrong input then instead of
showing the toast messege you can also shake the view.

shake animation

Steps :
1. Create a project with name "Shake animation" and package name
    "com.mukesh.shakeanimation".
2. add an "anim" folder inside the res folder.see the image below.

android shake animation
 
Source Code:

1. Activity Class:




package com.mukesh.shakeanimation;

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

public class MainActivity extends Activity implements View.OnClickListener {

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

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

 public void onClick(View v) {
  Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
     findViewById(R.id.pw).startAnimation(shake);
     Toast.makeText(this, "Wrong Password", Toast.LENGTH_SHORT).show();
 }

}


2) activity_main.xml




<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="@drawable/blue_gradient"

    android:orientation="vertical"

    android:padding="10dip" >



    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_marginBottom="10dip"

        android:text="Please enter your password:"

        android:textColor="@android:color/white"

        android:textStyle="bold" />



    <EditText

        android:id="@+id/pw"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:clickable="true"

        android:password="true"

        android:singleLine="true" />



    <Button

        android:id="@+id/login"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:background="@drawable/btn_active_pressed"

        android:padding="5dp"

        android:text="Login"

        android:textColor="@android:color/white"

        android:textStyle="bold" />



</LinearLayout>

3. res/anim/shake.xml

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

4. res/anim/cycle_7.xml

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



May this will helps someone.
Enjoy coding  :)

Không có nhận xét nào:

Đăng nhận xét