Skip to content

Commit 2e70434

Browse files
author
jianbo
committed
first commit.
0 parents  commit 2e70434

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+948
-0
lines changed

.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild
10+
/.gradle
11+
/.idea
12+
/app/src/test
13+
/app/src/androidTest
14+
/*.bat

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
6+
defaultConfig {
7+
applicationId "com.jeanboy.app.luckymonkeypanel"
8+
minSdkVersion 15
9+
targetSdkVersion 25
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
sourceSets { main { res.srcDirs = ['src/main/res', 'src/main/res/drawable-xhdpi'] } }
21+
}
22+
23+
dependencies {
24+
compile fileTree(dir: 'libs', include: ['*.jar'])
25+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
26+
exclude group: 'com.android.support', module: 'support-annotations'
27+
})
28+
compile 'com.android.support:appcompat-v7:25.3.1'
29+
compile 'com.android.support.constraint:constraint-layout:1.0.2'
30+
testCompile 'junit:junit:4.12'
31+
}

app/proguard-rules.pro

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in D:\Develop\Android\SDK/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile

app/src/main/AndroidManifest.xml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.jeanboy.app.luckymonkeypanel">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package com.jeanboy.app.luckymonkeypanel;
2+
3+
import android.content.Context;
4+
import android.support.annotation.AttrRes;
5+
import android.support.annotation.NonNull;
6+
import android.support.annotation.Nullable;
7+
import android.util.AttributeSet;
8+
import android.widget.FrameLayout;
9+
import android.widget.ImageView;
10+
11+
/**
12+
* Created by jeanboy on 2017/4/20.
13+
*/
14+
15+
public class LuckyMonkeyPanelView extends FrameLayout {
16+
17+
18+
private ImageView bg_1;
19+
private ImageView bg_2;
20+
21+
private PanelItemView itemView1, itemView2, itemView3,
22+
itemView4, itemView6,
23+
itemView7, itemView8, itemView9;
24+
25+
private PanelItemView[] itemViewArr = new PanelItemView[8];
26+
private int currentIndex = 0;
27+
private int currentTotal = 0;
28+
private int stayIndex = 0;
29+
30+
private boolean isMarqueeRunning = false;
31+
private boolean isGameRunning = false;
32+
private boolean isTryToStop = false;
33+
34+
private static final int DEFAULT_SPEED = 150;
35+
private static final int MIN_SPEED = 50;
36+
private int currentSpeed = DEFAULT_SPEED;
37+
38+
public LuckyMonkeyPanelView(@NonNull Context context) {
39+
this(context, null);
40+
}
41+
42+
public LuckyMonkeyPanelView(@NonNull Context context, @Nullable AttributeSet attrs) {
43+
this(context, attrs, 0);
44+
}
45+
46+
public LuckyMonkeyPanelView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
47+
super(context, attrs, defStyleAttr);
48+
inflate(context, R.layout.view_lucky_mokey_panel, this);
49+
setupView();
50+
51+
52+
}
53+
54+
@Override
55+
protected void onAttachedToWindow() {
56+
super.onAttachedToWindow();
57+
startMarquee();
58+
}
59+
60+
@Override
61+
protected void onDetachedFromWindow() {
62+
stopMarquee();
63+
isGameRunning = false;
64+
super.onDetachedFromWindow();
65+
}
66+
67+
private void setupView() {
68+
bg_1 = (ImageView) findViewById(R.id.bg_1);
69+
bg_2 = (ImageView) findViewById(R.id.bg_2);
70+
itemView1 = (PanelItemView) findViewById(R.id.item1);
71+
itemView2 = (PanelItemView) findViewById(R.id.item2);
72+
itemView3 = (PanelItemView) findViewById(R.id.item3);
73+
itemView4 = (PanelItemView) findViewById(R.id.item4);
74+
itemView6 = (PanelItemView) findViewById(R.id.item6);
75+
itemView7 = (PanelItemView) findViewById(R.id.item7);
76+
itemView8 = (PanelItemView) findViewById(R.id.item8);
77+
itemView9 = (PanelItemView) findViewById(R.id.item9);
78+
79+
itemViewArr[0] = itemView4;
80+
itemViewArr[1] = itemView1;
81+
itemViewArr[2] = itemView2;
82+
itemViewArr[3] = itemView3;
83+
itemViewArr[4] = itemView6;
84+
itemViewArr[5] = itemView9;
85+
itemViewArr[6] = itemView8;
86+
itemViewArr[7] = itemView7;
87+
}
88+
89+
private void stopMarquee() {
90+
isMarqueeRunning = false;
91+
}
92+
93+
private void startMarquee() {
94+
isMarqueeRunning = true;
95+
new Thread(new Runnable() {
96+
@Override
97+
public void run() {
98+
while (isMarqueeRunning) {
99+
try {
100+
Thread.sleep(250);
101+
} catch (InterruptedException e) {
102+
e.printStackTrace();
103+
}
104+
105+
post(new Runnable() {
106+
@Override
107+
public void run() {
108+
if (bg_1 != null && bg_2 != null) {
109+
if (VISIBLE == bg_1.getVisibility()) {
110+
bg_1.setVisibility(GONE);
111+
bg_2.setVisibility(VISIBLE);
112+
} else {
113+
bg_1.setVisibility(VISIBLE);
114+
bg_2.setVisibility(GONE);
115+
}
116+
}
117+
}
118+
});
119+
}
120+
}
121+
}).start();
122+
}
123+
124+
private long getInterruptTime() {
125+
currentTotal++;
126+
if (isTryToStop) {
127+
currentSpeed += 10;
128+
if (currentSpeed > DEFAULT_SPEED) {
129+
currentSpeed = DEFAULT_SPEED;
130+
}
131+
} else {
132+
if (currentTotal / itemViewArr.length > 0) {
133+
currentSpeed -= 10;
134+
}
135+
if (currentSpeed < MIN_SPEED) {
136+
currentSpeed = MIN_SPEED;
137+
}
138+
}
139+
return currentSpeed;
140+
}
141+
142+
public boolean isGameRunning() {
143+
return isGameRunning;
144+
}
145+
146+
public void startGame() {
147+
isGameRunning = true;
148+
isTryToStop = false;
149+
currentSpeed = DEFAULT_SPEED;
150+
new Thread(new Runnable() {
151+
@Override
152+
public void run() {
153+
while (isGameRunning) {
154+
try {
155+
Thread.sleep(getInterruptTime());
156+
} catch (InterruptedException e) {
157+
e.printStackTrace();
158+
}
159+
160+
post(new Runnable() {
161+
@Override
162+
public void run() {
163+
int preIndex = currentIndex;
164+
currentIndex++;
165+
if (currentIndex >= itemViewArr.length) {
166+
currentIndex = 0;
167+
}
168+
169+
itemViewArr[preIndex].setFocus(false);
170+
itemViewArr[currentIndex].setFocus(true);
171+
172+
if (isTryToStop && currentSpeed == DEFAULT_SPEED && stayIndex == currentIndex) {
173+
isGameRunning = false;
174+
}
175+
}
176+
});
177+
}
178+
}
179+
}).start();
180+
}
181+
182+
public void tryToStop(int position) {
183+
stayIndex = position;
184+
isTryToStop = true;
185+
}
186+
187+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.jeanboy.app.luckymonkeypanel;
2+
3+
import android.support.v7.app.AppCompatActivity;
4+
import android.os.Bundle;
5+
import android.util.Log;
6+
import android.view.View;
7+
import android.widget.Button;
8+
9+
import java.util.Random;
10+
11+
public class MainActivity extends AppCompatActivity {
12+
13+
14+
private LuckyMonkeyPanelView lucky_panel;
15+
private Button btn_action;
16+
17+
@Override
18+
protected void onCreate(Bundle savedInstanceState) {
19+
super.onCreate(savedInstanceState);
20+
setContentView(R.layout.activity_main);
21+
22+
lucky_panel = (LuckyMonkeyPanelView) findViewById(R.id.lucky_panel);
23+
btn_action = (Button) findViewById(R.id.btn_action);
24+
25+
btn_action.setOnClickListener(new View.OnClickListener() {
26+
@Override
27+
public void onClick(View v) {
28+
if (!lucky_panel.isGameRunning()) {
29+
lucky_panel.startGame();
30+
} else {
31+
int stayIndex = new Random().nextInt(8);
32+
Log.e("LuckyMonkeyPanelView", "====stay===" + stayIndex);
33+
lucky_panel.tryToStop(stayIndex);
34+
}
35+
}
36+
});
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.jeanboy.app.luckymonkeypanel;
2+
3+
import android.content.Context;
4+
import android.support.annotation.Nullable;
5+
import android.util.AttributeSet;
6+
import android.view.View;
7+
import android.widget.FrameLayout;
8+
9+
/**
10+
* Created by jeanboy on 2017/4/20.
11+
*/
12+
13+
public class PanelItemView extends FrameLayout {
14+
15+
private View overlay;
16+
17+
public PanelItemView(Context context) {
18+
this(context, null);
19+
}
20+
21+
public PanelItemView(Context context, @Nullable AttributeSet attrs) {
22+
this(context, attrs, 0);
23+
}
24+
25+
public PanelItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
26+
super(context, attrs, defStyleAttr);
27+
inflate(context, R.layout.view_panel_item, this);
28+
overlay = findViewById(R.id.overlay);
29+
}
30+
31+
public void setFocus(boolean isFocused) {
32+
if (overlay != null) {
33+
overlay.setVisibility(isFocused ? INVISIBLE : VISIBLE);
34+
}
35+
}
36+
37+
}
5.27 KB
Loading
5.17 KB
Loading
2.02 KB
Loading
2.03 KB
Loading
Loading
34 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:shape="rectangle">
4+
5+
6+
<solid android:color="#1affffff" />
7+
8+
<corners android:radius="50dp" />
9+
10+
<stroke
11+
android:width="1dp"
12+
android:color="#26ffffff" />
13+
14+
</shape>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<item android:drawable="@drawable/btn_pressed" android:state_pressed="true"/>
5+
<item android:drawable="@drawable/btn_normal" />
6+
7+
</selector>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:shape="rectangle">
4+
5+
<solid android:color="#e03632" />
6+
<corners android:radius="10dp" />
7+
8+
</shape>

0 commit comments

Comments
 (0)