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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章