/
WashingMachineView.java
133 lines (99 loc) · 4.83 KB
/
WashingMachineView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.naman14.washingmachineview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import com.naman14.washingmachineview.waterwave.WaterWave;
/**
* Created by naman on 28/08/15.
*/
public class WashingMachineView extends LinearLayout {
//linear layout consisting of three views-topview,machineview and bottomview
private int mTopviewHeight;
private int mMiddleviewHeight;
private int mBottomviewHeight;
private int mMachineColor;
public WashingMachineView(Context context, AttributeSet attrs) {
super(context, attrs);
final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.MachineView);
mTopviewHeight=attributes.getDimensionPixelSize(R.styleable.MachineView_top_view_height,getDimensionInPixel(50));
mBottomviewHeight=attributes.getDimensionPixelSize(R.styleable.MachineView_bottom_view_height,getDimensionInPixel(20));
mMiddleviewHeight=attributes.getDimensionPixelSize(R.styleable.MachineView_middle_view_height,getDimensionInPixel(240));
mMachineColor=attributes.getColor(R.styleable.MachineView_machineColor,Color.BLACK);
attributes.recycle();
setOrientation(VERTICAL);
//add the views
addView(new TopView(context, attrs));
addView(new MachineView(context, attrs));
addView(new BottomView(context, attrs));
}
public class TopView extends View {
Paint paint = new Paint();
public TopView(Context context, AttributeSet attrs) {
super(context, attrs);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, mTopviewHeight);
params.setMargins(0, 0, 0, 10);
setLayoutParams(params);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//background of topview
Rect rectBackground = new Rect(0, 0, getWidth(), getHeight());
paint.setColor(mMachineColor);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
canvas.drawRect(rectBackground, paint);
//the rectangular strip on topview
Rect rectWhite = new Rect(getDimensionInPixel(20), getHeight() / 2 - getDimensionInPixel(4), getDimensionInPixel(60), getHeight() / 2 + getDimensionInPixel(4));
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
canvas.drawRect(rectWhite, paint);
//three dots at the end of topview
canvas.drawCircle(getWidth() - getDimensionInPixel(80), getHeight() / 2, getDimensionInPixel(6), paint);
canvas.drawCircle(getWidth() - getDimensionInPixel(55), getHeight() / 2, getDimensionInPixel(6), paint);
canvas.drawCircle(getWidth() - getDimensionInPixel(30), getHeight() / 2, getDimensionInPixel(4), paint);
}
}
public class BottomView extends View {
Paint paint = new Paint();
public BottomView(Context context, AttributeSet attrs) {
super(context, attrs);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, mBottomviewHeight);
params.setMargins(0, 15, 0, 0);
setLayoutParams(params);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect rect = new Rect(0, 0, getWidth(), getHeight());
paint.setColor(mMachineColor);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
canvas.drawRect(rect, paint);
}
}
//machineview(middle view) extends FrameLayout and has two views-WaterWave and Overlay.
//Overlay consists of a transparent hole from which water waves are visible
public class MachineView extends FrameLayout {
public MachineView(Context context, AttributeSet attrs) {
super(context, attrs);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, mMiddleviewHeight);
setLayoutParams(params);
addView(new WaterWave(context, attrs));
addView(new OverlayView(context, attrs));
}
}
//get all dimensions in dp so that views behaves properly on different screen resolutions
private int getDimensionInPixel(int dp){
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
}