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;
    }

}

<?xml version="1.0" encoding="utf-8"?>
<alpha
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1.0"
    android:toAlpha="0.3"
    android:repeatCount="1"
    android:duration="500">
</alpha>

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:toXDelta="5"
    android:duration="500"
    android:interpolator="@anim/cycle_7" />

<?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" />

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值