一般Table的数据(行)更新时,要配合一定的动画效果,才能引起用户注意。
在Android里,颜色值用8位的16进制来表示,其中前两2位用来表示 alpha channel, 后6位用来表示rgb. 在程序里面,只要在指定时间内,将alpha channel从 0调整到 0xff,就实现了 highlight TableRow.
class ColorRefreshTask extends TimerTask {
final static int DELAY_ONCE =200;
final static int TOTAL_RUNTIME = 3000;
final static int POWER_16_16 = 16 * 16* 16 * 16 * 16 * 16;
final static int INCREASE_ONCE = 0xff / (TOTAL_RUNTIME / DELAY_ONCE);
int color;
int id;
int startTime;
int alphaChannel;
/**
*
* @param color (TableRow's current background color)
* @param id TableRow's id(user id)
*/
public ColorRefreshTask(int color, int id) {
super();
Log.d("color", "ready to set color!");
this.color = color;
this.id = id;
this.startTime = 0;
this.alphaChannel = 0;
}
public void run(){
int colorComm = color -0xff000000; //RGB color value;
int currColor = color;
if(startTime < TOTAL_RUNTIME) {
startTime += DELAY_ONCE;
alphaChannel += INCREASE_ONCE;
currColor = POWER_16_16 * alphaChannel + colorComm;
Log.d("color", Integer.toHexString(currColor));
sendMsg(currColor);
messageHandler.postDelayed(this,DELAY_ONCE);
}
else {
sendMsg(currColor);
}
}
public void startTimer(){
messageHandler.postDelayed(this,DELAY_ONCE);
}
private void sendMsg(int currColor){
Message message = messageHandler.obtainMessage();
message.what = MESSAGE_HANDLE_ID_UPDATEROW_BACKGROUND;
Bundle bundle = new Bundle();
bundle.putInt(MESSAGE_KEY_ID, id);
bundle.putInt(MESSAGE_KEY_COLOR, currColor);
message.setData(bundle);
messageHandler.sendMessage(message);
}
public void stopTimer(){
this.cancel();
}
}
private static final int MESSAGE_HANDLE_ID = 800;
private static final int MESSAGE_HANDLE_ID_UPDATEROW_DATA = MESSAGE_HANDLE_ID + 1;
private static final int MESSAGE_HANDLE_ID_UPDATEROW_BACKGROUND = MESSAGE_HANDLE_ID +2;
private Handler messageHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case MESSAGE_HANDLE_ID_UPDATEROW_DATA:{
int id = msg.getData().getInt(MESSAGE_KEY_ID);
String fields[] = new String[5];
fields[0] = msg.getData().getString("fields1");
fields[1] = msg.getData().getString("fields2");
fields[2] = msg.getData().getString("fields3");
fields[3] = msg.getData().getString("fields4");
fields[4] = msg.getData().getString("fields5");
updateRowInTable(id, fields);
break;
}
case MESSAGE_HANDLE_ID_UPDATEROW_BACKGROUND:
{
int id = msg.getData().getInt(MESSAGE_KEY_ID);
int color = msg.getData().getInt(MESSAGE_KEY_COLOR);
TableRow tableRow= (TableRow)findViewById( id);
if(tableRow != null) {
int count = tableRow.getChildCount();
for(int index = 0; index < count; index++) {
TextView child = (TextView)(tableRow.getChildAt(index));
if(child != null) {
child.setBackgroundColor(color);
}
}
}
break;
}
default:
}
}
};
private final void updateRowInTable(int code,String[] otherFields) {
TableRow tableRow= (TableRow)findViewById(code);
if(tableRow == null) {
Log.d("tablerow", "can't find row in the table!");
return;
}
int bgColorEnd = (otherFields[2].indexOf("-") >= 0) ? 0xff2e8b57 : 0xffb22222;
for(int index = 0; index < otherFields.length; index++) {
int identity = code * 13 +(index+1);
TextView textView= (findViewById(identity)!= null) ? (TextView) findViewById(identity) : new TextView(this);
if(otherFields[index].indexOf('-') >=0) {
textView.setTextColor(Color.GREEN);
}
else if( index == 2) {
textView.setTextColor(Color.RED);
}
else {
textView.setTextColor(Color.WHITE);
}
textView.setText(otherFields[index]);
Log.d("tablerow", "updated id:" + (code) + ", value=" + otherFields[index] );
if(findViewById(identity) == null){
textView.setId(identity);
tableRow.addView(textView);
Log.d("tablerow", "add new view!");
}
else {
Log.d("tablerow", "upate view!");
}
textView.postInvalidate();
//textView.invalidate();
}
tableRow.postInvalidate();
//tableRow.invalidate();
ColorRefreshTask refresh = new ColorRefreshTask(bgColorEnd,code);
messageHandler.postDelayed(refresh, 50);
}