HehuaView.java
3.93 KB
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
134
135
136
137
138
package com.lotus.town.widget;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lotus.town.R;
import com.lotus.town.home.HomeItem;
import com.lotus.town.home.ItemType;
import com.lotus.town.utils.Utils;
public class HehuaView extends LinearLayout {
private int mValidHeight;
private int mValidWidth;
private RelativeLayout mHehuaContainerRl;
private Rect mValidRect = null;
private ImageView mHehuaBottomIv;
private ImageView mHehuaTopIv;
private TextView mHehuaTextTv;
private ItemType mItemType;
public enum HitPosition {
LEFT,
RIGHT,
NULL;
}
public HehuaView(Context context) {
this(context, null);
}
public HehuaView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public HehuaView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater.from(context).inflate(R.layout.view_hehua, this);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mHehuaContainerRl = findViewById(R.id.hehua_container);
mHehuaBottomIv = findViewById(R.id.hehua_bottom);
mHehuaTopIv = findViewById(R.id.hehua_top);
mHehuaTextTv = findViewById(R.id.hehua_text);
}
public void doMeasure() {
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
mHehuaBottomIv.measure(w, h);
mValidWidth = getValidDistrictWidth();
mValidHeight = getValidDistrictHeight();
}
public void setItemType(HomeItem homeItem) {
switch (homeItem.getType()) {
case 0:
mItemType = ItemType.BOMB_ITEM;
mHehuaBottomIv.setBackgroundResource(R.drawable.red);
mHehuaTopIv.setBackgroundResource(R.drawable.pong);
mHehuaTextTv.setText("爆炸");
break;
case 1:
mItemType = ItemType.LUCK_NEXT;
mHehuaBottomIv.setBackgroundResource(R.drawable.yellow);
mHehuaTopIv.setBackgroundResource(R.drawable.pig);
mHehuaTextTv.setText("好运");
break;
case 2:
mItemType = ItemType.EARN_MONEY;
mHehuaBottomIv.setBackgroundResource(R.drawable.green_hehua);
mHehuaTopIv.setBackgroundResource(R.drawable.money);
mHehuaTextTv.setText(homeItem.getMoney() + "元");
break;
case 3:
mItemType = ItemType.POWER;
mHehuaBottomIv.setBackgroundResource(R.drawable.green_deep);
mHehuaTopIv.setBackgroundResource(R.drawable.power_water);
mHehuaTextTv.setText(Utils.doubleToInt(homeItem.getMoney()) + "克");
break;
}
}
public int getValidDistrictWidth() {
return mHehuaBottomIv.getMeasuredWidth();
}
public int getValidDistrictHeight() {
return mHehuaBottomIv.getMeasuredHeight();
}
public void getVaildLocationInWindow(int[] position) {
mHehuaBottomIv.getLocationInWindow(position);
}
public Rect getValidRect(int width, int height) {
if (mValidRect == null) {
int[] position = new int[2];
getVaildLocationInWindow(position);
mValidRect = new Rect(position[0],position[1],position[0]+mValidWidth,position[1]+mValidHeight);
}
return mValidRect;
}
public Rect getValidRect() {
return mValidRect;
}
public HitPosition getHitPosition(Rect src) {
Rect dst = getValidRect();
HitPosition position = HitPosition.NULL;
if (src.left < dst.left) {
if ((dst.left - src.left) > 10) {
position = HitPosition.LEFT;
}
} else if (src.right > dst.right) {
if ((src.right - dst.right) > 10) {
position = HitPosition.RIGHT;
}
}
if (!src.intersect(dst)) {
position = HitPosition.NULL;
}
return position;
}
}