您的位置:首页 > 移动开发 > Android开发

解决android2.3版本下设置控件透明度的问题

2015-06-24 11:14 351 查看
    开发了一段时间4.0的app,突然老板说要兼容2.3。。。

  好吧,将版本转为2.3.3,发现原来直接的setAlpha()的函数没了,百度了一下就只有getBackground().setAlpha()......怒摔,这能一样吗,而且要是这个控件没有设置background还会愤怒地报空指针错误。。而且在布局文件上面也没了‘android:alpha=“” ’的方法了。。一时半会想不到什么特别好的取代方法,于是就决定用动画效果来代替——alphaanimation。下面贴代码,还是非常简单的。

<pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; line-height: 21.6000003814697px; font-family: 'Courier New' !important;">Animation a=<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span> AlphaAnimation(250, 0);<span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.5 !important;">透明度从250渐变至0</span>
a.setFillAfter(<span style="margin: 0px; padding: 0px; color: rgb(0, 0, 255); line-height: 1.5 !important;">true</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">);          <span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.5 !important;">将控件设置为动画的最后状态,也就是变为透明</span>
mDrawer.startAnimation(a);</span>


这样一来就可以实现将控件变为透明了,当然了如果完全不想要效果的话,可以调用该语句
<pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; line-height: 21.6000003814697px; font-family: 'Courier New' !important;">a.setDuration(100);<span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="margin: 0px; padding: 0px; color: rgb(0, 128, 0); line-height: 1.5 !important;">根据需求设置动画的时长</span>



虽然感觉实用性不是很大,不过感觉还算是不错的一个解决android 2.3版本下无法直接设置控件透明的问题的方法~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: