【问题标题】:Ray effect animation behind buttons in androidandroid中按钮后面的光线效果动画
【发布时间】:2015-02-27 09:55:49
【问题描述】:

有什么方法可以在 Android 中的按钮后面创建射线动画,例如 this javascript-demo。

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

光线应该在父布局的矩形内旋转。

有什么简单的方法吗?

我的代码:

    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);

【问题讨论】:

标签: android android-animation android-button


【解决方案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);

如果你想要垂直动画,将 fromXdelta 和 toXdelta 值更改为 fromYdelta 和 toYdelta 值。

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

希望对你有帮助。

【讨论】:

  • 谢谢,但我的问题是旋转图像的截断,例如:我在相对布局视图中有一个图像视图,想旋转它,使其居中并截断边框父布局。
  • 我在上面的问题中添加了一张示例图片。
  • 好吧,我想你只想为你拥有的背景/线性布局或图像设置动画。可以把xml分布放到父布局上吗?
  • 在上面添加了我的代码 sn-p。请看一下。对于您的动画示例,图像视图正在旋转,但它不可见(或只是视图像素),因为它无法扩展其父边界。
  • 请发布您的xml代码,这是一个边界问题,所以布局可能没有很好地定义。
猜你喜欢
  • 1970-01-01
  • 2016-12-25
  • 2018-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多