標籤:
第一種:TranslateAnimation 動畫效果示範:
public void move(View view){//傳統動畫效果TranslateAnimation animation=new TranslateAnimation(0, 500, 0, 0);//時間animation.setDuration(500);//設定移動後的位置不恢複animation.setFillAfter(true);ImageButton img=(ImageButton) findViewById(R.id.img);TextView tv=(TextView) findViewById(R.id.lab);//設定動畫效果 控制項img.startAnimation(animation);tv.startAnimation(animation);Toast.makeText(this, "移動時間", Toast.LENGTH_SHORT).show();}
XML 配置按鈕時間
<Button android:gravity="center" android:layout_marginTop="500sp" android:layout_marginStart="30sp" android:layout_marginLeft="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="移動按鈕" android:id="@+id/btn" android:onClick="move"
使用-------------使用ObjectAnimator----------360
// -------------使用ObjectAnimator----------
ObjectAnimator animator=new ObjectAnimator();
// animator.ofFloat(img, "translationY", 0,100).setDuration(1000).start();
if(flag)
{
animator.ofFloat(img, "Y", 0,300).setDuration(1000).start();
animator.ofFloat(img, "X", 0,300).setDuration(1000).start();
animator.ofFloat(img, "rotation", 0,360).setDuration(1000).start();
flag=false;
}
else
{
animator.ofFloat(img, "Y", 300,0).setDuration(1000).start();
animator.ofFloat(img, "X", 300,0).setDuration(1000).start();
animator.ofFloat(img, "rotation", 3600,0).setDuration(1000).start();
flag=true;
}
PropertyValuesHolder對象的使用
/** * 跟上面不同的是代碼最佳化了 */public void propteValueHolderDemo(){ImageButton img=(ImageButton) findViewById(R.id.img);PropertyValuesHolder pro1=PropertyValuesHolder.ofFloat("rotation", 0,360F);PropertyValuesHolder pro2=PropertyValuesHolder.ofFloat("x", 0,300);PropertyValuesHolder pro3=PropertyValuesHolder.ofFloat("y", 0,300);ObjectAnimator.ofPropertyValuesHolder(img,pro1,pro2,pro3).setDuration(1000).start();}
/** * 按順序 示範動畫效果 */public void PlaySequentiallyDemo(){ImageButton img=(ImageButton) findViewById(R.id.img);ObjectAnimator animator1=ObjectAnimator.ofFloat("img","X",0,360F);ObjectAnimator animator2=ObjectAnimator.ofFloat("img","Y",0,360F);AnimatorSet set=new AnimatorSet();set.playSequentially(animator1,animator2);set.setDuration(1000);set.start();}
android動畫效果示範