您的位置:首页 > 移动开发 > Android开发

android 5.X之使用Palette

2015-11-28 14:26 826 查看
这几天为了学些android5.0版本sdk的新特性,折腾了好久。AndroidStudio被我反复的安装又卸载又安装,在eclipse和AndroidStudio

之间来回折腾。没想到sdk升级到5.0版本,竟然会出这么多的麻烦。一开始还想着继续用eclipse,但是被各种升级插件以及导包折磨的死去活来,

换成AndroidStudio,结果电脑总是卡成狗!我都无语死了,后来在百般折腾下,最终还是抛弃了eclipse,使用AndroidStuidio。现在还是

不很习惯AndroidStudio的操作。以后会慢慢习惯吧。

好了吐槽完毕。既然工具已经搞定了,就开始迫不及待的看看android 5.0X都新增了哪些新特性。今天就来说说我学习的第一个新特性,

即调色板Palette。因为这个Palette可以从一张Bitmap图片中提取你所需要的色调,这样子极大了方便开发者来保持app的颜色观和谐统一。

下面是我的收获和一个小的例子。

一、使用前的准备

首先需要在gradle中添加依赖。

即在你build.gradle的dependencies中添加appconat-v7和palette-v7的依赖。如下:

dependencies {

compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:palette-v7:21.+'

}


二、关于Palette的使用API

首先是获取到一个Palette对象,有四种方式来获取,如下:

(一)
Palette p = Palette.generate(bitmap);
(二)
Palette p = Palette.generate(bitmap, 24);
(三)


注:(1)前两种方法为同步获取,后两种方法为异步获取,使用哪一种都行。异步获取到的palette在

onGenerated方法的参数中。因此一般颜色设定的逻辑也在这个方法里。

(2)在获取Palette对象时,可以指定它的size。一般size越大获取需要的时间也就越长。不指定

时,默认的调色板大小,即size为16.

(3)size大小多少为合适呢?一般来说头像之类的设定,最好在24~32,风景大图之类的,一般在

8~16之间。

获取到Palette就该使用它了,即接着通过Palette对象获取到一个样本swatch,有6中样本,如下:

Palette.Swatch s = p.getVibrantSwatch();       //获取到充满活力的这种色调

Palette.Swatch s = p.getDarkVibrantSwatch();    //获取充满活力的黑

Palette.Swatch s = p.getLightVibrantSwatch();   //获取充满活力的亮

Palette.Swatch s = p.getMutedSwatch();           //获取柔和的色调

Palette.Swatch s = p.getDarkMutedSwatch();      //获取柔和的黑

Palette.Swatch s = p.getLightMutedSwatch();    //获取柔和的亮


最后我们就可以利用采集的色调样本swatch对象给需要的东西赋予颜色了,有如下几个方法:

比如你的TextView有个背景图片,你可以使用Palette获取到这个背景图片的色调,然后利用getBodyTextColor

来给这个TextView的文字设定颜色,从而与背景图片比较相称!

三,一个小小的例子

用AndroidStudio新建一个项目,然后修改它的activity_main.xml代码。如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/mytext_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Hello World!" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/palette"
android:text="我是按钮"/>
</LinearLayout>


很简单,放置了一个TextView和一个按钮,且给按钮设置了一个背景图片。下面我要做的就是从

这张背景图片上提取色调来给整个布局赋予颜色。修改MainActivity的代码如下:

package kun.fuly.myapplication;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.graphics.Palette;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

private Bitmap bmp;
private TextView myText;
private Button btn;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bmp = BitmapFactory.decodeResource(getResources(), R.drawable.palette);

myText = (TextView) findViewById(R.id.mytext_view);

btn = (Button) findViewById(R.id.btn);

Palette p = Palette.generate(bmp );

Palette.Swatch s = p.getVibrantSwatch();

//给TextView设置背景颜色和文本颜色
myText.setBackground(new ColorDrawable(s.getRgb()));

myText.setTextColor(s.getBodyTextColor());

//为按钮的文本设置颜色
btn.setTextColor(s.getBodyTextColor());

//为actionbar设置颜色
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(s.getRgb()));
}

}


代码很简单,没什么好解释的。运行,效果如下:



好吧,看起来还是很丑,不过颜色还算是和谐。相信Palette在你的手里一定能运用的非常漂亮。其他的就不再说了,

关于Palette的介绍到此为止。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: