Skip to content

Commit

Permalink
dp2px
Browse files Browse the repository at this point in the history
  • Loading branch information
yannecer committed Jul 2, 2020
1 parent ec0656a commit 643820d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 12 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
25 changes: 25 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions app/src/main/java/com/necer/ncalendar/DensityUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.necer.ncalendar;

import android.content.Context;
import android.util.TypedValue;

/**
* Created by necer on 2020/7/2.
*/
public class DensityUtil {

public static int dp2px(Context context, float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.TypedValue;

import com.necer.entity.CalendarDate;
import com.necer.ncalendar.DensityUtil;
import com.necer.painter.CalendarPainter;
import com.necer.utils.CalendarUtil;
import com.necer.view.CalendarView;
Expand All @@ -32,7 +34,7 @@ public LigaturePainter(Context context) {
mTextPaint = getPaint();
mBgPaint = getPaint();

//mCircleRadius = CalendarUtil.dp2px(context, 20);
mCircleRadius = DensityUtil.dp2px(context, 20);
mBgPaint.setColor(Color.parseColor("#ff7575"));
}

Expand Down Expand Up @@ -129,19 +131,20 @@ private void drawSelectBg(Canvas canvas, RectF rectF, LocalDate localDate, boole

//绘制公历
private void drawSolar(Canvas canvas, RectF rectF, LocalDate date, boolean isSelected, boolean isCurrectMonthOrWeek) {
// mTextPaint.setTextSize(CalendarUtil.dp2px(mContext, 18));
mTextPaint.setTextSize(DensityUtil.dp2px(mContext, 16));
mTextPaint.setColor(isSelected ? Color.WHITE : Color.BLACK);
mTextPaint.setAlpha(isCurrectMonthOrWeek ? 255 : 100);
canvas.drawText(date.getDayOfMonth() + "", rectF.centerX(), rectF.centerY(), mTextPaint);
}

//绘制农历
private void drawLunar(Canvas canvas, RectF rectF, LocalDate date, boolean isSelected, boolean isCurrectMonthOrWeek) {
// mTextPaint.setTextSize(CalendarUtil.dp2px(mContext, 10));
mTextPaint.setTextSize(DensityUtil.dp2px(mContext, 10));
CalendarDate calendarDate = CalendarUtil.getCalendarDate(date);
mTextPaint.setColor(isSelected ? Color.WHITE : Color.GRAY);
mTextPaint.setAlpha(isCurrectMonthOrWeek ? 255 : 100);
// canvas.drawText(calendarDate.lunar.lunarOnDrawStr, rectF.centerX(), rectF.centerY() + CalendarUtil.dp2px(mContext, 12), mTextPaint);
canvas.drawText(calendarDate.lunar.lunarOnDrawStr, rectF.centerX(), rectF.centerY() + DensityUtil.dp2px(mContext, 12), mTextPaint);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.graphics.RectF;
import android.text.TextUtils;

import com.necer.ncalendar.DensityUtil;
import com.necer.painter.CalendarPainter;
import com.necer.utils.CalendarUtil;
import com.necer.view.CalendarView;
Expand All @@ -24,7 +25,7 @@ public StretchPainter(Context context) {
mBgPaint = getPaint();
mTextPaint = getPaint();
mBgPaint.setColor(Color.parseColor("#ff7575"));
// mTextPaint.setTextSize(CalendarUtil.dp2px(context, 10));
mTextPaint.setTextSize(DensityUtil.dp2px(context, 10));
mTextPaint.setColor(Color.parseColor("#000000"));
}

Expand Down
15 changes: 8 additions & 7 deletions app/src/main/java/com/necer/ncalendar/painter/TicketPainter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.text.TextUtils;

import com.necer.calendar.ICalendar;
import com.necer.ncalendar.DensityUtil;
import com.necer.painter.CalendarPainter;
import com.necer.utils.CalendarUtil;
import com.necer.view.CalendarView;
Expand Down Expand Up @@ -45,7 +46,7 @@ public TicketPainter(Context context, ICalendar iCalendar) {
mBgPaint = getPaint();

mBgPaint.setColor(Color.parseColor("#7D7DFF"));
// mCircleRadius = (int) CalendarUtil.dp2px(context, 20);
mCircleRadius = (int) DensityUtil.dp2px(context, 20);

mPriceMap = new HashMap<>();
mHolidayList = new ArrayList<>();
Expand Down Expand Up @@ -110,7 +111,7 @@ private void drawSelectBg(Canvas canvas, RectF rectF, boolean isisSelected, bool

//绘制公历
private void drawSolar(Canvas canvas, RectF rectF, LocalDate localDate, boolean isSelected, boolean isCurrectMonthOrWeek) {
// mTextPaint.setTextSize(CalendarUtil.dp2px(mContext, 18));
mTextPaint.setTextSize(DensityUtil.dp2px(mContext, 16));
mTextPaint.setColor(isSelected ? Color.WHITE : Color.BLACK);
mTextPaint.setAlpha(isCurrectMonthOrWeek ? 255 : 100);
canvas.drawText(localDate.getDayOfMonth() + "", rectF.centerX(), TextUtils.isEmpty(mPriceMap.get(localDate)) ? getBaseLineY(rectF) : rectF.centerY(), mTextPaint);
Expand All @@ -120,26 +121,26 @@ private void drawSolar(Canvas canvas, RectF rectF, LocalDate localDate, boolean
private void drawPrice(Canvas canvas, RectF rectF, LocalDate localDate, boolean isSelected, boolean isCurrectMonthOrWeek) {
String price = mPriceMap.get(localDate);
if (!TextUtils.isEmpty(price)) {
// mTextPaint.setTextSize(CalendarUtil.dp2px(mContext, 10));
mTextPaint.setTextSize(DensityUtil.dp2px(mContext, 10));
mTextPaint.setColor(isSelected ? Color.WHITE : Color.RED);
mTextPaint.setAlpha(isCurrectMonthOrWeek ? 255 : 100);
// canvas.drawText(price, rectF.centerX(), rectF.centerY() + CalendarUtil.dp2px(mContext, 12), mTextPaint);
canvas.drawText(price, rectF.centerX(), rectF.centerY() + DensityUtil.dp2px(mContext, 12), mTextPaint);
}
}


//绘制节假日
private void drawHolidays(Canvas canvas, RectF rectF, LocalDate localDate, boolean isSelected, boolean isCurrectMonthOrWeek) {
// mTextPaint.setTextSize(CalendarUtil.dp2px(mContext, 10));
mTextPaint.setTextSize(DensityUtil.dp2px(mContext, 10));
if (mHolidayList.contains(localDate)) {
mTextPaint.setColor(isSelected ? Color.WHITE : Color.GREEN);
mTextPaint.setAlpha(isCurrectMonthOrWeek ? 255 : 100);
// canvas.drawText("休", rectF.centerX() + CalendarUtil.dp2px(mContext, 10), rectF.centerY() - CalendarUtil.dp2px(mContext, 5), mTextPaint);
canvas.drawText("休", rectF.centerX() + DensityUtil.dp2px(mContext, 10), rectF.centerY() - DensityUtil.dp2px(mContext, 5), mTextPaint);
}
if (mWorkdayList.contains(localDate)) {
mTextPaint.setColor(isSelected ? Color.WHITE : Color.RED);
mTextPaint.setAlpha(isCurrectMonthOrWeek ? 255 : 100);
// canvas.drawText("班", rectF.centerX() + CalendarUtil.dp2px(mContext, 10), rectF.centerY() - CalendarUtil.dp2px(mContext, 5), mTextPaint);
canvas.drawText("班", rectF.centerX() + DensityUtil.dp2px(mContext, 10), rectF.centerY() - DensityUtil.dp2px(mContext, 5), mTextPaint);
}
}

Expand Down

0 comments on commit 643820d

Please sign in to comment.