android 仿iphone 密码错误震动效果

package com.hy.testpwd;

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.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends Activity {
	private GridView gridViewPwd;
	private ImageView mImg1;
	private ImageView mImg2;
	private ImageView mImg3;
	private ImageView mImg4;
	private ImageView mImg5;
	private ImageView mImg6;
	private ImageView[] mImages;
	private LoginPwdAdapter loginAdapter;
	private String pwdStr = "";// 密码
	private Animation mShakeAnimation;
	private Animation mAlphaAnimation;

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

	private void init() {

		mImg1 = (ImageView) findViewById(R.id.img1);
		mImg2 = (ImageView) findViewById(R.id.img2);
		mImg3 = (ImageView) findViewById(R.id.img3);
		mImg4 = (ImageView) findViewById(R.id.img4);
		mImg5 = (ImageView) findViewById(R.id.img5);
		mImg6 = (ImageView) findViewById(R.id.img6);

		mShakeAnimation = AnimationUtils.loadAnimation(this, R.anim.shake);
		mAlphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);

		mImages = new ImageView[] { mImg1, mImg2, mImg3, mImg4, mImg5, mImg6 };
		gridViewPwd = (GridView) findViewById(R.id.gridviewPwd);
		loginAdapter = new LoginPwdAdapter(this);
		gridViewPwd.setAdapter(loginAdapter);
		gridViewPwd
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView adapterView,
							View view, int i, long l) {
						// TextView v= (TextView) adapterView.getSelectedItem();
						// String num=v.getText().toString();
						int value = i + 1;
						if (i == 9) {// 取消
							for (int num = 0; num < pwdStr.length(); num++) {
								mImages[num]
										.setImageResource(R.drawable.page_indicator_unfocused);
							}
							pwdStr = "";
						} else if (i == 11) {// 回退
							int len = pwdStr.length();
							if (len > 0) {
								pwdStr = pwdStr.substring(0, len - 1);
								mImages[len - 1]
										.setImageResource(R.drawable.page_indicator_unfocused);
							}
						} else if (i == 10) {
							if (pwdStr.length() < 6) {// 密码为六位
								pwdStr = pwdStr + "0";
								int len = pwdStr.length();
								mImages[len - 1]
										.setImageResource(R.drawable.page_indicator_focused);
							}
						} else {
							if (pwdStr.length() < 6) {// 密码为六位
								pwdStr = pwdStr + value;
								int len = pwdStr.length();
								mImages[len - 1]
										.setImageResource(R.drawable.page_indicator_focused);
							}
						}
						checkPwd(pwdStr);

					}
				});
	}

	//密码为8888888时才是正确的
	private void checkPwd(String pwdStr) {
		if (pwdStr.length() == 6) {
			if ("888888".equals(pwdStr)) {
				alphaView();
			} else {
				shakeView();
			}
		}
	}

	private void shakeView() {
		for (int i = 0; i < mImages.length; i++) {
			mImages[i].setImageResource(R.drawable.page_indicator_unfocused);
			mImages[i].startAnimation(mShakeAnimation);
		}
		pwdStr = "";
	}

	private void alphaView() {
		for (int i = 0; i < mImages.length; i++) {
			mImages[i].startAnimation(mAlphaAnimation);
		}
		pwdStr = "";

		mAlphaAnimation.setAnimationListener(new Animation.AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				/*
				 * Intent intent = new Intent(MainActivity.this,
				 * BaseActivity.class);
				 * intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				 * startActivity(intent); finish();
				 */
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}
		});

	}

}


免费0积分源码下载 地址:http://download.csdn.net/detail/zabio/7274755



package com.hy.testpwd;


import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * Created by Administrator on 2014/4/2.
 */
public class LoginPwdAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private String[] str=new String[]{"1","2","3","4","5","6","7","8","9","取消","0","回退"};

    public LoginPwdAdapter(Context context){
        this.context=context;
        inflater=LayoutInflater.from(this.context);
    }

    @Override
    public int getCount() {
        return str.length;
    }

    @Override
    public Object getItem(int i) {
        return str[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view=inflater.inflate(R.layout.item_list_loginpwd,null);
        TextView btnItem= (TextView) view.findViewById(R.id.text_item_loginpwd);
        btnItem.setText(getItem(i).toString());
        if(i==9||i==11){
            btnItem.setBackgroundColor(Color.parseColor("#cccccc"));
            btnItem.setTextColor(Color.BLACK);
        }
        return view;
    }

}











免费0积分源码下载 地址: http://download.csdn.net/detail/zabio/7274755

你可能感兴趣的:(android)