Android 动态渐变button

 

先上个图

 

看着特别炫酷吧

其实就是自定义颜色两秒轮播

动画

	AnimationDrawable animationDrawable = (AnimationDrawable) button.getBackground();
//      animationDrawable.setEnterFadeDuration(2000);
        animationDrawable.setExitFadeDuration(2000);
        animationDrawable.start();

xml

long_button_gradient

<animation-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@drawable/long_color_gradient1"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient2"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient3"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient4"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient5"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient6"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient7"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient8"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient9"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient10"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient11"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient12"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient13"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient14"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient15"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient16"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient17"
        android:duration="2000" />

    <item
        android:drawable="@drawable/long_color_gradient18"
        android:duration="2000" />
</animation-list>

long_color_gradient1-long_color_gradientN(渐变一到渐变N)

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="@dimen/button_gradient_fillet" />

    <gradient
        android:angle="45"
        android:centerColor="@color/color_gradient_2"
        android:endColor="@color/color_gradient_3"
        android:startColor="@color/color_gradient_1"
        android:type="linear" />
</shape>

color

    <!--++++++++++++++渐变button+++++++++++++++++-->
    <color name="color_gradient_1">#ffdc6b</color>
    <color name="color_gradient_2">#ffe96b</color>
    <color name="color_gradient_3">#daff6b</color>
    <color name="color_gradient_4">#9fff6b</color>
    <color name="color_gradient_5">#6aff84</color>
    <color name="color_gradient_6">#69ff79</color>
    <color name="color_gradient_7">#6affb5</color>
    <color name="color_gradient_8">#6bffee</color>
    <color name="color_gradient_9">#69cdff</color>
    <color name="color_gradient_X">#6ac6ff</color>

    <color name="color_7FC13F">#7FC13F</color>
    <color name="color_E9E9E9">#e9e9e9</color>
    <color name="color_777777">#777777</color>

dimens

    <dimen name="button_gradient_fillet">7dp</dimen>

that's all!

demo下载地址

备注:有想法的小伙伴写出来的

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Android中的GestureDetector类来检测长按事件,并使用AudioManager类来调整音量。在长按事件开始时,你可以调用AudioManager中的getStreamMaxVolume()方法获取最大音量值,然后在长按事件期间根据时间的流逝逐渐增加音量值,并使用setStreamVolume()方法设置音量。你还可以使用Handler类来处理定期更新音量值的任务。 以下是一个简单的示例代码: ``` button.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); final int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); final int timeInterval = 50; // 定时更新时间间隔 final int volumeStep = maxVolume / (1000 / timeInterval); // 每次增加的音量值 final Handler handler = new Handler(); final Runnable updateVolume = new Runnable() { int currentVolume = 0; @Override public void run() { currentVolume += volumeStep; if (currentVolume > maxVolume) { currentVolume = maxVolume; } audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0); // 第三个参数是可选的,指定音量变化时是否显示系统音量控制UI if (currentVolume < maxVolume) { handler.postDelayed(this, timeInterval); } } }; handler.post(updateVolume); return true; } }); ``` 注意:在使用此代码时,请将其适当地修改以适应您的应用程序需求,并确保在使用音量控制时遵循适当的法规和用户隐私政策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值