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

Android 省略号加载动画效果的实现思路

2017-10-18 16:18 597 查看
public class PointWaitBar extends LinearLayout {
private static final int NUM = 5;
private Context context;
private String TAG = "PointWaitBar";
private ImageView mOldIM;
private UpdateHandler handler;

public PointWaitBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
init();
}

public PointWaitBar(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}

public PointWaitBar(Context context) {
super(context);
this.context = context;
init();
}

private void init() {
//初始化数据
this.setOrientation(LinearLayout.HORIZONTAL);
this.setGravity(Gravity.CENTER);
handler = new UpdateHandler(context);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.point_waitingbar_black);
LinearLayout.LayoutParams tLayoutParams = new LinearLayout.LayoutParams(bitmap.getWidth(), bitmap.getHeight());
tLayoutParams.leftMargin = 10;
tLayoutParams.rightMargin = 10;
//添加5个小点省略号
for (int i = 0; i < NUM; i++) {
ImageView vDot = new ImageView(context);
vDot.setLayoutParams(tLayoutParams);
if (i == 0) {
vDot.setBackgroundResource(R.drawable.point_waitingbar_white);
} else {
vDot.setBackgroundResource(R.drawable.point_waitingbar_black);
}
this.addView(vDot);
}
mOldIM = (ImageView) this.getChildAt(0);
handler.sendEmptyMessage(0);
}

//提供给外部消除message
public void setDestroyCallBack() {
if (handler != null) {
handler.removeCallbacksAndMessages(null);
LogUtil.i(TAG, "已经清除消息");
}
}

class UpdateHandler extends Handler {
WeakReference<Context> reference;

public UpdateHandler(Context context) {
reference = new WeakReference<Context>(context);
}

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
int cPosition = msg.what;
if (mOldIM != null)
mOldIM.setBackgroundResource(R.drawable.point_waitingbar_black);
ImageView currentIM = (ImageView) PointWaitBar.this.getChildAt(cPosition);
currentIM.setBackgroundResource(R.drawable.point_waitingbar_white);
mOldIM = currentIM;
if (++cPosition == NUM)
cPosition = 0;
this.sendEmptyMessageDelayed(cPosition, 200);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android