在开发中,我们可能会对一张图片加上渐变效果,这可以通过在ImageView中添加Filter来实现。
首先定义一个color数组,在arrays.xml中添加如下代码:
-
<array name="filter_colors">
-
<item>#96eeeeee</item>
-
<item>#86eeeeee</item>
-
<item>#76eeeeee</item>
-
<item>#56eeeeee</item>
-
<item>#46eeeeee</item>
-
<item>#36eeeeee</item>
-
<item>#26eeeeee</item>
-
<item>#16eeeeee</item>
-
<item>#06eeeeee</item>
-
</array>
然后定义了一个Handler,在Handler中对ImageView 添加filter就可以了:
-
Handler mHandler = new Handler() {
-
public void handleMessage(Message msg) {
-
switch (msg.what) {
-
case 0:
-
mHandler.sendEmptyMessageDelayed(0,
-
1000);
-
if(mProgress%10==1){
-
int index=mProgress/10;
-
int color = mFilterColors.getColor(index,0);
-
PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP);
-
mImg.setColorFilter(colorFilter);
-
mProgress++;
-
}
-
}
-
}
-
}
阅读(7569) | 评论(0) | 转发(0) |