首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android中按钮背后的光线效果动画

android中按钮背后的光线效果动画
EN

Stack Overflow用户
提问于 2015-02-27 17:55:50
回答 1查看 747关注 0票数 1

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

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

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

有什么简单的方法可以做到吗?

我的代码:

代码语言:javascript
复制
    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);
EN

回答 1

Stack Overflow用户

发布于 2015-02-27 18:03:31

只需创建您自己的动画:按照下面的示例在动画文件夹中创建shake.xml

代码语言:javascript
复制
<?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" />

和cycle.xml在动画文件夹中

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" 
    android:cycles="4" />

现在在您的代码中添加动画

代码语言:javascript
复制
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
anyview.startAnimation(shake);

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

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

希望能对你有所帮助。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28762165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档