Android 5.x--使用Material Theme加Palette

Android5.0较之以前的版本,有一个比较大的改变:在Android5.x中支持Material Design的主题风格,这对于视觉上将是一个重大的改变。新的主题风格给人的感觉眼前一亮,原来Android也可以这么美,相信通过不断完善,Android也将越来越强大。那么今天就介绍一下,如何使用这让人眼前一亮的主题吧。

开始使用

目前只有在Android5.0之上的版本才可以支持Material的主题风格,这样我们首先在res/values-v21/styles.xml文件中定义使用Material主题。

<resources>
    <style name="AppTheme" parent="android:Theme.Material.Light">
    </style>
</resources>

注意直接在values/styles.xml文件之中修改的话,编译器会提示:当前支持的最低的API版本中不存在该主题。

定制自己的主题

Android开发者官网中给出一张图片,形象的告诉我们Material主题所支持修改的颜色属性,方便我们定制自己的主题:

这样我们就可以修改这些值来定制自己的主题了:

<resources>
    <style name="AppTheme" parent="android:Theme.Material.Light">
        <item name="android:colorPrimary">#00ffff</item>
        <item name="android:colorPrimaryDark">#0000ff</item>
        <item name="android:navigationBarColor">#0000ff</item>
        <item name="android:textColorPrimary">#fff</item>
    </style>
</resources>

在Android5.0的模拟器上运行一下吧,

怎么样,连标题栏和底部的导航栏都可以改变颜色了,而且连Actionbar都和以前不一样了,是不是很酷炫。

同时Android也允许你使用android:statusBarColor属性,或者使用Window.setStatusBarColor方法快速设置状态栏的颜色。

适配较低的Android版本

一切都很好,只是我的主题只能在5.0之上的版本才能用吗,太扫兴了吧。别担心,谷歌当然也会为低版本提供支持,让我们看看怎么做吧。

1.在Android studio中,首先来增加下面的Gradle依赖模块到工程中来:

dependencies {
    ......
    compile ‘com.android.support:appcompat-v7:22.0.0‘
}

接着我们在values/style.xml文件中添加自己的主题,注意这时我们不再使用android:Theme.Material.XXX的主题,而是使用依赖库中的主题Theme.AppCompat.XXX,同时不再需要values-v21/style.xml中的android:Theme.Material.XXX主题。

<style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#0ff</item>
        <item name="colorPrimaryDark">#00f</item>
        <item name="colorAccent">#fff</item>
</style>

测试中发现textColorPrimary属性的名称改为了colorAccent

2.在Eclipse中,我们需要升级到最新的ADT以及SDK,在WorkSpace中导入appcompat-v7的library工程,之后的使用步骤是一模一样的,大家可以试一试。

Palette的使用

同时,在Android 5.0中Google还发布了一个新的类Palette帮助我们提取Bitmap中颜色值,让我更方便的定制自己的主题,一起来看看吧。

这个类能提取以下突出的颜色:

  • Vibrant(清爽的)
  • Vibrant dark(清爽的暗色)
  • Vibrant light(清爽的亮色)
  • Muted(柔和的)
  • Muted dark(柔和的暗色)
  • Muted lighr(柔和的亮色)

使用Palette

首先在使用之前,我们需要引用该依赖库到我们的工程中来:

dependencies {
    ...
    compile ‘com.android.support:palette-v7:21.0.+‘
}  

通过调用Palette的静态方法generate或generateAsync可以帮助我们提取颜色值,提取颜色的过程必须在异步线程中执行。

注意,generate方法不允许在主线程中调用,如果自己没有使用异步线程,可以使用generateAsync方法去帮助我们开启一个异步线程,并监听回调事件。

Palette.generateAsync(bitmap,
        new Palette.PaletteAsyncListener() {
    @Override
    public void onGenerated(Palette palette) {
         Palette.Swatch vibrant =
                 palette.getVibrantSwatch();
          if (vibrant != null) {
              // If we have a vibrant color
             .......
          }
    }
});

从View获得Bitmap

传入的对象必须是一个Bitmap对象,那么如何从一个View转化为Bitmap呢,我们可以通过画布绘制来实现:

private Bitmap getViewBitmap(View v) {
    v.clearFocus();
    v.setPressed(false);

    boolean willNotCache = v.willNotCacheDrawing();
    v.setWillNotCacheDrawing(false);

    int color = v.getDrawingCacheBackgroundColor();
    v.setDrawingCacheBackgroundColor(0);

    if (color != 0) {
        v.destroyDrawingCache();
    }
    v.buildDrawingCache();
    Bitmap cacheBitmap = v.getDrawingCache();
    if (cacheBitmap == null) {
        return null;
    }
    Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
    // Restore the view
    v.destroyDrawingCache();
    v.setWillNotCacheDrawing(willNotCache);
    v.setDrawingCacheBackgroundColor(color);
    return bitmap;
}

接下来我们来用一下吧,通过提取Activity的整个布局中较安的柔和颜色,并把它设置给状态栏上。

package com.example.acer.materialtest;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.graphics.Palette;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends ActionBarActivity {

    private LinearLayout containLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        containLayout = (LinearLayout) findViewById(R.id.contain_layout);
        Bitmap bitmap = getViewBitmap(containLayout);
        if (bitmap != null)
            Palette.generateAsync(bitmap,
                    new Palette.PaletteAsyncListener() {
                        @Override
                        public void onGenerated(Palette palette) {
                            Palette.Swatch swatch =
                                    palette.getDarkMutedSwatch();
                            if (swatch != null) {
                               getWindow().setStatusBarColor(swatch .getRgb());
                            }
                        }
                    });

    }

    private Bitmap getViewBitmap(View v) {
        v.clearFocus();
        v.setPressed(false);

        boolean willNotCache = v.willNotCacheDrawing();
        v.setWillNotCacheDrawing(false);

        // Reset the drawing cache background color to fully transparent
        // for the duration of this operation
        int color = v.getDrawingCacheBackgroundColor();
        v.setDrawingCacheBackgroundColor(0);

        if (color != 0) {
            v.destroyDrawingCache();
        }
        v.buildDrawingCache();
        Bitmap cacheBitmap = v.getDrawingCache();
        if (cacheBitmap == null) {
            return null;
        }
        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
        // Restore the view
        v.destroyDrawingCache();
        v.setWillNotCacheDrawing(willNotCache);
        v.setDrawingCacheBackgroundColor(color);
        return bitmap;
    }

}

效果如下:

时间: 2024-08-07 11:13:53

Android 5.x--使用Material Theme加Palette的相关文章

Android最佳实践之Material Design

Material概述及主题 学习地址:http://developer.android.com/training/material/get-started.html 使用material design创建App: 温习一下material design说明 在app中应用material 主题 创建遵循material design规则的布局 指定投射阴影的高度 使用ListView和CardView 自定义动画 使用Material 主题 <!-- res/values/styles.xml

Material Theme

Material Theme提供了一下功能: 1.系统widgets能够设置调色板 2.系统widgets的触摸反馈 3.Activity过渡动画 你能够依据你品牌的色彩来定义Material Theme.能够使用Material Theme的色彩为status bar.action bar着色.參考下图. 系统Widgets有新的设计和触摸动画,你也能够在自己的应用中自己定义色彩调色板.触摸反馈动画.Activity过渡. Material Theme的定义例如以下: @android:sty

Material Design系列第三篇——Using the Material Theme

Using the Material Theme This lesson teaches you to Customize the Color Palette Customize the Status Bar Theme Individual Views You should also read Material design specification Material design on Android The new material theme provides: System widg

Android(Lollipop/5.0) Material Design(三) 使用Material主题

官网地址:https://developer.android.com/intl/zh-tw/training/material/theme.html 新的Material主题提供了: 系统Widgets可设置它们的调色板 系统Widgets的触摸反馈动画 Activity的过渡动画 您可以自定义Material主题,根据你的品牌标识,用一个调色板来控制. 可以使用主题属性来为操作栏和状态栏着色,如图所示: Material主题的定义: @android:style/Theme.Material

Creating Apps With Material Design —— Using the Material Theme

转载请注明 http://blog.csdn.net/eclipsexys 翻译自Developer Android,时间仓促,有翻译问题请留言指出,谢谢 使用Material主题 这种新材料的主题为: 可以让你设置自己的自定义color palette 要自定义主题的基础颜色,以适应你的品牌,使用的时候,你从材料主题主题继承属性定义自定义颜色: 系统部件 触摸反馈的动画为系统部件 Activity过渡动画 您可以根据一个color palette,你控制你的品牌形象定制的材料主题的外观.您可以

使用Material Design 创建App翻译系列----材料主题的使用(Using Material Theme)

上一篇是使用Material Design 创建App翻译系列--開始学习篇,进入正题: 新的材料主题提供了下面内容: 1. 提供了同意设置颜色板的系统部件组件. 2. 为这些系统组件提供了触摸反馈动画. 3. Activity的过渡动画. 依据你的品牌标识,使用你所控制的颜色板能够自己定义材料主题的外观. 使用主题的属性能够给ActionBar 和 status bar进行着色. 系统部件拥有新的设计和触摸反馈动画.你能够为你的应用自己定义颜色板.触摸反馈动画以及Activity之间跳转的过渡

Android中的Apk的加固(加壳)原理解析和实现

Android中的Apk的加固(加壳)原理解析和实现 标签: android 2015-09-13 13:58 42287人阅读 评论(49) 收藏 举报 本文章已收录于:  Android知识库  分类: Android(140)  版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 一.前言 今天又到周末了,憋了好久又要出博客了,今天来介绍一下Android中的如何对Apk进行加固的原理.现阶段.我们知道Android中的反编译工作越来越让人操作熟练,我们辛苦的开发出一个

Android(Lollipop/5.0) Material Design(二) 入门指南

官网地址:https://developer.android.com/intl/zh-tw/training/material/get-started.html Apply the Material Theme 运用材料主题 Design Your Layouts  设计你的布局 除了应用和自定义材料的主题,你的布局应符合材料的设计准则.当你设计你的布局,以下需要特别注意: 基线网格 Keylines 间距 触摸目标尺寸 布局结构 Specify Elevation in Your Views 

Material design之Material Theme

Material Theme提供了三方面的内容: 系统组件的颜色可以自定义更改 系统组件添加了触摸反馈动画效果 Activity切换动画效果 系统主题默认被定义在以下三个样式中: 使用Material主题的时候需要注意,Material目前只能运行在Android L的预览版本上. 系统组件颜色的更改: 通过在res/values/styles建立一个新的style,继承自android:Theme.Material, 然后可以更改适合自己应用的样式了 以上更改的位置对应下图: 其中的Statu