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

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
这个你可以用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();  
    }  
  
}  

标签: isp

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Android静默安装和静默卸载代码

下一篇:android 字体大小 根据分辨率 自动调整