网友通过本文主要向大家介绍了谷歌电子市场官网,谷歌电子市场,谷歌安卓电子市场,谷歌官方电子市场,谷歌电子市场注册等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
谷歌电子市场5--推荐,谷歌电子市场5--

1.RecommendFragment
public class RecommendFragment extends 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

