Android open-source components,
I. Component Introduction |
The header of the information list in the App product will contain automatic rotation advertisement images. ViewPager can be used for implementation, but encoding is troublesome. We can use the LoopView open-source control to complete this process, loopView is a powerful rotation chart control and provides many configuration methods to meet your application needs.
II. Environment Configuration |
If your project is built using Gradle, you only need to add the following line to dependencies in your build. gradle file:
compile 'com.kevin:loopview:1.0.4'
1. Configure LoopView in layout. xml
Add the <com. kevin. loopview. AdLoopView> code in the Layout file as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:kevin="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.kevin.loopview.AdLoopView android:id="@+id/adloop_act_adloopview" android:layout_width="match_parent" android:layout_height="192dp" kevin:loop_interval="5000" kevin:loop_dotMargin="5dp" kevin:loop_autoLoop="true" kevin:loop_dotSelector="@drawable/ad_dots_selector" kevin:loop_layout="@layout/ad_loopview_layout"> </com.kevin.loopview.AdLoopView></RelativeLayout>
2. Add code in Activity:
Public class AdLoopActivity extends Activity implements BaseLoopAdapter. onItemClickListener {AdLoopView mLoopView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_adloopview); initViews (); initEvents ();} private void initViews () {mLoopView = (AdLoopView) this. findViewById (R. id. adloop_act_adloopview); initRotateVie W ();}/*** initialize LoopView */private void initRotateView () {// set custom Layout // mLoopView. setLoopLayout (R. layout. ad_loopview_layout); // sets the data String json = LocalFileUtils. getStringFormAsset (this, "loopview_date.json"); LoopData loopData = JsonTool. toBean (json, LoopData. class); if (null! = LoopData) {mLoopView. refreshData (loopData);} // sets the page switching excessive event mLoopView. setScrollDuration (2000); // sets the page switching interval mLoopView. setInterval (3000);}/*** initialization event */private void initEvents () {mLoopView. setOnClickListener (this) ;}@ Override public void onItemClick (PagerAdapter parent, View view, int position, int realPosition) {LoopData loopData = mLoopView. getLoopData (); String url = loopData. items. get (position ). link; Intent intent = new Intent (); intent. setData (Uri. parse (url); intent. setAction (Intent. ACTION_VIEW); startActivity (intent) ;}@ Override protected void onDestroy () {super. onDestroy ();}}
3. Main methods involved in LocalFileUtils
Public class LocalFileUtils {/*** get text content under Asset */public static String getStringFormAsset (Context context, String str) {BufferedReader in = null; try {in = new BufferedReader (new InputStreamReader (context. getAssets (). open (str); String line; StringBuilder buffer = new StringBuilder (); while (line = in. readLine ())! = Null) {buffer. append (line ). append ('\ n');} return buffer. toString ();} catch (IOException e) {e. printStackTrace (); return "" ;}finally {if (in! = Null) {try {in. close (); in = null;} catch (IOException e) {e. printStackTrace ();}}}}}
Iv. LoopView main methods |
// Set the time for switching the ViewPager page to mLoopView. setScrollDuration (1000); // sets the rotation interval mLoopView. setInterval (3000); // initialize the mLoopView data in the form of a set. setLoopViewPager (List <Map <String, String> data); // initialize the mLoopView data in JSON format. setLoopViewPager (String jsonData); // initialize the mLoopView as a data entity. setLoopViewPager (LoopData rotateData); // refresh the data in the form of a set mLoopView. refreshData (final List <Map <String, String> data); // refresh the data in the form of data entities. refreshData (LoopData loopData); // refresh the data in JSON format. refreshData (String jsonData); // obtain the configured rotation big chart data mLoopView. getLoopData (); // starts automatic rotation of mLoopView. startAutoLoop (); // automatically rotate the mLoopView after the specified time delay. startAutoLoop (long delayTimeInMills); // stop automatic rotation of mLoopView. stopAutoLoop (); // sets the custom Layout of mLoopView. setLoopLayout (int layoutResId );
Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation: