標籤:android抖動動畫
給按鈕做抖動效果,可以這樣做,建立anim檔案夾在res下面,建立一個button_shake.xml
<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="120" android:fromDegrees="-3" android:pivotX="100%" android:pivotY="100%" android:repeatCount="infinite" android:repeatMode="reverse" android:toDegrees="3" />
在代碼裡載入:
final ImageButton button = (ImageButton) findViewById(R.id.btn);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Animation shake = AnimationUtils.loadAnimation(AnimationTest.this, R.anim.button_shake);shake.reset();shake.setFillAfter(true);button.startAnimation(shake);}});
給EditText做一個橫向抖動的效果:
這樣寫anim的檔案:
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="10" android:duration="1300" android:interpolator="@anim/cycle" />
cycle.xml主要描述動畫的加速器:
<?xml version="1.0" encoding="utf-8"?><!-- 動畫從開始到結束,變動率是迴圈給定次數的正弦曲線。 --><cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="20" />
代碼可以這樣載入:
final Button confirm = (Button) findViewById(R.id.btn_confirm);confirm.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if(custom_edittext.getText().toString().equals("jake")){Toast.makeText(AnimationTest.this, "welcome", Toast.LENGTH_LONG).show();}else{Animation shake = AnimationUtils.loadAnimation(AnimationTest.this, R.anim.shake_x);custom_edittext.startAnimation(shake);}}});
代碼可以在http://download.csdn.net/detail/baidu_nod/7616277下載