Skip to content

Commit e4733fb

Browse files
committed
添加夜间模式效果
1 parent b257fe1 commit e4733fb

6 files changed

Lines changed: 104 additions & 16 deletions

File tree

‎src/main/AndroidManifest.xml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
<activity
3939
android:name=".activity.AnimatorActivity"
4040
android:label="@string/android_animator" />
41-
<activity android:name=".activity.DayNightActivity"></activity>
41+
<activity
42+
android:name=".activity.DayNightActivity"
43+
android:label="@string/day_night_mode" />
4244
</application>
4345

4446
</manifest>

‎src/main/java/com/clock/study/activity/DayNightActivity.java‎

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package com.clock.study.activity;
22

3+
import android.animation.Animator;
4+
import android.animation.AnimatorListenerAdapter;
5+
import android.animation.ObjectAnimator;
6+
import android.animation.ValueAnimator;
7+
import android.graphics.Bitmap;
38
import android.os.Bundle;
49
import android.support.v7.app.AppCompatActivity;
10+
import android.util.Log;
511
import android.view.Menu;
612
import android.view.MenuInflater;
713
import android.view.MenuItem;
14+
import android.view.View;
15+
import android.widget.ImageView;
16+
import android.widget.RelativeLayout;
817

918
import com.clock.study.R;
1019

@@ -18,11 +27,26 @@ public class DayNightActivity extends AppCompatActivity {
1827

1928
private final static String TAG = DayNightActivity.class.getSimpleName();
2029

30+
private static boolean isNight = false;
31+
32+
private View mRootView;
33+
/**
34+
* 切换模式时,保存界面效果用于
35+
*/
36+
private ImageView mTransView;
37+
2138
@Override
2239
protected void onCreate(Bundle savedInstanceState) {
2340
super.onCreate(savedInstanceState);
41+
if (isNight) {
42+
setTheme(R.style.NightTheme);
43+
} else {
44+
setTheme(R.style.DayTheme);
45+
}
2446
setContentView(R.layout.activity_day_night);
2547

48+
initView();
49+
2650
}
2751

2852
@Override
@@ -36,10 +60,51 @@ public boolean onCreateOptionsMenu(Menu menu) {
3660
public boolean onOptionsItemSelected(MenuItem item) {
3761
int itemId = item.getItemId();
3862
if (itemId == R.id.day_mode) {
39-
63+
isNight = false;
64+
recreate();
4065
} else if (itemId == R.id.night_mode) {
41-
66+
isNight = true;
67+
initTransBitmap();
68+
startAnimation(mTransView);
69+
recreate();
4270
}
4371
return super.onOptionsItemSelected(item);
4472
}
73+
74+
private void initView() {
75+
mRootView = findViewById(R.id.layout_main);
76+
mTransView = (ImageView) findViewById(R.id.iv_trans);
77+
}
78+
79+
/**
80+
* 初始化起过渡效果用的Bitmap
81+
*/
82+
private void initTransBitmap() {
83+
//开启绘制缓存,让View中的内容能够转换成为Bitmap
84+
mRootView.setDrawingCacheEnabled(true);
85+
Bitmap bitmap = mRootView.getDrawingCache();
86+
if (bitmap != null) {
87+
mTransView.setImageBitmap(bitmap);
88+
mTransView.setAlpha(1f);
89+
mTransView.setVisibility(View.VISIBLE);
90+
}
91+
}
92+
93+
/**
94+
* 启动过度用的动画效果
95+
*/
96+
private void startAnimation(final View transView) {
97+
ObjectAnimator animator = ObjectAnimator.ofFloat(transView, "alpha", 1f, 0f);
98+
animator.addListener(new AnimatorListenerAdapter() {
99+
@Override
100+
public void onAnimationEnd(Animator animation) {
101+
super.onAnimationEnd(animation);
102+
transView.setVisibility(View.GONE);
103+
}
104+
});
105+
animator.setDuration(1000);
106+
animator.start();
107+
108+
}
109+
45110
}
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,42 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
6-
android:paddingBottom="@dimen/activity_vertical_margin"
7-
android:paddingLeft="@dimen/activity_horizontal_margin"
8-
android:paddingRight="@dimen/activity_horizontal_margin"
9-
android:paddingTop="@dimen/activity_vertical_margin"
106
tools:context=".activity.DayNightActivity">
117

128
<LinearLayout
13-
android:layout_width="wrap_content"
14-
android:layout_height="wrap_content"
9+
android:id="@+id/layout_main"
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent"
1512
android:layout_centerInParent="true"
13+
android:gravity="center"
1614
android:orientation="vertical">
1715

1816
<ImageView
17+
android:id="@+id/iv_portrait"
1918
android:layout_width="200dp"
2019
android:layout_height="200dp"
2120
android:background="@mipmap/lufy" />
2221

2322
<TextView
24-
android:layout_width="match_parent"
23+
android:layout_width="wrap_content"
2524
android:layout_height="wrap_content"
2625
android:layout_marginTop="5dp"
2726
android:text="昵称:D_clock爱吃葱花"
2827
android:textSize="15sp" />
2928

3029
<TextView
31-
android:layout_width="match_parent"
30+
android:layout_width="wrap_content"
3231
android:layout_height="wrap_content"
3332
android:layout_marginTop="5dp"
3433
android:text="格言:人的梦想是不会终结的!"
3534
android:textSize="15sp" />
3635
</LinearLayout>
3736

38-
</RelativeLayout>
37+
<ImageView
38+
android:id="@+id/iv_trans"
39+
android:layout_width="match_parent"
40+
android:layout_height="match_parent"
41+
android:visibility="gone" />
42+
</FrameLayout>

‎src/main/res/layout/activity_main.xml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
android:id="@+id/btn_animation"
1919
android:layout_width="match_parent"
2020
android:layout_height="wrap_content"
21-
android:text="Android Animation" />
21+
android:text="@string/android_animation" />
2222

2323
<Button
2424
android:id="@+id/btn_animator"
2525
android:layout_width="match_parent"
2626
android:layout_height="wrap_content"
27-
android:text="Android Animator" />
27+
android:text="@string/android_animator" />
2828

2929
<Button
3030
android:id="@+id/btn_day_night"
3131
android:layout_width="match_parent"
3232
android:layout_height="wrap_content"
33-
android:text="Day Night" />
33+
android:text="@string/day_night_mode" />
3434

3535
</LinearLayout>

‎src/main/res/values/strings.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
<string name="settings">设置</string>
1212
<string name="android_animation">Android Animation</string>
1313
<string name="android_animator">Android Animator</string>
14+
<string name="day_night_mode">DayNight Mode</string>
1415
<string name="animation_test">Animation Test</string>
1516
</resources>

‎src/main/res/values/styles.xml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,20 @@
88
<item name="colorAccent">@color/colorAccent</item>
99
</style>
1010

11+
<!--白天主题,此处直接和AppTheme一样-->
12+
<style name="DayTheme" parent="Theme.AppCompat.Light.DarkActionBar">
13+
<item name="colorPrimary">@color/colorPrimary</item>
14+
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
15+
<item name="colorAccent">@color/colorAccent</item>
16+
</style>
17+
18+
<!--夜间主题-->
19+
<style name="NightTheme" parent="Theme.AppCompat.Light.DarkActionBar">
20+
<item name="colorPrimary">#3F3F3F</item>
21+
<item name="colorPrimaryDark">#3A3A3A</item>
22+
<item name="colorAccent">#868686</item>
23+
<item name="android:background">#3F3F3F</item>
24+
<item name="android:textColor">#8A9599</item>
25+
</style>
26+
1127
</resources>

0 commit comments

Comments
 (0)