This article shares the specific code for Android custom Banner carousel effect display for your reference. The specific content is as follows
Customize View Layout
<RelativeLayout xmlns:andro android:layout_width="match_parent" android:layout_height="wrap_content"> <. android: android:layout_width="match_parent" android:layout_height="200dp"> </.> <LinearLayout android: android:layout_centerHorizontal="true" android:layout_alignBottom="@+id/banner_view_pager" android:layout_marginBottom="10dp" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> </LinearLayout> </RelativeLayout>
Customize View body content
public class CustomBanner extends FrameLayout { @BindView(.banner_view_pager) ViewPager bannerViewPager; @BindView(.linear_bannner) LinearLayout linearBannner; private List<String> list; private int time = 2; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if ( == 0) { int currentItem = (); (currentItem + 1); //Send again sendEmptyMessageDelayed(0, time * 1000); } } }; private List<ImageView> listDoc; private OnClickLisner onClickLisner; public CustomBanner(@NonNull Context context) { super(context); init(); } public CustomBanner(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public CustomBanner(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } /** * Initialization */ private void init() { View view = (getContext(), .bannner_layout, this); (this, view); } /** * Provide external method of setting image path */ public void setImageUrls(List<String> list) { = list; if (list == null) { return; } //Set the adapter LunBoAdapter lunBoAdapter = new LunBoAdapter(getContext(), list); (lunBoAdapter); initDoc(); //Show a certain position in the middle (() * 10000); //Use handler to automatically carousel (0, time * 1000); //Stection event of changing status (new () { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { // When selecting a certain page, switch the background of the dots for (int i = 0; i < (); i++) { if (position % () == i) { (i).setBackgroundResource(.shape_01); } else { (i).setBackgroundResource(.shape_02); } } } @Override public void onPageScrollStateChanged(int state) { } }); } /** * Initialize small dots */ private void initDoc() { //Create a collection to record these small dots listDoc = new ArrayList<>(); //Clear the layout (); for (int i = 0; i < (); i++) { ImageView docImage = new ImageView(getContext()); if (i == 0) { (.shape_01); } else { (.shape_02); } //Add to collection (docImage); //Add to linear layout params = new (.WRAP_CONTENT, .WRAP_CONTENT); (5, 0, 5, 0); (docImage, params); } } /** * Provide carousel time */ public void setTimeSecond(int time) { = time; } /** * Click Event * * @param onClickLisner */ public void setClickListner(OnClickLisner onClickLisner) { = onClickLisner; } private class LunBoAdapter extends PagerAdapter { private List<String> list; private Context context; public LunBoAdapter(Context context, List<String> list) { = context; = list; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, final int position) { //Create imageView ImageView imageView = new ImageView(context); (.FIT_XY); //Load this picture (context).load((position % ())).into(imageView); //Click event (new OnClickListener() { @Override public void onClick(View view) { //trigger (position % ()); } }); //Touch Event (new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (()) { case MotionEvent.ACTION_DOWN: //Cancel the message and callback on the handler (null); break; case MotionEvent.ACTION_MOVE: (null); break; case MotionEvent.ACTION_CANCEL: (0, time * 1000); break; case MotionEvent.ACTION_UP: (0, time * 1000); break; } return false; } }); //Add to container (imageView); //return return imageView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((View) object); } } public interface OnClickLisner { void onItemClick(int position); } }
Custom dots
public class CountView extends View implements { private int count = 0; public CountView(Context context) { super(context); init(); } public CountView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public CountView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } //Initialization method private void init() { (this); } @Override protected void onDraw(Canvas canvas) { (canvas); Paint paint = new Paint(); (); (true); (); //round (300,300,200,paint); (); (100); String text = (count); //Get the width and height of the text Rect rect = new Rect(); (text,0,(),rect); (text,()/2,300+()/2,paint); } @Override public void onClick(View view) { count ++; //Repaint postInvalidate(); } }
Small dot shapepe
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:andro> <solid android:color="#00ff00"/> <corners android:radius="10dp"/> <size android:height="10dp" android:width="10dp"/> </shape> <?xml version="1.0" encoding="utf-8"?> <shape xmlns:andro> <solid android:color="#ff0000"/> <corners android:radius="10dp"/> <size android:height="10dp" android:width="10dp"/> </shape>
Running in Main
public class MainActivity extends AppCompatActivity { private CustomBanner customBanner; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); customBanner = findViewById(.custom_banner); getDataFromNet(); } private void getDataFromNet() { ("/ad/getAd", new Callback() { private List<String> list; @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (()){ String json = ().string(); final HomeBean detalBean = new Gson().fromJson(json,); list = new ArrayList<>(); List<> data = (); for (int i = 0; i < (); i++) { String icon = (i).getIcon(); (icon); } runOnUiThread(new Runnable() { @Override public void run() { //Set time (5); //Set the display carousel (list); // Events of the click to jump to the details page of banner (new OnBannerListener() { @Override public void OnBannerClick(int position) { List<> datab = (); if ((position).getType() == 0) { Intent intent = new Intent(getActivity(), ); ("databurl", (position).getUrl()); startActivity(intent); } else { (getContext(), "Redirect to product details page soon", Toast.LENGTH_SHORT).show(); } } }); } }); } } }); } }
WebView page
public class WebViewActivity extends AppCompatActivity { private WebView web_view; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_web_view); web_view = findViewById(.web_view); String databurl = getIntent().getStringExtra("databurl"); web_view.loadUrl(databurl); //A series of settings for webview web_view.setWebViewClient(new WebViewClient());//Open in the current application, instead of going to the browser WebSettings settings = web_view.getSettings(); (true); (true); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.