2013年8月5日星期一

Android Launcher analyze and modify 11 - custom paging indicator(paged_view_indicator)

 

Android4.0 the Launcher comes with a simple paging indicator is Hotseat above that line, this is essentially an ImageView use .9. png images do , the effect is really quite beautiful, with testers, then ugly. Especially in conjunction with other style icon and background, did not look good. So I plan to re-write an indicator. This so-called paging indicator function is very simple, is to tell how many split-screen user interface, is currently in the first sort of screen. Of course, now there are some Launcher enhances this feature, just slip these indicators can quickly slide the desktop.

 

today to talk about how to customize a paging indicator and added to the Launcher inside use slide switch function subsequent repeat. The default blue paging indicator ~

 

 

(PS: the new QQ group, who are interested can join together to discuss: Android group: 322599434)

 

 

1, Custom View

 

order to implement custom paging indicator, you need to customize a View, this is easy to add other functions later into it, it will not disrupt the original Launcher code logic. Finally, I chose to use as the View LinearLayout container, heavy LinearLayout do this paging indicator. LinearLayout use these pages as long as the horizontal arrangement of icons can be.

 
  
   
//Edited by mythou
//http://www.cnblogs.com/mythou/
  
public class PageSlideIndicator extends LinearLayout 
{
private final static String TAG="OWL_PageIndicator";
//控件页面滑动,操作PagedView
PagedView mPagedView
=null;

Context mContext;

//其他页面标识
Drawable mNormalDrawable;
//当前页面标识
Drawable mFocusDrawable;

//总页面数
int mTotalPage=0;
//当前页面
int mCurPageNum=0;

//...............
}
 
 

The above is my definition of a paging indicator of class, the main attribute is the two Drawable, representing two different pictures, the current page and the other pages pictures . The reason why is because the direct definition Drawable, the number of other pages indicator is uncertain, here Drawable save the picture directly, to facilitate subsequent creation ImageView use. Of course, you can also load the ImageView, dynamic maintenance of a queue, but will be more consumption of resources.

 

There are also two of the more important properties, the total number of pages and the current page number. These two attributes determine the paging indicator that you need to display the number and position of the indicator displays the current page. These two properties of the data, we can get to the inside through PagedView class. (Add this page indicator requires PagedView, Workspace, Launcher, AppsCustomizePagedView these classes have a certain understanding)

 

addition, I also defined a PagedView object used to do this is to facilitate the subsequent slide page, you need to call PagedView approach. Sliding function temporarily not speak today, a later ~

 

 

2, initialize the paging indicator

 
  
//Edited by mythou
//http://www.cnblogs.com/mythou/
  
  public void reflashIndacitor(int curPage, int totalPage) 
{
mTotalPage
= totalPage;
mCurPageNum
= curPage;
//清空容器的view对象
this.removeAllViews();
//添加分页指示器
for (int page = 0; page < mTotalPage; ++page)
{
       //View的属性设置
LayoutParams layoutParams
= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin
=10;
layoutParams.rightMargin
=10;
       //新建ImageView,用来显示分页图标
ImageView newPageImageView
= new ImageView(mContext);
   //根据当前页还是其他页面设置对应图标
if (page == mCurPageNum)
{
newPageImageView.setBackgroundDrawable(mFocusDrawable);
}
else
{
newPageImageView.setBackgroundDrawable(mNormalDrawable);
}
//把图标添加到LinearLayout
this.addView(newPageImageView, layoutParams);
}
}
 
 

above is a simple initialization process, the total number of pages based on the incoming and the current page number, set the picture as well as initialization LinearLayout. It should be noted that the above ImageView because it is dynamically created, so it is necessary to use LayoutParams class to set parameters related attributes.

 

 

3, resource file reference

 

Here we look at how to use the XML configuration file which prepare our new class, here only interface Workspace configuration. workspace and AllApp pages inside is not the same configuration. Because two different View interface reference configuration, so if you want two interfaces are using custom paging indicators need to be configured to use. Basically the same principle, here only Workspace configuration.

 
  
   
//Edited by mythou
//http://www.cnblogs.com/mythou/
  
<!--分页指示器  OWL  --> 
<com.android.launcher2.PageSlideIndicator
android:id
="@+id/workSpacePageIndicator"
android:layout_width
="match_parent"
android:layout_height
="wrap_content"
android:layout_marginBottom
="153dp"
android:layout_gravity
="bottom"
android:gravity
="center_horizontal"
/>
 
 

above referenced inside is my launcher.xml we just write paging indicator. As for the location of the View, according to the actual interface you need to adjust, I was placed in the hotseat here above. Configurable attributes inside nothing special, in fact, is to configure a LinearLayout properties.

 

 

4, Launcher.java inside initialize

 
  
   
//Edited by mythou
//http://www.cnblogs.com/mythou/
  
//初始化页面标识器 OWL 
mPageSlideIndicator = (PageSlideIndicator)findViewById(R.id.workSpacePageIndicator);
mPageSlideIndicator.InitPageSlideIndicator(
3, 1);
mPageSlideIndicator.setPageViewObject(mWorkspace);
mWorkspace.setPageIndicator(mPageSlideIndicator);
 
 

Then we Launcher.java inside initialize the class, some may ask why you want to initialize Launcher.java inside rather than inside the workspace initialization, because this is actually Using the workspace data inside. The reason is because our XML configuration file is referenced in launcher.xml file inside, we need to find the corresponding resources. Note that the page initialization how many pages and the current page into the store, I am here to set a fixed value, because I only three default workspace, the current page is the first two. This you can default setting, you can also load the interface is complete, access workspace data set again. Another object is to pass this PageSlideIndicator workspace class to go inside, because the specific detection of the page in the workspace inside the sliding switch is complete (to be exact PagedView).

 

 

5, detection sliding switch pages

 
  
    
  
//Edited by mythou
//http://www.cnblogs.com/mythou/
  
  protected void onPageEndMoving()  
{
super.onPageEndMoving();
if (isHardwareAccelerated())
{
updateChildrenLayersEnabled();
}
else
{
clearChildrenCache();
}

if (!mDragController.dragging())
{

if (LauncherApplication.isScreenLarge())
{
hideOutlines();
}
}
mOverScrollMaxBackgroundAlpha
= 0.0f;
mOverScrollPageIndex
= -1;
if (mDelayedResizeRunnable != null)
{
mDelayedResizeRunnable.run();
mDelayedResizeRunnable
= null;
}
//检测页面滑动完成,设置相关页面切换
mPageSlideIndicator.setCurrentPage(mCurrentPage, getPageCount());
}
 
 

related pages and then we switched data passed to PageSlideIndicator handle on it. If you do not understand these processes friend, you need to first understand the Launcher workspace inside PagedView and relationships, and how they will deal with the sliding switch pages, which I have analyzed the previous article, are interested can check my previous article.

 

Another point is that, if you want to make a more flashy effects, where you can add animation effects. Effects such as fade, the user experience will be better. I am here simply to explain how to implement custom paging indicator.

 

Another point to note is that if your page is dynamic (eg AllAPP interface inside), meaning that the number of pages you are not fixed , the need to detect changes in APP and listen removeview other methods, and then dynamically modify the number of pages you. ALlAPP interface inside monitor the number of pages you need to change, because this is uncertain, depending on the number of users to install software dynamics.

 

 

6, after the words

 

look back, this is the first 11 related articles Launcher analyzed and modified, but today looked at, I found an article that Launcher to see people Few o (╯ □ ╰) o, I do not know that I write too Launcher food or engage in the development of fewer people. Anyway, this series will continue writing.

 

 

series of articles:

 

Android Launcher analyze and modify 1 - Launcher default interface configuration (default_workspace)

 

Android Launcher analyze and modify 2 - Icon modified interface layout adjustments, wallpaper set

 

Android Launcher analyzed and modified 3 - Launcher startup and initialization

 

Android Launcher analysis and Amendment 4 - Initial Load Data <​​a>

 

Android Launcher analyzed and modified 5 - HotSeat Analysis

 

Android Launcher analysis and Amendment 6 - page slide (PagedView

 

Android Launcher analyzed and modified 7 - AllApp Full application list (AppsCustomizeTabHost)

 

Android Launcher analyzed and modified 8 - AllAPP drag interface elements (PagedViewWithDraggableItems)

 

Android Launcher analyzed and modified 9 - Launcher starts APP process

 

Android Launcher analyze and modify 10 - HotSeat depth Advanced

 

 

Edited by mythou

 

Original blog, reproduced, please indicate the source: http://www.cnblogs.com/mythou/p/3231000.html

 

没有评论:

发表评论