Skip to content

Commit

Permalink
optimize homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
wsj1024 committed Jul 23, 2021
1 parent 3c2cadb commit d4e6927
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ class WeatherFragment : BaseVmFragment<FragmentWeatherBinding, WeatherViewModel>

private lateinit var todayBriefInfoBinding: LayoutTodayBriefInfoBinding

// private lateinit var todayDetailBinding: LayoutTodayDetailBinding

private lateinit var forecastHourlyBinding: LayoutForecastHourlyBinding

private lateinit var forecast15dBinding: LayoutForecast15dBinding
Expand Down Expand Up @@ -120,8 +118,6 @@ class WeatherFragment : BaseVmFragment<FragmentWeatherBinding, WeatherViewModel>

forecast15dBinding = LayoutForecast15dBinding.bind(mBinding.root)

// todayDetailBinding = LayoutTodayDetailBinding.bind(mBinding.root)

sunMoonBinding = LayoutSunMoonBinding.bind(mBinding.root)

airQualityBinding = LayoutAirQualityBinding.bind(mBinding.root)
Expand Down Expand Up @@ -207,7 +203,6 @@ class WeatherFragment : BaseVmFragment<FragmentWeatherBinding, WeatherViewModel>
todayBriefInfoBinding.tvWindScale.text = now.windDir + now.windScale + ""
todayBriefInfoBinding.tvPressure.text = now.pressure + "hpa"


}

/**
Expand All @@ -231,7 +226,7 @@ class WeatherFragment : BaseVmFragment<FragmentWeatherBinding, WeatherViewModel>
mForecastAdapter3d?.notifyDataSetChanged()
var min = forecastBase.tempMin.toInt()
var max = forecastBase.tempMax.toInt()
dailyForecast.forEach {
mForecastList.forEach {
min = Math.min(it.tempMin.toInt(), min)
max = Math.max(it.tempMax.toInt(), max)
}
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/me/wsj/fengyun/widget/TempChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import me.wsj.fengyun.R;
import me.wsj.fengyun.bean.Daily;
import per.wsj.commonlib.utils.DisplayUtil;
import per.wsj.commonlib.utils.LogUtil;

public class TempChart extends View {

Expand Down Expand Up @@ -76,6 +77,7 @@ private void init() {


public void setData(int minTemp, int maxTemp, Daily prev, Daily current, Daily next) {
// LogUtil.e("min: " + minTemp + " max:" + maxTemp + " prev:" + prev + " curr: " + current + " next: " + next);
this.minTemp = minTemp;
this.maxTemp = maxTemp;
this.lowTemp = Integer.parseInt(current.getTempMin());
Expand All @@ -91,13 +93,17 @@ public void setData(int minTemp, int maxTemp, Daily prev, Daily current, Daily n
highTextWidth = (int) mTextPaint.measureText(highText);

tempDiff = maxTemp - minTemp;

if (usableHeight != 0) {
density = usableHeight / (float) tempDiff;
}
}

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

mHalfWidth = getMeasuredWidth()/2f;
mHalfWidth = getMeasuredWidth() / 2f;
mHeight = getMeasuredHeight();

usableHeight = (int) (mHeight - topBottom * 2 - textHeight * 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import me.wsj.lib.utils.IconUtils;
import me.wsj.fengyun.utils.WeatherUtil;
import per.wsj.commonlib.utils.DisplayUtil;
import per.wsj.commonlib.utils.LogUtil;

import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -54,7 +55,7 @@ public class HourlyForecastView extends View implements ScrollWatcher {
private Paint bitmapPaint;

//文本的大小
private int textSize;
private int textHeight;

//数据
private List<Hourly> hourlyWeatherList;
Expand Down Expand Up @@ -91,12 +92,13 @@ public class HourlyForecastView extends View implements ScrollWatcher {
private int paddingL = 0;
private int paddingT = 0;
private int paddingR = 0;
private int paddingB = 0;

private int mScrollX = 0;
private float baseLineHeight;
private Paint paint1;

private static int ITEM_SIZE = 24;


public HourlyForecastView(Context context) {
this(context, null);
Expand Down Expand Up @@ -125,11 +127,8 @@ private void init(Context context) {
initPaint();
}

private static int ITEM_SIZE = 24;


public void initData(List<Hourly> weatherData) {

hourlyWeatherList = weatherData;
int size = weatherData.size();
ITEM_SIZE = size;
Expand All @@ -140,7 +139,6 @@ public void initData(List<Hourly> weatherData) {
Hourly hourlyBase;
String lastText = "";


int idx = 0;
while (iterator.hasNext()) {
hourlyBase = (Hourly) iterator.next();
Expand Down Expand Up @@ -173,8 +171,6 @@ private void initDefValue() {
paddingL = DisplayUtil.dip2px(mContext, 10);
paddingR = DisplayUtil.dip2px(mContext, 15);

textSize = DisplayUtil.sp2px(mContext, 12);

bitmapHeight = 1 / 2f * (2 * defHeightPixel - lowestTempHeight) + DisplayUtil.dip2px(mContext, 2);//- 给文字留地方
bitmapXY = 18;
}
Expand Down Expand Up @@ -205,13 +201,16 @@ private void initPaint() {
dashPaint.setStrokeWidth(DisplayUtil.dip2px(mContext, 1));
dashPaint.setStyle(Paint.Style.STROKE);

int textSize = DisplayUtil.sp2px(mContext, 12);
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(textSize);
textPaint.setColor(mContext.getResources().getColor(R.color.search_light_un_color));

textHeight = (int) (textPaint.getFontMetrics().bottom - textPaint.getFontMetrics().top);

textLinePaint = new TextPaint();
textLinePaint.setTextSize(DisplayUtil.sp2px(getContext(), 12));
textLinePaint.setTextSize(textSize);
textLinePaint.setAntiAlias(true);
textLinePaint.setColor(mContext.getResources().getColor(R.color.black));

Expand All @@ -229,46 +228,21 @@ private void initPaint() {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//当设置的padding值小于默认值是设置为默认值
paddingT = DisplayUtil.dip2px(mContext, 20);
paddingL = DisplayUtil.dip2px(mContext, 10);
paddingR = DisplayUtil.dip2px(mContext, 15);
paddingB = Math.max(paddingB, getPaddingBottom());

//获取测量模式
//注意 HorizontalScrollView的子View 在没有明确指定dp值的情况下 widthMode总是MeasureSpec.UNSPECIFIED
//同理 ScrollView的子View的heightMode
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);

//获取测量大小
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);


if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
mWidth = widthSize + paddingL + paddingR;
mHeight = heightSize;
}

//如果为wrap_content 那么View大小为默认值
if (widthMode == MeasureSpec.UNSPECIFIED && heightMode == MeasureSpec.AT_MOST) {
mWidth = defWidthPixel + paddingL + paddingR;
mHeight = defHeightPixel + paddingT + paddingB;
}
mWidth = defWidthPixel + paddingL + paddingR;
mHeight = heightSize;

//设置视图的大小
setMeasuredDimension(mWidth, mHeight);

}


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
initDefValue();
initPaint();

if (hourlyWeatherList != null && hourlyWeatherList.size() != 0) {
drawLines(canvas);
drawBitmaps(canvas);
Expand Down Expand Up @@ -300,7 +274,6 @@ private void drawTemp(Canvas canvas) {
}

private void drawBitmaps(Canvas canvas) {

int scrollX = mScrollX;
boolean leftHide;
boolean rightHide;
Expand Down Expand Up @@ -353,10 +326,8 @@ private void drawBitmaps(Canvas canvas) {

assert bd != null;
Bitmap bitmap = WeatherUtil.bitmapResize(bd.getBitmap(), DisplayUtil.dip2px(mContext, bitmapXY), DisplayUtil.dip2px(mContext, bitmapXY));
// Bitmap bitmap = DisplayUtil.bitmapResize(bd.getBitmap(),
// DisplayUtil.dip2px(mContext, bitmapXY), DisplayUtil.dip2px(mContext, bitmapXY));

//越界判断
// 越界判断
if (drawPoint >= right - bitmap.getWidth() / 2f) {
drawPoint = right - bitmap.getWidth() / 2f;
}
Expand All @@ -365,10 +336,6 @@ private void drawBitmaps(Canvas canvas) {
}

drawBitmap(canvas, bitmap, drawPoint, bitmapHeight);
// String text = hourlyWeatherList.get(dashLineList.get(i)).getCond_txt();
// textPaint.setTextSize(DisplayUtil.sp2px(mContext, 8));
// canvas.drawText(text, drawPoint, bitmapHeight + bitmap.getHeight() + 100 / 3f, textPaint);

}

}
Expand All @@ -382,7 +349,7 @@ private void drawBitmap(Canvas canvas, Bitmap bitmap, float left, float top) {

private void drawLines(Canvas canvas) {
//底部的线的高度 高度为控件高度减去text高度的1.5倍
baseLineHeight = mHeight - 1.5f * textSize;
baseLineHeight = mHeight - 1.5f * textHeight;
Path path = new Path();
List<Float> dashWidth = new ArrayList<>();
List<Float> dashHeight = new ArrayList<>();
Expand Down Expand Up @@ -516,14 +483,14 @@ private void drawLines(Canvas canvas) {
} else {
textPaint.setTextAlign(Paint.Align.CENTER);
}
canvas.drawText(time.substring(time.length() - 11, time.length() - 6), w, baseLineHeight + textSize + DisplayUtil.dip2px(mContext, 3), textPaint);
canvas.drawText(time.substring(time.length() - 11, time.length() - 6), w, baseLineHeight + textHeight + DisplayUtil.dip2px(mContext, 3) , textPaint);
}
} else {
textPaint.setTextAlign(Paint.Align.CENTER);
if (i == 0) {
canvas.drawText(mContext.getString(R.string.now), w, baseLineHeight + textSize + DisplayUtil.dip2px(mContext, 3), textPaint);
canvas.drawText(mContext.getString(R.string.now), w, baseLineHeight + textHeight + DisplayUtil.dip2px(mContext, 3), textPaint);
} else {
canvas.drawText(time.substring(time.length() - 11, time.length() - 6), w, baseLineHeight + textSize + DisplayUtil.dip2px(mContext, 3), textPaint);
canvas.drawText(time.substring(time.length() - 11, time.length() - 6), w, baseLineHeight + textHeight + DisplayUtil.dip2px(mContext, 3), textPaint);
}
}
}
Expand Down
42 changes: 18 additions & 24 deletions app/src/main/java/me/wsj/fengyun/widget/skyview/SunView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextUtils;
Expand All @@ -18,21 +17,20 @@
import java.text.DecimalFormat;

import me.wsj.fengyun.R;
import me.wsj.fengyun.utils.ContentUtil;
import me.wsj.fengyun.utils.WeatherUtil;
import per.wsj.commonlib.utils.DisplayUtil;
import per.wsj.commonlib.utils.LogUtil;

public class SunView extends View {

/**
* view宽度
* view宽度/2
*/
private int mWidth;
private int mHalfWidth;
/**
* 离顶部的高度
*/
private int marginTop = 30;
private int marginTop;
private int mCircleColor; //圆弧颜色
private int mFontColor; //字体颜色
private int mRadius; //圆的半径
Expand Down Expand Up @@ -101,7 +99,7 @@ public SunView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)

private void initView(Context context, @Nullable AttributeSet attrs) {
mContext = context;
marginTop = DisplayUtil.dip2px(context, 20);
marginTop = DisplayUtil.dip2px(context, 12);

TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.SunAnimationView);
mCircleColor = type.getColor(R.styleable.SunAnimationView_sun_circle_color, getContext().getResources().getColor(R.color.sun_line_color));
Expand Down Expand Up @@ -192,13 +190,17 @@ public void setTimes(String startTime, String endTime, String currentTime) {
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

mWidth = getMeasuredWidth();
mHalfWidth = getMeasuredWidth() >> 1;
// start x of sun
positionX = (mWidth >> 1) - mRadius - DisplayUtil.dip2px(mContext, 9);
positionX = mHalfWidth - mRadius - DisplayUtil.dip2px(mContext, 9);
// start y of sun
positionY = mRadius + marginTop - (mSunIcon.getHeight() >> 1);

mRectF.set((mWidth >> 1) - mRadius, marginTop, (mWidth >> 1) + mRadius, mRadius * 2 + marginTop);
mRectF.set(mHalfWidth - mRadius, marginTop, mHalfWidth + mRadius, mRadius * 2 + marginTop);

int height = mHalfWidth + marginTop + DisplayUtil.dip2px(mContext, 30);

setMeasuredDimension(getMeasuredWidth(), height);
}

@Override
Expand All @@ -209,14 +211,14 @@ protected void onDraw(Canvas canvas) {
canvas.drawArc(mRectF, 180, 180, false, mCirclePaint);

// step2:draw bottom line
canvas.drawLine(mWidth / 2 - mRadius - lineBias, mRadius + marginTop, mWidth / 2 + mRadius + lineBias, mRadius + marginTop, mLinePaint);
canvas.drawLine(mHalfWidth - mRadius - lineBias, mRadius + marginTop, mHalfWidth + mRadius + lineBias, mRadius + marginTop, mLinePaint);

// step3:draw text
drawText(canvas);

// step4:draw sun/moon
canvas.save();
canvas.rotate(mCurrentAngle, mWidth / 2, mRadius + marginTop);
canvas.rotate(mCurrentAngle, mHalfWidth, mRadius + marginTop);
canvas.drawBitmap(mSunIcon, positionX, positionY, mPathPaint);
canvas.restore();
}
Expand All @@ -236,11 +238,12 @@ private void drawText(Canvas canvas) {
sunrise = "月出";
sunset = "月落";
}
int dp8 = DisplayUtil.dip2px(mContext, 8);

canvas.drawText(sunrise, mWidth / 2 - mRadius + DisplayUtil.dip2px(mContext, 8), mRadius + DisplayUtil.dip2px(mContext, 16) + marginTop, mTextPaint);
canvas.drawText(startTime, mWidth / 2 - mRadius + DisplayUtil.dip2px(mContext, 8), mRadius + DisplayUtil.dip2px(mContext, 32) + marginTop, mTimePaint);
canvas.drawText(sunset, mWidth / 2 + mRadius - DisplayUtil.dip2px(mContext, 8), mRadius + DisplayUtil.dip2px(mContext, 16) + marginTop, mTextPaint);
canvas.drawText(endTime, mWidth / 2 + mRadius - DisplayUtil.dip2px(mContext, 8), mRadius + DisplayUtil.dip2px(mContext, 32) + marginTop, mTimePaint);
canvas.drawText(sunrise, mHalfWidth - mRadius + dp8, mRadius + DisplayUtil.dip2px(mContext, 20) + marginTop, mTextPaint);
canvas.drawText(startTime, mHalfWidth - mRadius + dp8, mRadius + DisplayUtil.dip2px(mContext, 36) + marginTop, mTimePaint);
canvas.drawText(sunset, mHalfWidth + mRadius - dp8, mRadius + DisplayUtil.dip2px(mContext, 20) + marginTop, mTextPaint);
canvas.drawText(endTime, mHalfWidth + mRadius - dp8, mRadius + DisplayUtil.dip2px(mContext, 36) + marginTop, mTimePaint);
}

/**
Expand Down Expand Up @@ -359,17 +362,8 @@ private void setAnimation(float startAngle, float currentAngle, int duration) {
sunAnimator.addUpdateListener(animation -> {
//每次要绘制的圆弧角度
mCurrentAngle = (float) animation.getAnimatedValue();
// LogUtil.e("mCurrentAngle: " + mCurrentAngle);
invalidate();
// invalidateView(mCurrentAngle);
});
sunAnimator.start();
}

private void invalidateView(float angle) {
//绘制太阳的x坐标和y坐标
positionX = mWidth / 2 - (float) (mRadius * Math.cos((angle) * Math.PI / 180)) - DisplayUtil.dip2px(mContext, 8);
positionY = mRadius - (float) (mRadius * Math.sin((angle) * Math.PI / 180)) + DisplayUtil.dip2px(mContext, 16);
invalidate();
}
}
Loading

0 comments on commit d4e6927

Please sign in to comment.