Skip to content

Commit

Permalink
Merge pull request #10 from wuyalun198612/develop
Browse files Browse the repository at this point in the history
优化电池控件
  • Loading branch information
uni7corn authored Dec 9, 2017
2 parents f0c4f23 + 1c2c5be commit 464f5fb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
@Override
public void onClick(View v) {
power++;
mBatteryView.setFullBattery(power);
mBatteryView.setAh(power);
}
});

findViewById(R.id.bt_reduce).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
power--;
mBatteryView.setFullBattery(power);
mBatteryView.setAh(power);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

Expand Down Expand Up @@ -71,7 +68,6 @@ public class BatteryView extends View {
private RectF mCapRectF;

private float mProgress = 0.0f;
private int mContentWidth;
private float mBatteryAhWidth;


Expand Down Expand Up @@ -102,15 +98,17 @@ private void init(Context context, AttributeSet attrs) {

mProgress = (float) mCurrentBattery / mFullBattery;

Log.e(TAG, "init: ------>" + mProgress + " currentBattery=" + mCurrentBattery + " fullBattery=" + mFullBattery);
this.mTopFullHalfColor = a.getColor(R.styleable.BatteryView_top_full_half_color,
0xFF7EA5F4);
this.mTopLessHalfColor = a.getColor(R.styleable.BatteryView_top_less_half_color,
0xFFDB514F);

this.mTopFullHalfColor = a.getColor(R.styleable.BatteryView_top_full_half_color, Color.YELLOW);
this.mTopLessHalfColor = a.getColor(R.styleable.BatteryView_top_less_half_color, Color.YELLOW);
this.mBottomFullHalfColor = a.getColor(R.styleable
.BatteryView_bottom_full_half_color, 0xFF6293F5);
this.mBottomLessHalfColor = a.getColor(R.styleable
.BatteryView_bottom_less_half_color, 0xFFCC3835);

this.mBottomFullHalfColor = a.getColor(R.styleable.BatteryView_bottom_full_half_color, Color.BLUE);
this.mBottomLessHalfColor = a.getColor(R.styleable.BatteryView_bottom_less_half_color, Color.BLUE);

this.mBatteryCircleColor = a.getColor(R.styleable.BatteryView_border_color, Color.BLUE);
this.mBatteryCircleColor = a.getColor(R.styleable.BatteryView_border_color, 0xFF7D8FB3);
this.mBatteryBorderWidth = a.getDimension(R.styleable.BatteryView_border_width,
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1.0f, getResources().getDisplayMetrics()));

Expand Down Expand Up @@ -184,9 +182,7 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
int contentWidth = w - paddingLeft - paddingRight;
int contentHeight = h - paddingTop - paddingBottom;

this.mContentWidth = contentWidth;

int centerX = contentWidth >> 1;
// int centerX = contentWidth >> 1;
int centerY = contentHeight >> 1;

this.mCenterY = centerY;
Expand Down Expand Up @@ -271,7 +267,6 @@ protected void onDraw(Canvas canvas) {

private void clipPath(Canvas canvas) {
int save = canvas.save();
Log.e(TAG, "clipPath: -------->" + mProgress);

canvas.clipPath(mClipPath);
drawBatteryTopHalf(canvas);
Expand All @@ -287,20 +282,20 @@ private void init2(Canvas canvas) {
canvas.restoreToCount(saveLayerAlpha);
}

private void init1(Canvas canvas) {
Paint batteryCirclePaint = this.mBatteryBorderPaint;
int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null,
Canvas.ALL_SAVE_FLAG);
batteryCirclePaint.setXfermode(null);
batteryCirclePaint.setColor(0xFFFFFFFF);
drawBatteryCap(canvas);
drawBatteryBorder(canvas);
batteryCirclePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
batteryCirclePaint.setColor(0x7F7D8FB3);
batteryCirclePaint.setStyle(Paint.Style.FILL);
canvas.drawRect(new RectF(0, 0, getWidth(), getHeight()), batteryCirclePaint);
canvas.restoreToCount(sc);
}
// private void init1(Canvas canvas) {
// Paint batteryCirclePaint = this.mBatteryBorderPaint;
// int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null,
// Canvas.ALL_SAVE_FLAG);
// batteryCirclePaint.setXfermode(null);
// batteryCirclePaint.setColor(0xFFFFFFFF);
// drawBatteryCap(canvas);
// drawBatteryBorder(canvas);
// batteryCirclePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
// batteryCirclePaint.setColor(0x7F7D8FB3);
// batteryCirclePaint.setStyle(Paint.Style.FILL);
// canvas.drawRect(new RectF(0, 0, getWidth(), getHeight()), batteryCirclePaint);
// canvas.restoreToCount(sc);
// }

private void drawBatteryBottomHalf(Canvas canvas) {
Paint batteryBottomAhPaint = this.mBatteryBottomAhPaint;
Expand Down Expand Up @@ -334,7 +329,6 @@ private void drawBatteryTopHalf(Canvas canvas) {

mTopAhRectF.right = mTopAhRectF.left + mBatteryAhWidth * mProgress;
canvas.drawRect(mTopAhRectF, batteryTopAhPaint);
Log.e(TAG, "drawBatteryTopHalf: ------->" + mTopAhRectF.toString());
}

private void drawPercentText(Canvas canvas) {
Expand Down Expand Up @@ -364,7 +358,7 @@ private String FormatTextPower(float power) {
}


public void setFullBattery(int battery) {
public void setAh(int battery) {
if (battery < 0 || battery > 100) return;
this.mCurrentBattery = battery;
mProgress = (float) battery / mFullBattery;
Expand Down
13 changes: 6 additions & 7 deletions app/src/main/res/layout/activity_main_battery.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="@color/schedule_font_color"
app:battery_circle_color="@color/battery_circle_color"
app:battery_circle_width="3dp"
app:battery_radius="4dp"
app:battery_text_size="18sp"
app:ah="30"
app:ah_radius="4dp"
app:border_color="@color/battery_circle_color"
app:border_radius="8dp"
app:border_width="3dp"
app:bottom_full_half_color="@color/battery_bottom_full_color"
app:bottom_less_half_color="@color/battery_bottom_less_color"
app:current_battery="30"
app:full_battery="100"
app:full_ah="100"
app:text_color="@color/white"
app:text_size="18sp"
app:top_full_half_color="@color/battery_top_full_color"
app:top_less_half_color="@color/battery_top_less_color" />

Expand Down

0 comments on commit 464f5fb

Please sign in to comment.