| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
9年前发布

Android最省内存的ViewPager添加小圆点指示器

这个你可以用View叠加来实现,但是这个效果不太理想,1占内存,而移植起来不方便,需要找各种资源,所以采用自己绘制更好,方便,而且效率高。

    import java.util.ArrayList;    import java.util.List;    import java.util.Map;        import android.content.Context;    import android.graphics.Canvas;    import android.graphics.Paint;    import android.graphics.Paint.Style;    import android.support.v4.view.ViewPager;    import android.util.AttributeSet;    import android.view.View;        public class ADView extends ViewPager {            AdAdapter mAdAdapter;        Context context;        Paint paint;        List<Map<String, String>> urls;            public ADView(Context context, AttributeSet attrs) {            super(context, attrs);            this.context = context;            paint = new Paint();            InitAdapter();        }            private void InitAdapter() {            urls = new ArrayList<Map<String, String>>();            mAdAdapter = new AdAdapter(context, urls);            this.setAdapter(mAdAdapter);        }            public void setData(List<Map<String, String>> data) {            urls.clear();            for (int i = 0; i < data.size(); i++) {                urls.add(data.get(i));            }            if(urls.size()>0){                this.setVisibility(View.VISIBLE);            }else{                this.setVisibility(View.GONE);            }            mAdAdapter.notifyDataSetChanged();        }            @Override        public void draw(Canvas canvas) {            super.draw(canvas);            drawCycle(canvas);        }            private void drawCycle(Canvas canvas) {            canvas.save();            canvas.translate(getScrollX(), getScrollY());            int count = 0;            if (this.getAdapter() != null) {                count = this.getAdapter().getCount();            }            int select = getCurrentItem();            float density = getContext().getResources().getDisplayMetrics().density;            int itemWidth = (int) (11 * density);            int itemHeight = itemWidth / 2;            int x = (getWidth() - count * itemWidth)/2;            int y = getHeight() - itemWidth;            int minItemHeight = (int) ((float) itemHeight * 0.8F);            paint.setAntiAlias(true);                paint.setStyle(Style.FILL);            for (int i = 0; i < count; i++) {                if (select == i) {                    paint.setColor(0xFFbdbdbd);                    canvas.drawCircle(x + itemWidth * i + itemWidth / 2, y, minItemHeight, paint);                } else {                    paint.setColor(0xFFe6e6e6);                    canvas.drawCircle(x + itemWidth * i + itemWidth / 2, y, minItemHeight, paint);                }            }            canvas.restore();        }        }