How to create basic animations in android :
In this blog i will show you how to create simple shake animation in android.First create a anim folder in res/anim in the android project. Create a xml file in it sopy the following code in it
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="0"
android:interpolator="@anim/cycle_7"
android:toXDelta="10" />
Create another xml file in android cycle_7.xml copy the following code:
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="7" />
Then in the activity in which you want to show the animation copy the following code:
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
public class LoginActivity extends Activity implements AnimationListener {
Animation anim;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Edit Text for username and password
final EditText txtUserName = (EditText) findViewById(R.id.txtUsername);
final EditText txtPassword = (EditText) findViewById(R.id.txtPassword);
// set animation
// to change animation change R.anim.bounce to R.anim.shake
anim = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.bounce);
// login button
Button btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String username = txtUserName.getText().toString();
String password = txtPassword.getText().toString();
try {
if (username.equals("")) {
txtUserName.setError("Username is required");
txtUserName.startAnimation(anim);
}
if (password.equals("")) {
txtPassword.setError("Password is required");
txtPassword.startAnimation(anim);
}
if (password.length() < 6) {
txtPassword
.setError("Password must be 6 character long");
txtPassword.startAnimation(anim);
}
} catch (Exception e) {
Toast.makeText(LoginActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
LoginASYNC login = new LoginASYNC();
login.execute(new String[] { ServerUrl.BASE_URL });
}
});
txtUserName.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
// do the login
System.out.println("send uiser");
Toast.makeText(LoginActivity.this, "login clicked",
Toast.LENGTH_LONG).show();
handled = true;
}
return handled;
}
});
txtPassword.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
// do the login
System.out.println("send pass ");
Toast.makeText(LoginActivity.this, "login clicked",
Toast.LENGTH_LONG).show();
handled = true;
}
return handled;
}
});
TextView forget = (TextView) findViewById(R.id.forget_password);
forget.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(LoginActivity.this,
Forget_Password.class));
}
});
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
Hope the code helps.
0 Comment(s)