Launcher FolderGroup 指示器效果

FolderGroup这个名字是我自己想的,意思一目了然,那到到啥是FolderGroup呢?

来看这种效果:



自从某桌面第一次尝试这种文件夹的交互方式之后,马上就被抄袭的一塌糊涂。对Launcher又了解的人都知道,Worksapce的左滑和右滑效果是通过重写了ViewGroup来实现的,单单这个类就将近4k行,系统的LinearLayout也不过才3k多。ViewPager也作为一个左滑右滑的框架,并且用起来很简单,Launcher中的整体数据的更新,删除,增加已经优化的很好了,所以,我们自己重新封装一个UI层来对文件夹的FolderGroup交互来做一个实现。

LauncherModel中有一个静态的HashMap,该map中装了所有的文件夹数据。

static final HashMap<Long, FolderInfo> sBgFolders = new HashMap<Long, FolderInfo>();

对于把数据填充的ViewPager中直接使用ViewPager或者封装一下再使用就不多介绍了,主要介绍指示器效果如何实现。

我们分析一下:指示器可以左右滚动,内部的标签装的文本,可以滚动直接用的就是ScrollView了。


1.继承HorizontalScrollView.java

2.内部整个控件用LinearLayout

3.LinearLayout中装TextView

4.左右滑动ViewPager时在onScroll方法中做处理去改变指示器


当滑动到结尾时:



FolderGroupIndicator.java指示器主要类,实现效果的时候我把文件夹的名字写成死的了,使用的时候可以传值过去。字的变大变小是在监听器中完成的。

public class FolderGroupIndicator extends HorizontalScrollView implements OnPageChangeListener, OnClickListener {

	private LinearLayout mContainer;
	private LinearLayout.LayoutParams defaultTabLayoutParams, expandedTabLayoutParams;
	private FolderGroup mGroup;
	private int mScrollOffset = 52;
	private int mLastScrollX = 0;
	private int mCurrentPosition;
	private float mCurrentPositionOffset = 0f;
	int a = 1;

	public FolderGroupIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		setFillViewport(true);
		setWillNotDraw(false);

		mContainer = new LinearLayout(getContext());
		mContainer.setOrientation(LinearLayout.HORIZONTAL);
		mContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		addView(mContainer);
		
		defaultTabLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
		expandedTabLayoutParams = new LinearLayout.LayoutParams(getContext().getResources().getDisplayMetrics().widthPixels,
				LayoutParams.MATCH_PARENT);//相当于屏幕宽的一个空View足够滚动了。
		setIndicator(new String[] { "文件夹", "新建文件夹", "工具" });//死的数据,文件夹没有名字,我只好写成死的来看效果

	}

	public void setIndicator(String[] title) {
		mContainer.removeAllViews();
		for (int i = 0; i < title.length; i++) {
			addChildView(title[i], i);
		}
		// 滚动效果偏移量不足,最后添加一个空的TextView填充
		addSupplementChild();
	}

	private void addChildView(String title, int i) {
		TextView mChild = new TextView(getContext());
		mChild.setText(title);
		mChild.setTextSize(20);
		mChild.setGravity(Gravity.BOTTOM | Gravity.CENTER);
		mChild.setPadding(100, 0, 0, 0);
		mChild.setSingleLine();
		mChild.setLayoutParams(defaultTabLayoutParams);
		mChild.setOnClickListener(this);
		mContainer.addView(mChild, i);
	}

	private void addSupplementChild() {
		TextView mChild = new TextView(getContext());
		mChild.setLayoutParams(expandedTabLayoutParams);
		mContainer.addView(mChild, mContainer.getChildCount());
	}

	public void setFolderGroup(FolderGroup group) {
		mGroup = group;
		mGroup.setOnPageChangeListener(this);
	}

	public void updateChildTextSize(int pos, float p) {
		TextView view1 = (TextView) mContainer.getChildAt(pos);
		view1.setTextSize(35 - 20 * p);//计算字体大小
		TextView view2 = (TextView) mContainer.getChildAt(pos + 1);
		view2.setTextSize(20 * p + 15);//同上,上面的变化到最大要和下面的吻合,不然会有bug


	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		return true;
	}

	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		int newscroll = mContainer.getChildAt(arg0).getLeft() + (int) (mContainer.getChildAt(arg0).getWidth() * arg1)-20;
		scrollTo(newscroll, 0);
		updateChildTextSize(arg0, arg1);
	}

	@Override
	public void onPageSelected(int arg0) {
	}

	@Override
	public void onClick(View v) {
		TextView view = (TextView) v;
		Log.i("Gmw", "view=" + view.getText().toString());

	}

}


使用:

<?xml version="1.0" encoding="utf-8"?>
<com.zoe.launcher.FolderGroupLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/folder_group"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/folder_group_blur"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/foldergroup_blur"
        android:scaleType="centerCrop" />

    <com.zoe.launcher.FolderGroupIndicator
        android:id="@+id/folder_group_indicator"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:scrollbars="none" >
    </com.zoe.launcher.FolderGroupIndicator>//这是指示器

    <com.zoe.launcher.FolderGroup
        android:id="@+id/folder_group_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/folder_group_indicator" >
    </com.zoe.launcher.FolderGroup>

</com.zoe.launcher.FolderGroupLayout>

有什么不合理的地方大家可以指出来,哈哈哈...
posted @ 2015-04-18 00:44  顾明伟  阅读(222)  评论(0编辑  收藏  举报