Android 省略號加載動畫效果的實現思路

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);
        }
    }
}
發佈了61 篇原創文章 · 獲贊 50 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章