Skip to content

Commit

Permalink
新生专题就业
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay committed Aug 17, 2017
1 parent a53bb6d commit 4326ed7
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.mredrock.freshmanspecial.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;

/**
* Created by Jay on 2017/8/17.
*/

public class BarGraphView extends View {
private static final int COLOR_FILL = Color.parseColor("#9efcee");
private static final int COLOR_STROKE = Color.parseColor("#6cead5");
private static final int COLOR_TEXT = Color.parseColor("#666666");

private static final int MIN_VALUE = 10;
private static final int MAX_VALUE = 300;
private static final int GAP_VALUE = 10;

private final int DEFAULT_RADIUS = (int) dp2px(80);
private final int STROKE_WIDTH = dp2px(2);

private int mValue;
private int mRadius;
private Paint mFillPaint;
private Paint mStrokePaint;
private Paint mTextPaint;

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

public BarGraphView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
mFillPaint = new Paint();
mFillPaint.setAntiAlias(true);
mFillPaint.setDither(true);
mFillPaint.setColor(COLOR_FILL);
mFillPaint.setStyle(Paint.Style.FILL);

mStrokePaint = new Paint();
mStrokePaint.setAntiAlias(true);
mStrokePaint.setDither(true);
mStrokePaint.setColor(COLOR_STROKE);
mStrokePaint.setStrokeWidth(STROKE_WIDTH);
mStrokePaint.setStyle(Paint.Style.STROKE);

// TODO: 2017/8/17 textPaint
mTextPaint = new Paint();
mTextPaint.setTextSize(dp2px(14));
mTextPaint.setColor(COLOR_TEXT);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
/*int width = 0;
int height = 0;
switch (MeasureSpec.getMode(widthMeasureSpec)) {
case MeasureSpec.UNSPECIFIED:
height = DEFAULT_RADIUS;
mRadius = height / 2;
width = value2px(MAX_VALUE);
break;
case MeasureSpec.AT_MOST:
int leftWidth = MeasureSpec.getSize(widthMeasureSpec);
width = Math.min(leftWidth, value2px(MAX_VALUE, DEFAULT_RADIUS));
mRadius = width / (MAX_VALUE / GAP_VALUE);
height = mRadius * 2;
break;
case MeasureSpec.EXACTLY:
width = MeasureSpec.getSize(widthMeasureSpec);
mRadius = width / (MAX_VALUE / GAP_VALUE);
height = mRadius * 2;
break;
}
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);*/
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mRadius = getMeasuredHeight() / 2;
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF rect = new RectF(0, 0, value2px(mValue), mRadius * 2);
canvas.drawRoundRect(rect, mRadius, mRadius, mFillPaint);
Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
int baseline = (int) ((rect.bottom + rect.top - fontMetrics.bottom - fontMetrics.top) / 2);
canvas.drawText(mValue + "人", rect.right + dp2px(10), baseline, mTextPaint);
}

private int dp2px(int dp) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
}

private int value2px(int value) {
return value2px(value, mRadius);
}

private int value2px(int value, int size) {
if (value <= MIN_VALUE) {
return size * 2;
} else if (value >= MAX_VALUE) {
return size * 2 + size * (MAX_VALUE - MIN_VALUE) / GAP_VALUE;
} else {
return size * 2 + size * (value - MIN_VALUE) / GAP_VALUE;
}
}

public void setValue(int value) {
mValue = value;
postInvalidate();
}

public int getValue() {
return mValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,30 @@ private void setPager() {

private void initPager() {
String[] tabTitles = new String[]{"男女比例", "最难科目", "就业数据"};
Fragment[] fragments = new Fragment[]{new SexRateFragment(), new MostDifficultFragment(), new EmploymentDataFragment()};
final Fragment[] fragments = new Fragment[]{new SexRateFragment(), new MostDifficultFragment(), new EmploymentDataFragment()};
ViewPagerAdapter adapter = new ViewPagerAdapter(this,getSupportFragmentManager(), tabTitles, fragments);
viewPager.setOffscreenPageLimit(2);
viewPager.setOffscreenPageLimit(1);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
if (position == 2) {
EmploymentDataFragment fragment = (EmploymentDataFragment) fragments[2];
fragment.startAnimation();
}
}

@Override
public void onPageScrollStateChanged(int state) {

}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,97 @@
package com.mredrock.freshmanspecial.view.dataFragments;


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.mredrock.freshmanspecial.R;
import com.mredrock.freshmanspecial.view.BarGraphView;

import java.util.ArrayList;
import java.util.List;

/**
* A simple {@link Fragment} subclass.
*/
public class EmploymentDataFragment extends Fragment {
private boolean isAnimation = false;

private String[] mCompanyNames = new String[]{
"腾讯", "华为", "烽火",
"中国铁塔", "海信", "猪八戒",
"中国联通", "深信服", "中国移动",
"中国电信"
};

private int[] mValues = new int[]{20, 22, 23, 40, 35, 65, 194, 25, 177, 123};

private List<BarGraphView> mBars = new ArrayList<>();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View parent = LayoutInflater.from(getActivity())
.inflate(R.layout.special_2017_fragment_employment_data, null, false);
ViewGroup myContainer = (ViewGroup) parent.findViewById(R.id.container);

for (int i = 0; i < mCompanyNames.length; i++) {
ViewGroup itemView = (ViewGroup) LayoutInflater.from(getActivity())
.inflate(R.layout.special_2017_item_fragment_employment_data, null, false);
TextView name = (TextView) itemView.findViewById(R.id.commpany);
name.setText(mCompanyNames[i]);
BarGraphView bar = (BarGraphView) itemView.findViewById(R.id.bar);
bar.setValue(mValues[i]);
mBars.add(bar);
myContainer.addView(itemView);
}
myContainer.getChildAt(0).setPadding(0, dp2px(22), 0, 0);
myContainer.getChildAt(myContainer.getChildCount() - 1)
.setPadding(0, dp2px(30), 0, dp2px(22));
startAnimation();

return parent;
}

public void startAnimation() {
if (isAnimation) {
return;
}
for (BarGraphView bar : mBars) {
ObjectAnimator animator = ObjectAnimator.ofInt(bar, "value", 0, bar.getValue());
animator.setDuration(2000 * bar.getValue() / 194);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
isAnimation = false;
}

@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
isAnimation = true;
}
});
animator.start();
}
}

private int dp2px(int dp) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
}

public void toast(String msg) {
Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

</LinearLayout>
</ScrollView>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingTop="30dp">

<TextView
android:id="@+id/commpany"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="猪八戒"
android:textColor="#000000"
android:textSize="14sp"/>

<com.mredrock.freshmanspecial.view.BarGraphView
android:id="@+id/bar"
android:layout_width="0dp"
android:layout_height="18dp"
android:layout_weight="3.37"/>
</LinearLayout>

0 comments on commit 4326ed7

Please sign in to comment.