Android 通过Animate硬编码实现简单的平移、旋转、缩放及透明度动画过程
以下只列出主要代码:
[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(aO
补充:移动开发 , Android ,