当用户名或者密码错误时,输入框会左右震动,来表示“用户名或者密码错误”。同时,通过这个小案例,来初步了解Animation动画。
Animation的XML
在项目的res目录下新建anim文件夹,用来存放Animation动画的XML。
新建shake.xml如下:
[java]
< xml version="1.0" encoding="utf-8" >
android:fromXDelta="0"
android:toXDelta="10"
android:duration="1000"
android:interpolator="@anim/cycle_7" />
其中,fromXDelta表示指定控件在动画开始时水平方向的像素位置,toXDelta表示在水平方向上的位移像素。
相应的,还可以有fromYDelta和toYDelta。
duration表示动画的持续时间。
Animation的应用
[java]
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
findViewById(R.id.editText2).startAnimation(shake);
findViewById(R.id.editText1).startAnimation(shake);
完整的应用
Java文件
[java]
package com.app;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.Toast;
@SuppressLint("NewApi")
public class MyQQActivity extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.qq_login);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
}
public void onClick(View v) {
EditText editText1 = (EditText) findViewById(R.id.editText1);
String text1 = editText1.getText().toString();
EditText editText2 = (EditText) findViewById(R.id.editText2);
String text2 = editText2.getText().toString();
switch (v.getId()) {
case R.id.button1:
if (text1.equals(text2)) {
Intent intent2 = new Intent();
intent2.setClass(MyQQActivity.this,Tabs.class );
startActivity(intent2);
int version = Integer.valueOf(android.os.Build.VERSION.SDK);
if(version >= 5) {
overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
}
}
else {
Toast.makeText(MyQQActivity.this, "账号或密码错误,请重新输入!", Toast.LENGTH_LONG).show();
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
findViewById(R.id.editText2).startAnimation(shake);
findViewById(R.id.editText1).startAnimation(shake);
editText2.setText(null);
}
break;
case R.id.button2:
//注册账号
Uri uri1 = Uri.parse("http://zc.qq.com/chs/index.
html");
Intent it1 = new Intent(Intent.ACTION_VIEW,uri1);
startActivity(it1);
break;
case R.id.button3:
//忘记密码
Uri uri2 = Uri.parse("https://aq.qq.com/cn2/findpsw/pc/pc_find_pwd_input_account");
Intent it2 = new Intent(Intent