在android中的按钮后面的雷效果动画

时间:2015-02-27 09:55:50

标签: android android-animation android-button

有没有办法像Android this javascript-demo一样在Android背后的按钮后面创建光线动画。

它应该在点击Button后旋转,但不应该扩展视图的内容区域(浮动在Button后面)。

rotating ray 那些光线应该在父布局的矩形内旋转。

任何简单的方法吗?

我的代码:

    final LinearLayout.LayoutParams LLParamsCont = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, Utils.dpToPx(getContext(), 50));
    LLParamsCont.setMargins(Utils.dpToPx(getContext(), 5), Utils.dpToPx(getContext(), 5), Utils.dpToPx(getContext(), 5), Utils.dpToPx(getContext(), 5));
    LLParamsCont.gravity = Gravity.CENTER;

    RelativeLayout cont = new RelativeLayout(getContext());
    cont.setLayoutParams(LLParamsCont);

            /* This image view should extend its available space without change the parents dimensions and animate */
    ImageView imageView = new ImageView(getContext());
    imageView.setImageResource(R.drawable.sunray);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    cont.addView(imageView);

            /* Trying it with that code, but the imageview is behind the button and only visible a view pixel
               Increasing the parent will be a mess with other buttons, the rays should just overlap a litle bit all other elements in the back.
               The final rays will be a more alpha transparent ...*/
    Animation rotate = AnimationUtils.loadAnimation(getContext(), R.anim.rotate);
    imageView.startAnimation(rotate);


    Button btn = new Button(getContext());
      btn.setLayoutParams(new RelativeLayout.LayoutParams(
              RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
      btn.setText("Button");

      cont.addView(btn);

1 个答案:

答案 0 :(得分:1)

创建自己的动画: 按照此示例在anim文件夹中创建shake.xml

<?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="1000" 
                android:interpolator="@anim/cycle" />

和anim文件夹中的cycle.xml

<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" 
    android:cycles="4" />

现在在代码上添加动画

Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
anyview.startAnimation(shake);

如果您想要垂直动画,请将Xdelta和Xdelta值更改为从Ydelta到Ydelta值。

查看this教程。他有不同的动画方式

希望它对你有所帮助。