This repository has been archived by the owner on Sep 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
/
FilterMenuDrawable.java
120 lines (101 loc) · 3.55 KB
/
FilterMenuDrawable.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
package com.linroid.filtermenu.library;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
/**
* Created by linroid on 15/3/10.
*/
public class FilterMenuDrawable extends Drawable {
private Context ctx;
private Paint paint;
private IconState state = IconState.COLLAPSED;
private int radius;
private int lineWidth = 8;
private float expandProgress = 0;
public FilterMenuDrawable(Context ctx, int color, int radius) {
this.ctx = ctx;
this.radius = radius;
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(color);
paint.setStrokeWidth(lineWidth);
}
public enum IconState{
COLLAPSED,
EXPANDED
}
public float getExpandProgress() {
return expandProgress;
}
public void setExpandProgress(float expandProgress) {
this.expandProgress = expandProgress;
invalidateSelf();
}
@Override
public int getIntrinsicWidth() {
return (int) (radius*0.8f);
}
@Override
public int getIntrinsicHeight() {
return (int) (radius*0.8f);
}
@Override
public void draw(Canvas canvas) {
//draw three line
// paint.setColor(Color.BLACK);
// canvas.drawRect(getBounds(), paint);
// paint.setColor(Color.WHITE);
if(expandProgress<=0.5f){
drawTopLine(canvas, expandProgress);
drawMiddleLine(canvas, expandProgress);
drawBottomLine(canvas, expandProgress);
// draw cancel
}else{
drawTopLeftLine(canvas, expandProgress);
drawBottomLeftLine(canvas, expandProgress);
}
}
private void drawBottomLeftLine(Canvas canvas, float progress) {
int ly = (int) (getBounds().bottom-getIntrinsicHeight()*progress);
int ry = (int) (getBounds().top+ getIntrinsicHeight()*progress);
canvas.drawLine(getBounds().left, ly, getBounds().right, ry, paint);
}
private void drawTopLeftLine(Canvas canvas, float progress) {
int ry = (int) (getBounds().bottom-getIntrinsicHeight()*progress);
int ly = (int) (getBounds().top+ getIntrinsicHeight()*progress);
canvas.drawLine(getBounds().left, ly, getBounds().right, ry, paint);
}
private void drawTopLine(Canvas canvas, float progress) {
int y = getBounds().top + (int) (getIntrinsicHeight()* progress) + lineWidth;
canvas.drawLine(getBounds().left, y, getBounds().left+getIntrinsicWidth(), y, paint);
}
private void drawMiddleLine(Canvas canvas, float progress) {
int y = getBounds().top + getIntrinsicHeight() / 2;
int len = getIntrinsicWidth() /2;
int centerX = getBounds().centerX();
canvas.drawLine(centerX-len/2, y, centerX+len/2, y, paint);
}
private void drawBottomLine(Canvas canvas, float progress) {
int y = getBounds().top + (int) (getIntrinsicHeight() * (1-progress)) - lineWidth;
int len = getIntrinsicWidth() /4;
int centerX = getBounds().centerX();
canvas.drawLine(centerX-len/2, y, centerX+len/2, y, paint);
}
@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}