アニメーション--ボタン--左右に揺れる
6747 ワード
1 import android.view.animation.Animation; 2 import android.view.animation.Transformation; 3 4 public class CustomAnim extends Animation { 5 6 @Override // 。 7 public void initialize(int width, int height, int parentWidth, 8 int parentHeight) { 9 10 super.initialize(width, height, parentWidth, parentHeight); 11 } 12 13 @Override 14 protected void applyTransformation(float interpolatedTime, Transformation t) { 15 // interpolatedTime 0 1, 1。t 。 16 // System.out.println(interpolatedTime); 17 // t.setAlpha(interpolatedTime); 18 // interpolatedTime 19 // t.getMatrix().setTranslate(200*interpolatedTime, 200*interpolatedTime); 20 // (*20 *50 。) 21 t.getMatrix().setTranslate((float) (Math.sin(interpolatedTime*20)*50), 0); 22 23 super.applyTransformation(interpolatedTime, t); 24 } 25 26 }
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends Activity { private CustomAnim ca; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ca = new CustomAnim(); ca.setDuration(1000); findViewById(R.id.btnAnimMe).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View arg0) { arg0.startAnimation(ca); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }