Skip to content

Commit cfe131d

Browse files
committed
remove or add some links
1 parent abb4711 commit cfe131d

File tree

4 files changed

+307
-0
lines changed

4 files changed

+307
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ Utils/URLChecker/out
22
Utils/URLChecker/*.class
33
Utils/.idea
44
Utils/Utils.iml
5+
Utils/out
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
[原文链接](http://baoyz.com/android/2014/10/21/android-palette-use/) (*需要科学上网*)
2+
3+
## Android Lollipop 新特性 - Palette
4+
5+
> *Palette 可以从一张图片中提取颜色,我们可以把提取的颜色融入到App UI中,可以使UI风格更加美观融洽。比如,我们可以从图片中提取颜色设置给ActionBar做背景颜色,这样ActionBar的颜色就会随着显示图片的变化而变化。*
6+
7+
Palette可以提取的颜色如下:
8+
+ Vibrant (有活力的)
9+
+ Vibrant dark(有活力的 暗色)
10+
+ Vibrant light(有活力的 亮色)
11+
+ Muted (柔和的)
12+
+ Muted dark(柔和的 暗色)
13+
+ Muted light(柔和的 亮色)
14+
15+
### 使用方法
16+
17+
> *我们要想使用Palette,需要导入Palette的兼容库,`Gradle` 中添加下面依赖。*
18+
19+
```
20+
compile 'com.android.support:palette-v7:21.0.0'
21+
```
22+
23+
第一步,我们需要通过一个Bitmap对象来生成一个对应的Palette对象。
24+
Palette 提供了四个静态方法用来生成对象。
25+
26+
+ `Palette generate(Bitmap bitmap)`
27+
+ `Palette generate(Bitmap bitmap, int numColors)`
28+
+ `generateAsync(Bitmap bitmap, PaletteAsyncListener listener)`
29+
+ `generateAsync(Bitmap bitmap, int numColors, final PaletteAsyncListener listener)`
30+
31+
> *不难看出,生成方法分为`generate`(同步)和`generateAsync`(异步)两种,如果图片过大使用generate方法,可能会阻塞主线程,我们更倾向于使用`generateAsync`的方法,其实内部就是创建了一个`AsyncTask``generateAsync`方法需要一个`PaletteAsyncListener`对象用于监听生成完毕的回调。除了必须的`Bitmap`参数外,还可以传入一个`numColors`参数指定颜色数,默认是 16。*
32+
33+
第二步,得到Palette对象后,就可以拿到提取到的颜色值
34+
35+
+ `Palette.getVibrantSwatch()`
36+
+ `Palette.getDarkVibrantSwatch()`
37+
+ `Palette.getLightVibrantSwatch()`
38+
+ `Palette.getMutedSwatch()`
39+
+ `Palette.getDarkMutedSwatch()`
40+
+ `Palette.getLightMutedSwatch()`
41+
42+
第三步,使用颜色,上面get方法中返回的是一个 `Swatch` 样本对象,这个样本对象是Palette的一个内部类,它提供了一些获取最终颜色的方法。
43+
44+
+ `getPopulation()`: 样本中的像素数量
45+
+ `getRgb()`: 颜色的RBG值
46+
+ `getHsl()`: 颜色的HSL值
47+
+ `getBodyTextColor()`: 主体文字的颜色值
48+
+ `getTitleTextColor()`: 标题文字的颜色值
49+
50+
> *通过 `getRgb()` 可以得到最终的颜色值并应用到UI中。`getBodyTextColor()``getTitleTextColor()` 可以得到此颜色下文字适合的颜色,这样很方便我们设置文字的颜色,使文字看起来更加舒服。*
51+
52+
### 实例代码
53+
54+
```java
55+
// 此方法可能会阻塞主线程,建议使用异步方法
56+
Palette palette = Palette.generate(bitmap);
57+
// 异步提取Bitmap颜色
58+
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
59+
@Override
60+
public void onGenerated(Palette palette) {
61+
// 提取完毕
62+
// 有活力的颜色
63+
Palette.Swatch vibrant = palette.getVibrantSwatch();
64+
// 有活力的暗色
65+
Palette.Swatch darkVibrant = palette.getDarkVibrantSwatch();
66+
// 有活力的亮色
67+
Palette.Swatch lightVibrant = palette.getLightVibrantSwatch();
68+
// 柔和的颜色
69+
Palette.Swatch muted = palette.getMutedSwatch();
70+
// 柔和的暗色
71+
Palette.Swatch darkMuted = palette.getDarkMutedSwatch();
72+
// 柔和的亮色
73+
Palette.Swatch lightMuted = palette.getLightMutedSwatch();
74+
 
75+
// 使用颜色
76+
// 修改Actionbar背景颜色
77+
getActionBar().setBackgroundDrawable(new ColorDrawable(vibrant.getRgb()));
78+
// 修改文字的颜色
79+
mTextView.setTextColor(vibrant.getTitleTextColor());
80+
...
81+
// 根据需求选择不同效果的颜色应用
82+
});
83+
```
84+
85+
### 效果
86+
87+
![pic](https://raw.githubusercontent.com/baoboy/baoboy.github.io/master/images/screenshots/palette_3.png)
88+
89+
### 代码下载
90+
91+
[https://github.com/baoyongzhang/android-lollipop-samples](https://github.com/baoyongzhang/android-lollipop-samples)中的[palette-sample](https://github.com/baoyongzhang/android-lollipop-samples/tree/master/palette-sample)
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Handling Runtime Changes
2+
3+
Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, Android restarts the running `Activity` (`onDestroy()` is called, followed by `onCreate()`). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.
4+
5+
To properly handle a restart, it is important that your activity restores its previous state through the normal `Activity` lifecycle, in which Android calls `onSaveInstanceState()` before it destroys your activity so that you can save data about the application state. You can then restore the state during `onCreate()` or `onRestoreInstanceState()`.
6+
7+
To test that your application restarts itself with the application state intact, you should invoke configuration changes (such as changing the screen orientation) while performing various tasks in your application. Your application should be able to restart at any time without loss of user data or state in order to handle events such as configuration changes or when the user receives an incoming phone call and then returns to your application much later after your application process may have been destroyed. To learn how you can restore your activity state, read about the `Activity lifecycle`.
8+
9+
However, you might encounter a situation in which restarting your application and restoring significant amounts of data can be costly and create a poor user experience. In such a situation, you have two other options:
10+
11+
1. Retain an object during a configuration change
12+
13+
Allow your activity to restart when a configuration changes, but carry a stateful object to the new instance of your activity.
14+
2. Handle the configuration change yourself
15+
Prevent the system from restarting your activity during certain configuration changes, but receive a callback when the configurations do change, so that you can manually update your activity as necessary.
16+
17+
18+
### Retaining an Object During a Configuration Change
19+
20+
If restarting your activity requires that you recover large sets of data, re-establish a network connection, or perform other intensive operations, then a full restart due to a configuration change might be a slow user experience. Also, it might not be possible for you to completely restore your activity state with the `Bundle` that the system saves for you with the `onSaveInstanceState()` callback—it is not designed to carry large objects (such as bitmaps) and the data within it must be serialized then deserialized, which can consume a lot of memory and make the configuration change slow. In such a situation, you can alleviate the burden of reinitializing your activity by retaining a `Fragment` when your activity is restarted due to a configuration change. This fragment can contain references to stateful objects that you want to retain.
21+
22+
When the Android system shuts down your activity due to a configuration change, the fragments of your activity that you have marked to retain are not destroyed. You can add such fragments to your activity to preserve stateful objects.
23+
24+
To retain stateful objects in a fragment during a runtime configuration change:
25+
26+
1. Extend the Fragment class and declare references to your stateful objects.
27+
2. Call setRetainInstance(boolean) when the fragment is created.
28+
3. Add the fragment to your activity.
29+
4. Use FragmentManager to retrieve the fragment when the activity is restarted.
30+
31+
For example, define your fragment as follows:
32+
33+
```java
34+
public class RetainedFragment extends Fragment {
35+
36+
// data object we want to retain
37+
private MyDataObject data;
38+
39+
// this method is only called once for this fragment
40+
@Override
41+
public void onCreate(Bundle savedInstanceState) {
42+
super.onCreate(savedInstanceState);
43+
// retain this fragment
44+
setRetainInstance(true);
45+
}
46+
47+
public void setData(MyDataObject data) {
48+
this.data = data;
49+
}
50+
51+
public MyDataObject getData() {
52+
return data;
53+
}
54+
}
55+
```
56+
> **Caution**: While you can store any object, you should never pass an object that is tied to the `Activity`, such as a `Drawable`, an `Adapter`, a `View` or any other object that's associated with a `Context`. If you do, it will leak all the views and resources of the original activity instance. (Leaking resources means that your application maintains a hold on them and they cannot be garbage-collected, so lots of memory can be lost.)
57+
58+
Then use `FragmentManager` to add the fragment to the activity. You can obtain the data object from the fragment when the activity starts again during runtime configuration changes. For example, define your activity as follows:
59+
60+
```java
61+
public class MyActivity extends Activity {
62+
63+
private RetainedFragment dataFragment;
64+
65+
@Override
66+
public void onCreate(Bundle savedInstanceState) {
67+
super.onCreate(savedInstanceState);
68+
setContentView(R.layout.main);
69+
70+
// find the retained fragment on activity restarts
71+
FragmentManager fm = getFragmentManager();
72+
dataFragment = (DataFragment) fm.findFragmentByTag(“data”);
73+
74+
// create the fragment and data the first time
75+
if (dataFragment == null) {
76+
// add the fragment
77+
dataFragment = new DataFragment();
78+
fm.beginTransaction().add(dataFragment, “data”).commit();
79+
// load the data from the web
80+
dataFragment.setData(loadMyData());
81+
}
82+
83+
// the data is available in dataFragment.getData()
84+
...
85+
}
86+
87+
@Override
88+
public void onDestroy() {
89+
super.onDestroy();
90+
// store the data in the fragment
91+
dataFragment.setData(collectMyLoadedData());
92+
}
93+
}
94+
```
95+
96+
In this example, `onCreate()` adds a fragment or restores a reference to it. `onCreate()` also stores the stateful object inside the fragment instance. `onDestroy()` updates the stateful object inside the retained fragment instance.
97+
98+
### Handling the Configuration Change Yourself
99+
If your application doesn't need to update resources during a specific configuration change and you have a performance limitation that requires you to avoid the activity restart, then you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.
100+
101+
> **Note**: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.
102+
103+
To declare that your activity handles a configuration change, edit the appropriate `<activity>` element in your manifest file to include the `android:configChanges` attribute with a value that represents the configuration you want to handle. Possible values are listed in the documentation for the `android:configChanges` attribute (the most commonly used values are `"orientation"` to prevent restarts when the screen orientation changes and `"keyboardHidden"` to prevent restarts when the keyboard availability changes). You can declare multiple configuration values in the attribute by separating them with a pipe `|` character.
104+
105+
For example, the following manifest code declares an activity that handles both the screen orientation change and keyboard availability change:
106+
107+
```
108+
<activity android:name=".MyActivity"
109+
android:configChanges="orientation|keyboardHidden"
110+
android:label="@string/app_name">
111+
```
112+
113+
Now, when one of these configurations change, `MyActivity` does not restart. Instead, the MyActivity receives a call to `onConfigurationChanged()`. This method is passed a `Configuration` object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your activity's `Resources` object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your activity.
114+
115+
> **Caution**: Beginning with Android 3.2 (API level 13), **the "screen size" also changes** when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the `minSdkVersion` and `targetSdkVersion` attributes), you must include the `"screenSize"` value in addition to the `"orientation"` value. That is, you must decalare `android:configChanges="orientation|screenSize"`. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
116+
117+
For example, the following `onConfigurationChanged()` implementation checks the current device orientation:
118+
119+
```java
120+
@Override
121+
public void onConfigurationChanged(Configuration newConfig) {
122+
super.onConfigurationChanged(newConfig);
123+
124+
// Checks the orientation of the screen
125+
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
126+
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
127+
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
128+
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
129+
}
130+
}
131+
```
132+
133+
The `Configuration` object represents all of the current configurations, not just the ones that have changed. Most of the time, you won't care exactly how the configuration has changed and can simply re-assign all your resources that provide alternatives to the configuration that you're handling. For example, because the `Resources` object is now updated, you can reset any `ImageViews` with `setImageResource()` and the appropriate resource for the new configuration is used (as described in `Providing Resources`).
134+
135+
Notice that the values from the `Configuration` fields are integers that are matched to specific constants from the `Configuration` class. For documentation about which constants to use with each field, refer to the appropriate field in the `Configuration` reference.
136+
137+
> **Remember**: When you declare your activity to handle a configuration change, you are responsible for resetting any elements for which you provide alternatives. If you declare your activity to handle the orientation change and have images that should change between landscape and portrait, you must re-assign each resource to each element during `onConfigurationChanged()`.
138+
139+
If you don't need to update your application based on these configuration changes, you can instead not implement `onConfigurationChanged()`. In which case, all of the resources used before the configuration change are still used and you've only avoided the restart of your activity. However, your application should always be able to shutdown and restart with its previous state intact, so you should not consider this technique an escape from retaining your state during normal activity lifecycle. Not only because there are other configuration changes that you cannot prevent from restarting your application, but also because you should handle events such as when the user leaves your application and it gets destroyed before the user returns to it.
140+
141+
For more about which configuration changes you can handle in your activity, see the [`android:configChanges`](http://developer.android.com/guide/topics/manifest/activity-element.html#config) documentation and the [`Configuration`](http://developer.android.com/reference/android/content/res/Configuration.html) class. [注1]
142+
143+
[注1]: 需要科学上网
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[原文链接](https://gist.github.com/polbins/e37206fbc444207c0e92) by [polbins](https://gist.github.com/polbins)
2+
3+
---
4+
### Simple RecyclerView Divider
5+
Simple Horizontal Divider Item Decoration for RecyclerView
6+
7+
```java
8+
mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(
9+
getApplicationContext()
10+
));
11+
```
12+
13+
**NOTE**: Add item decoration prior to setting the adapter
14+
15+
---
16+
#### line_divider:
17+
18+
```java
19+
<?xml version="1.0" encoding="utf-8"?>
20+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
21+
android:shape="rectangle">
22+
23+
24+
<size
25+
android:width="1dp"
26+
android:height="1dp" />
27+
28+
29+
<solid android:color="@color/dark_gray" />
30+
31+
32+
</shape>
33+
```
34+
35+
---
36+
37+
#### SimpleDividerItemDecoration.java
38+
39+
```java
40+
public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
41+
private Drawable mDivider;
42+
43+
44+
public SimpleDividerItemDecoration(Context context) {
45+
mDivider = context.getResources().getDrawable(R.drawable.line_divider);
46+
}
47+
48+
49+
@Override
50+
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
51+
int left = parent.getPaddingLeft();
52+
int right = parent.getWidth() - parent.getPaddingRight();
53+
54+
55+
int childCount = parent.getChildCount();
56+
for (int i = 0; i < childCount; i++) {
57+
View child = parent.getChildAt(i);
58+
59+
60+
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
61+
62+
63+
int top = child.getBottom() + params.bottomMargin;
64+
int bottom = top + mDivider.getIntrinsicHeight();
65+
66+
67+
mDivider.setBounds(left, top, right, bottom);
68+
mDivider.draw(c);
69+
}
70+
}
71+
}
72+
```

0 commit comments

Comments
 (0)