以下只列出主要代碼:
[java]
private ImageView scanLight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scanLight = (ImageView)findViewById(R.id.img);
Button translateStart = (Button)findViewById(R.id.translateStart);
Button translateEnd = (Button)findViewById(R.id.translateEnd);
Button scaleStart = (Button)findViewById(R.id.scaleStart);
Button scaleEnd = (Button)findViewById(R.id.scaleEnd);
Button alphaStart = (Button)findViewById(R.id.alphaStart);
Button alphaEnd = (Button)findViewById(R.id.alphaEnd);
Button rotateStart = (Button)findViewById(R.id.rotateStart);
Button rotateEnd = (Button)findViewById(R.id.rotateEnd);
translateStart.setOnClickListener(listener);
translateEnd.setOnClickListener(listener);
scaleStart.setOnClickListener(listener);
scaleEnd.setOnClickListener(listener);
alphaStart.setOnClickListener(listener);
alphaEnd.setOnClickListener(listener);
rotateStart.setOnClickListener(listener);
rotateEnd.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener()
{
@Override
public void onClick(View v)
{
switch (v.getId())
{
//平移 setFillAfter(true) 控制執行動畫後定在目前狀態
case R.id.translateStart:
Animation translateIn = new TranslateAnimation(0, 100, 0, 0);
translateIn.setDuration(500);
translateIn.setFillAfter(true);
scanLight.startAnimation(translateIn);
break;
case R.id.translateEnd:
Animation translateOut = new TranslateAnimation(100, 0, 0, 0);
translateOut.setDuration(500);
translateOut.setFillAfter(true);
scanLight.startAnimation(translateOut);
break;
//縮放 後四個參數控制沿自身中心點縮放
case R.id.scaleStart:
Animation sIn = new ScaleAnimation(1f, 2f, 1f, 2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
sIn.setDuration(500);
sIn.setFillAfter(true);
scanLight.startAnimation(sIn);
break;
case R.id.scaleEnd:
Animation sOut = new ScaleAnimation(2f, 1f, 2f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
sOut.setDuration(500);
sOut.setFillAfter(true);
scanLight.startAnimation(sOut);
break;
//透明度
case R.id.alphaStart:
Animation aIn = new AlphaAnimation(1f, 0f);
aIn.setDuration(500);
aIn.setFillAfter(true);
scanLight.startAnimation(aIn);
break;
case R.id.alphaEnd:
Animation aOut = new AlphaAnimation(0f, 1f);
aOut.setDuration(500);
aOut.setFillAfter(true);
scanLight.startAnimation(aOut);
break;
//旋轉 後四個參數控制沿自身中心點旋轉
case R.id.rotateStart:
Animation rIn = new RotateAnimation(0f, +360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rIn.setDuration(500);
rIn.setFillAfter(true);
scanLight.startAnimation(rIn);
break;
case R.id.rotateEnd:
Animation rOut = new RotateAnimation(+360f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rOut.setDuration(500);
rOut.setFillAfter(true);
scanLight.startAnimation(rOut);
break;
default:
break;
}
}
};
private ImageView scanLight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scanLight = (ImageView)findViewById(R.id.img);
Button translateStart = (Button)findViewById(R.id.translateStart);
Button translateEnd = (Button)findViewById(R.id.translateEnd);
Button scaleStart = (Button)findViewById(R.id.scaleStart);
Button scaleEnd = (Button)findViewById(R.id.scaleEnd);
Button alphaStart = (Button)findViewById(R.id.alphaStart);
Button alphaEnd = (Button)findViewById(R.id.alphaEnd);
Button rotateStart = (Button)findViewById(R.id.rotateStart);
Button rotateEnd = (Button)findViewById(R.id.rotateEnd);
translateStart.setOnClickListener(listener);
translateEnd.setOnClickListener(listener);
scaleStart.setOnClickListener(listener);
scaleEnd.setOnClickListener(listener);
alphaStart.setOnClickListener(listener);
alphaEnd.setOnClickListener(listener);
rotateStart.setOnClickListener(listener);
rotateEnd.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener()
{
@Override
public void onClick(View v)
{
switch (v.getId())
{
//平移 setFillAfter(true) 控制執行動畫後定在目前狀態
case R.id.translateStart:
Animation translateIn = new TranslateAnimation(0, 100, 0, 0);
translateIn.setDuration(500);
translateIn.setFillAfter(true);
scanLight.startAnimation(translateIn);
break;
case R.id.translateEnd:
Animation translateOut = new TranslateAnimation(100, 0, 0, 0);
translateOut.setDuration(500);
translateOut.setFillAfter(true);
scanLight.startAnimation(translateOut);
break;
//縮放 後四個參數控制沿自身中心點縮放
case R.id.scaleStart:
Animation sIn = new ScaleAnimation(1f, 2f, 1f, 2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
sIn.setDuration(500);
sIn.setFillAfter(true);
scanLight.startAnimation(sIn);
break;
case R.id.scaleEnd:
Animation sOut = new ScaleAnimation(2f, 1f, 2f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
sOut.setDuration(500);
sOut.setFillAfter(true);
scanLight.startAnimation(sOut);
break;
//透明度
case R.id.alphaStart:
Animation aIn = new AlphaAnimation(1f, 0f);
aIn.setDuration(500);
aIn.setFillAfter(true);
scanLight.startAnimation(aIn);
break;
case R.id.alphaEnd:
Animation aOut = new AlphaAnimation(0f, 1f);
aOut.setDuration(500);
aOut.setFillAfter(true);
scanLight.startAnimation(aOut);
break;
//旋轉 後四個參數控制沿自身中心點旋轉
case R.id.rotateStart:
Animation rIn = new RotateAnimation(0f, +360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rIn.setDuration(500);
rIn.setFillAfter(true);
scanLight.startAnimation(rIn);
break;
case R.id.rotateEnd:
Animation rOut = new RotateAnimation(+360f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rOut.setDuration(500);
rOut.setFillAfter(true);
scanLight.startAnimation(rOut);
break;
default:
break;
}
}
};