[Android] add small dot indicator for ViewPager with the most memory-saving

Source: Internet
Author: User

[Android] add small dot indicator for ViewPager with the most memory-saving
Principle:

You can use View superposition to achieve this, but this effect is not ideal. 1 occupies the memory, which is inconvenient for transplantation. You need to find various resources, so it is better and convenient to use your own rendering, and high efficiency.

Solution:
package com.youtu.view;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
 
  > urls;public ADView(Context context, AttributeSet attrs) {super(context, attrs);this.context = context;paint = new Paint();InitAdapter();}private void InitAdapter() {urls = new ArrayList
  
   >();mAdAdapter = new AdAdapter(context, urls);this.setAdapter(mAdAdapter);}public void setData(List
   
    > 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();}@Overridepublic 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();}}
   
  
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.