/*
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
======`-.____`-.___\_____/___.-`____.-'======
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 永无BUG
*/
// 佛曰:
// 写字楼里写字间,写字间里程序员;
// 程序人员写程序,又拿程序换酒钱。
// 酒醒只在网上坐,酒醉还来网下眠;
// 酒醉酒醒日复日,网上网下年复年。
// 但愿老死电脑间,不愿鞠躬老板前;
// 奔驰宝马贵者趣,公交自行程序员。
// 别人笑我忒疯癫,我笑自己命太贱;
package com.fanxin.huangfangyi.main.activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.fanxin.huangfangyi.DemoApplication;
import com.fanxin.huangfangyi.R;
import com.fanxin.huangfangyi.domain.Info;
import com.fanxin.huangfangyi.main.widget.custom.CircularImage;
import com.fanxin.huangfangyi.main.widget.custom.CustomViewPager;
import com.fanxin.huangfangyi.main.widget.custom.FixedSpeedScroller;
import com.fanxin.huangfangyi.main.widget.custom.RadarView;
import com.fanxin.huangfangyi.main.widget.custom.RadarViewGroup;
import com.fanxin.huangfangyi.main.widget.custom.ZoomOutPageTransformer;
import com.fanxin.huangfangyi.ui.BaseActivity;
import java.lang.reflect.Field;
/**
* 项目名称:FanXin3.1
* 类描述:RadarActivity 描述:
* 创建人:songlijie
* 创建时间:2016/11/4 15:30
* 邮箱:814326663@qq.com
*/
public class RadarActivity extends BaseActivity implements ViewPager.OnPageChangeListener, RadarViewGroup.IRadarClickListener{
private String TAG = RadarActivity.class.getSimpleName();
private CustomViewPager viewPager;
private RelativeLayout ryContainer;
private RadarViewGroup radarViewGroup;
private RadarView id_scan_circle;
private int mPosition;
private FixedSpeedScroller scroller;
private SparseArray<Info> mDatas = new SparseArray<>();
private ImageView iv_back,iv_camera;
private TextView tv_title;
private RelativeLayout titleBar;
private int[] mImgs = {R.drawable.app_logo, R.drawable.weiyunxinlogo, R.drawable.app_logo,
R.drawable.weiyunxinlogo, R.drawable.app_logo, R.drawable.weiyunxinlogo, R.drawable.app_logo, R.drawable.weiyunxinlogo, R.drawable.app_logo,
R.drawable.weiyunxinlogo, R.drawable.app_logo, R.drawable.weiyunxinlogo, R.drawable.app_logo, R.drawable.weiyunxinlogo};
private String[] mNames = {"张三", "马六甲", "王尼玛", "张全蛋", "张根硕", "砖家", "叫兽", "匠师"};
//TODO 雷达扫描须进入页面是传一个经纬度给服务器,后请求下来数据做演示,这里面只做死数据展示.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DemoApplication.getInstance().saveActivity(this);
setContentView(R.layout.activity_radarscan);
initView();
initData();
setOnClick();
}
private void setOnClick() {
/**
* 将Viewpager所在容器的事件分发交给ViewPager
*/
ryContainer.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return viewPager.dispatchTouchEvent(event);
}
});
ViewpagerAdapter mAdapter = new ViewpagerAdapter();
viewPager.setAdapter(mAdapter);
//设置缓存数为展示的数目
viewPager.setOffscreenPageLimit(mImgs.length);
viewPager.setPageMargin(getResources().getDimensionPixelOffset(R.dimen.viewpager_margin));
//设置切换动画
viewPager.setPageTransformer(true, new ZoomOutPageTransformer());
viewPager.addOnPageChangeListener(this);
setViewPagerSpeed(250);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
radarViewGroup.setDatas(mDatas);
}
}, 1500);
radarViewGroup.setiRadarClickListener(this);
//返回键
iv_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
back(v);
}
});
}
private void initData() {
for (int i = 0; i < mImgs.length; i++) {
Info info = new Info();
info.setPortraitId(mImgs[i]);
info.setAge(((int) Math.random() * 25 + 16) + "岁");
info.setName(mNames[(int) (Math.random() * mNames.length)]);
info.setSex(i % 3 == 0 ? false : true);
info.setDistance(Math.round((Math.random() * 10) * 100) / 100);
mDatas.put(i, info);
}
}
private void initView() {
viewPager = (CustomViewPager) findViewById(R.id.vp);
radarViewGroup = (RadarViewGroup) findViewById(R.id.radar);
ryContainer = (RelativeLayout) findViewById(R.id.ry_container);
id_scan_circle = (RadarView) findViewById(R.id.id_scan_circle);
// id_scan_circle.setCenterBitmap(R.drawable.weiyunxinlogo);
iv_back = (ImageView) findViewById(R.id.iv_back);
iv_camera = (ImageView) findViewById(R.id.iv_camera);
tv_title = (TextView) findViewById(R.id.tv_title);
titleBar = (RelativeLayout) findViewById(R.id.title);
iv_camera.setVisibility(View.GONE);
tv_title.setText("雷达扫描");
}
/**
* 设置ViewPager切换速度
*
* @param duration
*/
private void setViewPagerSpeed(int duration) {
try {
Field field = ViewPager.class.getDeclaredField("mScroller");
field.setAccessible(true);
scroller = new FixedSpeedScroller(RadarActivity.this, new AccelerateInterpolator());
field.set(viewPager, scroller);
scroller.setmDuration(duration);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
mPosition = position;
}
@Override
public void onPageSelected(int position) {
radarViewGroup.setCurrentShowItem(position);
Log.d(TAG,"当前位置 " + mPosition);
Log.d(TAG,"速度 " + viewPager.getSpeed());
//当手指左滑速度大于2000时viewpager右滑(注意是item+2)
if (viewPager.getSpeed() < -1800) {
viewPager.setCurrentItem(mPosition + 2);
Log.d(TAG, "位置 " + mPosition);
viewPager.setSpeed(0);
} else if (viewPager.getSpeed() > 1800 && mPosition > 0) {
//当手指右滑速度大于2000时viewpager左滑(注意item-1即可)
viewPager.setCurrentItem(mPosition - 1);
Log.d(TAG, "位置 " + mPosition);
viewPager.setSpeed(0);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onRadarItemClick(int position) {
viewPager.setCurrentItem(position);
}
class ViewpagerAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, final int position) {
final Info info = mDatas.get(position);
//设置一大堆演示用的数据,麻里麻烦~~
View view = LayoutInflater.from(RadarActivity.this).inflate(R.layout.item_viewpager_layout, null);
CircularImage ivPortrait = (CircularImage) view.findViewById(R.id.iv);
ImageView ivSex = (ImageView) view.findViewById(R.id.iv_sex);
TextView tvName = (TextView) view.findViewById(R.id.tv_name);
TextView tvDistance = (TextView) view.findViewById(R.id.tv_distance);
TextView tv_say_hello = (TextView) view.findViewById(R.id.tv_say_hello);
tvName.setText(info.getName());
tvDistance.setText(info.getDistance() + "km");
ivPortrait.setImageResource(info.getPortraitId());
if (info.getSex()) {
ivSex.setImageResource(R.drawable.girl);
} else {
ivSex.setImageResource(R.drawable.boy);
}
tv_say_hello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(RadarActivity.this, "去给"+info.getName()+"打招呼吧!", Toast.LENGTH_SHORT).show();
}
});
ivPortrait.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(RadarActivity.this, "这是 " + info.getName(), Toast.LENGTH_SHORT).show();
}
});
container.addView(view);
return view;
}
@Override
public int getCount() {
return mImgs.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View) object;
container.removeView(view);
}
}
}