Skip to content

Commit

Permalink
Merge pull request hzuapps#931 from xuwenlang/master
Browse files Browse the repository at this point in the history
  • Loading branch information
zengsn authored Jun 14, 2016
2 parents ee1ce79 + b994d9d commit f1ae481
Show file tree
Hide file tree
Showing 55 changed files with 1,304 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,13 +1,194 @@
package edu.hzuapps.androidworks.homeworks.com1314080901139;

import android.app.Activity;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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;

public class Com1314080901139Activity extends AppCompatActivity {
import java.lang.reflect.Field;

import edu.hzuapps.androidworks.homeworks.com1314080901139.been.Info;
import edu.hzuapps.androidworks.homeworks.com1314080901139.custom.CustomViewPager;
import edu.hzuapps.androidworks.homeworks.com1314080901139.custom.RadarViewGroup;
import edu.hzuapps.androidworks.homeworks.com1314080901139.utils.FixedSpeedScroller;
import edu.hzuapps.androidworks.homeworks.com1314080901139.utils.LogUtil;
import edu.hzuapps.androidworks.homeworks.com1314080901139.utils.ZoomOutPageTransformer;

public class Com1314080901139Activity extends Activity implements ViewPager.OnPageChangeListener, RadarViewGroup.IRadarClickListener {

private CustomViewPager viewPager;
private RelativeLayout ryContainer;
private RadarViewGroup radarViewGroup;
private int[] mImgs = {R.drawable.len, R.drawable.leo, R.drawable.lep,
R.drawable.leq, R.drawable.ler, R.drawable.les, R.drawable.mln, R.drawable.mmz, R.drawable.mna,
R.drawable.mnj, R.drawable.leo, R.drawable.leq, R.drawable.les, R.drawable.lep};
private String[] mNames = {"ImmortalZ", "唐马儒", "王尼玛", "张全蛋", "蛋花", "王大锤", "叫兽", "哆啦A梦"};
private int mPosition;
private FixedSpeedScroller scroller;
private SparseArray<Info> mDatas = new SparseArray<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_com1314080901139);
initView();
initData();

/**
* 将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);
}

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);
}

/**
* 设置ViewPager切换速度
*
* @param duration
*/
private void setViewPagerSpeed(int duration) {
try {
Field field = ViewPager.class.getDeclaredField("mScroller");
field.setAccessible(true);
scroller = new FixedSpeedScroller(Com1314080901139Activity.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);
LogUtil.m("当前位置 " + mPosition);
LogUtil.m("速度 " + viewPager.getSpeed());
//当手指左滑速度大于2000时viewpager右滑(注意是item+2)
if (viewPager.getSpeed() < -1800) {

viewPager.setCurrentItem(mPosition + 2);
LogUtil.m("位置 " + mPosition);
viewPager.setSpeed(0);
} else if (viewPager.getSpeed() > 1800 && mPosition > 0) {
//当手指右滑速度大于2000时viewpager左滑(注意item-1即可)
viewPager.setCurrentItem(mPosition - 1);
LogUtil.m("位置 " + 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(Com1314080901139Activity.this).inflate(R.layout.viewpager_layout, null);
ImageView ivPortrait = (ImageView) 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);
tvName.setText(info.getName());
tvDistance.setText(info.getDistance() + "km");
ivPortrait.setImageResource(info.getPortraitId());
if (info.getSex()) {
ivSex.setImageResource(R.drawable.com139girl);
} else {
ivSex.setImageResource(R.drawable.com139boy);
}
ivPortrait.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Com1314080901139Activity.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);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package edu.hzuapps.androidworks.homeworks.com1314080901139.been;

/**
* Created by wenlangchu on 16/5/12.
*/
public class Info {
private int portraitId; //头像id
private String name; //名字
private String age; //年龄
private boolean sex; //false为男,true为女
private float distance; //距离

public int getPortraitId() {
return portraitId;
}

public void setPortraitId(int portraitId) {
this.portraitId = portraitId;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public float getDistance() {
return distance;
}

public void setDistance(float distance) {
this.distance = distance;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public boolean getSex() {
return sex;
}

public void setSex(boolean sex) {
this.sex = sex;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package edu.hzuapps.androidworks.homeworks.com1314080901139.custom;

/**
* Created by wenlangchu on 16/5/12.
*/

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import edu.hzuapps.androidworks.homeworks.com1314080901139.R;
import edu.hzuapps.androidworks.homeworks.com1314080901139.utils.DisplayUtils;

public class CircleView extends View {
private Paint mPaint;
private Bitmap mBitmap;
private float radius = DisplayUtils.dp2px(getContext(),9);//半径
private float disX;//位置X
private float disY;//位置Y
private float angle;//旋转的角度
private float proportion;//根据远近距离的不同计算得到的应该占的半径比例

public float getProportion() {
return proportion;
}

public void setProportion(float proportion) {
this.proportion = proportion;
}

public float getAngle() {
return angle;
}

public void setAngle(float angle) {
this.angle = angle;
}

public float getDisX() {
return disX;
}

public void setDisX(float disX) {
this.disX = disX;
}

public float getDisY() {
return disY;
}

public void setDisY(float disY) {
this.disY = disY;
}

public CircleView(Context context) {
this(context, null);
}

public CircleView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

private void init() {
mPaint = new Paint();
mPaint.setColor(getResources().getColor(R.color.bg_color_pink));
mPaint.setAntiAlias(true);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(measureSize(widthMeasureSpec), measureSize(heightMeasureSpec));
}

private int measureSize(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = DisplayUtils.dp2px(getContext(),18);
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}


@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(radius, radius, radius, mPaint);
if (mBitmap != null) {
canvas.drawBitmap(mBitmap, null, new Rect(0, 0, 2 * (int) radius, 2 * (int) radius), mPaint);
}
}

public void setPaintColor(int resId) {
mPaint.setColor(resId);
invalidate();
}

public void setPortraitIcon(int resId) {
mBitmap = BitmapFactory.decodeResource(getResources(), resId);
invalidate();
}
public void clearPortaitIcon(){
mBitmap = null;
invalidate();
}
}
Loading

0 comments on commit f1ae481

Please sign in to comment.