您的位置:首页 > 其它

谷歌电子市场5--推荐

2016-11-27 18:16 351 查看


1.
RecommendFragment

public class RecommendFragmentextends BaseFragment {

private ArrayList<String> mList;

@Override
public View onCreateSuccessView() {
// 初始化飞入飞出自定义控件
StellarMap stellar = new StellarMap(UIUtils.getContext());
// 设置内部文字距边缘边距为10dip
int padding = UIUtils.dip2px(10);
stellar.setInnerPadding(padding, padding, padding, padding);
// 设置数据源
stellar.setAdapter(new RecommendAdapter());
// 设定展示规则,9行6列(具体以随机结果为准)
stellar.setRegularity(6, 9);
// 设置默认组为第0组
stellar.setGroup(0, true);

return stellar;
}

@Override
public ResultState onLoad() {
RecommendProtocol protocol = new RecommendProtocol();
mList = protocol.getData(0);// 33条数据
return check(mList);
}

class RecommendAdapter implements StellarMap.Adapter {
// 返回组的数量
@Override
public int getGroupCount() {
return 2;
}

// 每组某个组下返回孩子的个数
@Override
public int getCount(int group) {
int count = mList.size() / getGroupCount();// 用总数除以组个数就是每组应该展示的孩子的个数
if (group == getGroupCount() - 1) {// 由于上一行代码不一定整除, 最后一组,将余数补上
count += mList.size() % getGroupCount();
}

return count;
}

@Override
public View getView(int group, int position, View convertView) {
if (group > 0) {// 如果发现不是第一组,需要更新position, 要加上之前几页的总数,才是当前组的准确位置
position = position + getCount(group - 1) * group;
}

TextView view = new TextView(UIUtils.getContext());
view.setText(mList.get(position));

// 设置随机文字大小
Random random = new Random();
int size = 16 + random.nextInt(10);// 产生16-25的随机数
view.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);// 以sp为单位设置文字大小

// 设置随机文字颜色
int r = 30 + random.nextInt(210);// 产生30-239的随机颜色, 绕过0-29,
// 240-255的值,避免颜色过暗或者过亮
int g = 30 + random.nextInt(210);
int b = 30 + random.nextInt(210);
view.setTextColor(Color.rgb(r, g, b));

return view;
}

@Override
public int getNextGroupOnZoom(int group, boolean isZoomIn) {
if (!isZoomIn) {
// 下一组
if (group < getGroupCount() - 1) {
return ++group;
} else {
return 0;// 如果没有下一页了,就跳到第一组
}
} else {
// 上一组
if (group > 0) {
return --group;
} else {
return getGroupCount() - 1;// 如果没有上一页了,就跳到最后一组
}
}
}
}

}


  

2.飞入飞出自定义控件 StellarMap

public class StellarMap extends FrameLayout implements AnimationListener, OnTouchListener, OnGestureListener {

private RandomLayout mHidenGroup;

private RandomLayout mShownGroup;

private Adapter mAdapter;
private RandomLayout.Adapter mShownGroupAdapter;
private RandomLayout.Adapter mHidenGroupAdapter;

private int mShownGroupIndex;// 显示的组
private int mHidenGroupIndex;// 隐藏的组
private int mGroupCount;// 组数

/** 动画 */
private Animation mZoomInNearAnim;
private Animation mZoomInAwayAnim;
private Animation mZoomOutNearAnim;
private Animation mZoomOutAwayAnim;

private Animation mPanInAnim;
private Animation mPanOutAnim;
/** 手势识别器 */
private GestureDetector mGestureDetector;

/** 构造方法 */
public StellarMap(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}

public StellarMap(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public StellarMap(Context context) {
super(context);
init();
}

/** 初始化方法 */
private void init() {
mGroupCount = 0;
mHidenGroupIndex = -1;
mShownGroupIndex = -1;
mHidenGroup = new RandomLayout(getContext());
mShownGroup = new RandomLayout(getContext());

addView(mHidenGroup, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mHidenGroup.setVisibility(View.GONE);
addView(mShownGroup, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

mGestureDetector = new GestureDetector(this);
setOnTouchListener(this);
//设置动画
mZoomInNearAnim = AnimationUtil.createZoomInNearAnim();
mZoomInNearAnim.setAnimationListener(this);
mZoomInAwayAnim = AnimationUtil.createZoomInAwayAnim();
mZoomInAwayAnim.setAnimationListener(this);
mZoomOutNearAnim = AnimationUtil.createZoomOutNearAnim();
mZoomOutNearAnim.setAnimationListener(this);
mZoomOutAwayAnim = AnimationUtil.createZoomOutAwayAnim();
mZoomOutAwayAnim.setAnimationListener(this);
}

/** 设置隐藏组和显示组的x和y的规则 */
public void setRegularity(int xRegularity, int yRegularity) {
mHidenGroup.setRegularity(xRegularity, yRegularity);
mShownGroup.setRegularity(xRegularity, yRegularity);
}

private void setChildAdapter() {
if (null == mAdapter) {
return;
}
mHidenGroupAdapter = new RandomLayout.Adapter() {
//取出本Adapter的View对象给HidenGroup的Adapter
@Override
public View getView(int position, View convertView) {
return mAdapter.getView(mHidenGroupIndex, position, convertView);
}

@Override
public int getCount() {
return mAdapter.getCount(mHidenGroupIndex);
}
};
mHidenGroup.setAdapter(mHidenGroupAdapter);

mShownGroupAdapter = new RandomLayout.Adapter() {
//取出本Adapter的View对象给ShownGroup的Adapter
@Override
public View getView(int position, View convertView) {
return mAdapter.getView(mShownGroupIndex, position, convertView);
}

@Override
public int getCount() {
return mAdapter.getCount(mShownGroupIndex);
}
};
mShownGroup.setAdapter(mShownGroupAdapter);
}

/** 设置本Adapter */
public void setAdapter(Adapter adapter) {
mAdapter = adapter;
mGroupCount = mAdapter.getGroupCount();
if (mGroupCount > 0) {
mShownGroupIndex = 0;
}
setChildAdapter();
}

/** 设置显示区域 */
public void setInnerPadding(int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) {
mHidenGroup.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
mShownGroup.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}

/** 给指定的Group设置动画 */
public void setGroup(int groupIndex, boolean playAnimation) {
switchGroup(groupIndex, playAnimation, mZoomInNearAnim, mZoomInAwayAnim);
}

/** 获取当前显示的group角标 */
public int getCurrentGroup() {
return mShownGroupIndex;
}

/** 给Group设置动画入 */
public void zoomIn() {
final int nextGroupIndex = mAdapter.getNextGroupOnZoom(mShownGroupIndex, true);
switchGroup(nextGroupIndex, true, mZoomInNearAnim, mZoomInAwayAnim);
}

/** 给Group设置出动画 */
public void zoomOut() {
final int nextGroupIndex = mAdapter.getNextGroupOnZoom(mShownGroupIndex, false);
switchGroup(nextGroupIndex, true, mZoomOutNearAnim, mZoomOutAwayAnim);
}

/** 给下一个Group设置进出动画 */
private void switchGroup(int newGroupIndex, boolean playAnimation, Animation inAnim, Animation outAnim) {
if (newGroupIndex < 0 || newGroupIndex >= mGroupCount) {
return;
}
//把当前显示Group角标设置为隐藏的
mHidenGroupIndex = mShownGroupIndex;
//把下一个Group角标设置为显示的
mShownGroupIndex = newGroupIndex;
// 交换两个Group
RandomLayout temp = mShownGroup;
mShownGroup = mHidenGroup;
mShownGroup.setAdapter(mShownGroupAdapter);
mHidenGroup = temp;
mHidenGroup.setAdapter(mHidenGroupAdapter);
//刷新显示的Group
mShownGroup.refresh();
//显示Group
mShownGroup.setVisibility(View.VISIBLE);

//启动动画
if (playAnimation) {
if (mShownGroup.hasLayouted()) {
mShownGroup.startAnimation(inAnim);
}
mHidenGroup.startAnimation(outAnim);
} else {
mHidenGroup.setVisibility(View.GONE);
}
}

// 重新分配显示区域
public void redistribute() {
mShownGroup.redistribute();
}

/** 动画监听 */
@Override
public void onAnimationStart(Animation animation) {
// 当动画启动
}

@Override
public void onAnimationEnd(Animation animation) {
// 当动画结束
if (animation == mZoomInAwayAnim || animation == mZoomOutAwayAnim || animation == mPanOutAnim) {
mHidenGroup.setVisibility(View.GONE);
}
}

@Override
public void onAnimationRepeat(Animation animation) {
// 当动画重复
}

/** 定位 */
@Override
public void onLayout(boolean changed, int l, int t, int r, int b) {
//用以判断ShownGroup是否onLayout的变量
boolean hasLayoutedBefore = mShownGroup.hasLayouted();
super.onLayout(changed, l, t, r, b);
if (!hasLayoutedBefore && mShownGroup.hasLayouted()) {
mShownGroup.startAnimation(mZoomInNearAnim);//第一次layout的时候启动动画
} else {
mShownGroup.setVisibility(View.VISIBLE);
}
}

/** 重写onTouch事件,把onTouch事件分配给手势识别 */
@Override
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}

/** 消费掉onDown事件 */
@Override
public boolean onDown(MotionEvent e) {
return true;
}

/** 空实现 */
@Override
public void onShowPress(MotionEvent e) {
}

@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
int centerX = getMeasuredWidth() / 2;
int centerY = getMeasuredWidth() / 2;

int x1 = (int) e1.getX() - centerX;
int y1 = (int) e1.getY() - centerY;
int x2 = (int) e2.getX() - centerX;
int y2 = (int) e2.getY() - centerY;

if ((x1 * x1 + y1 * y1) > (x2 * x2 + y2 * y2)) {
zoomOut();
} else {
zoomIn();
}
return true;
}

/** 内部类、接口 */
public static interface Adapter {
public abstract int getGroupCount();

public abstract int getCount(int group);

public abstract View getView(int group, int position, View convertView);

public abstract int getNextGroupOnZoom(int group, boolean isZoomIn);
}
}


 


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: