大佬教程收集整理的这篇文章主要介绍了在android上渐变按钮颜色变化,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何改善这个?
public class ActivityExample extends Activity { private changeColorBtn; @Override protected void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setContentView(R.layout.activity_animations); changeColorBtn = (Button) findViewById(R.id.btn_change_color); changeColorBtn.setBACkgroundColor(Color.bLACK); changeColorBtn.setOnClickListener(new View.onClickListener() { @Override public void onClick(View v) { changeButtonColor(v); } }); } private void changeButtonColor(View v) { // How many intermediate color will be,and delay in millisecond between them int count = 20,delay = 100; for (int i = 0; i < count; i++) { try { int color = ((ColorDrawablE) changeColorBtn.getBACkground()) .getColor(); int blue = Color.blue(color),red = Color.red(color),green = Color.green(color); changeColorBtn.setBACkgroundColor(Color.rgb(red+10,green+5,blue+3)); Thread.sleep(delay); } catch (InterruptedException inE) { } } } @Override protected void onSaveInstanceState(Bundle outStatE) { super.onSaveInstanceState(outStatE); } @Override public Boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main,menu); return true; }
}
>在drawable文件夹中创建一个xml文件,并写入如下内容:
`
<?xml version="1.0" encoding="UTF-8"?> <transition xmlns:android="http://scheR_734_11845@as.android.com/apk/res/android"> <item android:drawable="@color/color1" /> <item android:drawable="@color/color2" /> </transition>
`
>然后,在你的xml for this button(或其他元素/ View)中你应该在Android:BACkground属性中引用这个TransitionDrawable.@H_675_28@>此外,您应该将颜色存储为资源:为此,您必须创建如下的xml:
`
<?xml version="1.0" encoding="UTF-8"?> <resources> <color name="color1">#990000</color> <color name="color2">#cc3311</color> </resources>
`
并将此xml文件保存在/ res / values /文件夹中,将xml命名为color.xml.
>并启动代码转换:
`
int durationMillis = 2000; TransitionDrawable transition = (TransitionDrawablE) changeColorBtn.getBACkground(); transition.startTransition(durationMillis);
`
这对我有所帮助,我希望它对其他人有用.
以上是大佬教程为你收集整理的在android上渐变按钮颜色变化全部内容,希望文章能够帮你解决在android上渐变按钮颜色变化所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。