Android5.0的Palette(調色板)、視圖陰影、着色和裁剪介紹

Android5.0的Palette(調色板)、視圖陰影、着色和裁剪介紹

           隨着Android5.0的發佈,google帶來了Material Design,俗稱:材料設計。並帶來了一些新的東西,這裏就一一介紹這些新的設計元素。

1、Palette(調色板)
      
        在5.0以後的android版本中可以使用調色板來提取顏色,讓app的主題顏色動態的適應當前頁面的色調,使得你的app整體看起來主題基調和諧統一;下面是官網的介紹和使用說明,先截個圖,後面簡單翻譯下:
         
      翻譯:
palette這是一個可以從image中提取顏色的類,它可以從image中提取及幾種不同的色調,如下:
  •      Vibrant              : 充滿活力的,
  •      Vibrant  dark    :充滿活力的黑
  •      Vibrant  light     :充滿活力的亮
  •      Muted               :柔和的
  •      Muted   dark       : 柔和的黑
  •      Muted   light       :  柔和的亮
      使用Palette可以在Android studio中的gradle添加以下依賴:
compile 'com.android.support:palette-v7:23.4.0'
    從示例代碼中,我們可以看到:通過傳遞一個bitmap對象給Palette,並調用他的Palette.generate()靜態方法或者在靜態方法中添加異步接口的方法來創建一個Palette,接下來就可以使用Palette的getter方法來檢索相應的色調,就是是昂面那6中色調;下面顯示一段代碼,將通過背景圖片的柔和色調來改變ActionBar和狀態來的色調,使之能夠統一,然而並不是所有6種顏色方案都可用,每種顏色方案都返回爲Palette.Swatch,如果圖片示例包含的顏色不足以產生兼容的方案,則對應的顏色方案可能爲null。爲了展示這個功能,我們將創建一個圖片圖塊的背景和標題將通過Palette主題話。效果如下:


是不是給人很不一樣的感覺了,接下來就介紹代碼,如下:



item_list.xml 如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:gravity="center"
        android:textAppearance="?android:textAppearanceLarge" />
</LinearLayout>

適配器中的調色板顏色
ColorfulAdapter.class

package com.world.hello.colorfullistactivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.AsyncTask;
import android.support.v7.graphics.Palette;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by chengguo on 2016/6/12.
 */
public class ColorfulAdapter extends ArrayAdapter<String> {

    private static final int[] IMAGES = {
            R.drawable.bricks, R.drawable.flower,
            R.drawable.grass, R.drawable.stones,
            R.drawable.wood, R.drawable.dog
    };

    private static final String[] NAMES = {
            "Bricks", "Flower",
            "Grass", "Stones",
            "Wood", "Dog"
    };

    private SparseArray<Bitmap> mImages;
    private SparseArray<Palette.Swatch> mBackgroundClolors;

    public ColorfulAdapter(Context context) {
        super(context, R.layout.item_list, NAMES);
        mImages = new SparseArray<Bitmap>(IMAGES.length);
        mBackgroundClolors = new SparseArray<Palette.Swatch>(IMAGES.length);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_list, parent, false);
        }
        View root = convertView.findViewById(R.id.root);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
        TextView textView = (TextView) convertView.findViewById(R.id.text);

        int imageId = IMAGES[position];
        if (mImages.get(imageId) == null) {
            new ImageTask().execute(imageId);
            textView.setTextColor(Color.BLACK);
        } else {
            imageView.setImageBitmap(mImages.get(imageId));

            Palette.Swatch colors = mBackgroundClolors.get(imageId);
            if (colors != null) {
                root.setBackgroundColor(colors.getRgb());
                textView.setTextColor(colors.getTitleTextColor());
            }
        }
        textView.setText(NAMES[position]);
        return convertView;
    }

    private class ImageResult {
        public int imageId;
        public Bitmap image;
        public Palette.Swatch colors;

        public ImageResult(int imageId, Bitmap image, Palette.Swatch colors) {
            this.imageId = imageId;
            this.image = image;
            this.colors = colors;
        }
    }

    /**
     * 因爲從磁盤加載圖片和使用Palette分析這些圖片的過程會花費一些時間,所以我們要在後臺執行此工作,
     * 以免阻塞主線程太長時間,因此放在AsyncTask中執行
     */
    private class ImageTask extends AsyncTask<Integer, Void, ImageResult> {

        @Override
        protected ImageResult doInBackground(Integer... params) {

            int imageId = params[0];
            //確保圖片縮率圖不會太大
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            Bitmap image = BitmapFactory.decodeResource(getContext().getResources(), imageId, options);
            Palette colors = Palette.generate(image);
            Palette.Swatch selected = colors.getVibrantSwatch();
            if (selected == null) {
                selected = colors.getMutedSwatch();
            }
            
            return new ImageResult(imageId, image, selected);
        }

        @Override
        protected void onPostExecute(ImageResult imageResult) {
            updateImageItem(imageResult);
            notifyDataSetChanged();
        }
    }

    /**
     * 更新一項的顏色
     *
     * @param imageResult
     */
    private void updateImageItem(ImageResult imageResult) {
        mImages.put(imageResult.imageId, imageResult.image);
        mBackgroundClolors.put(imageResult.imageId, imageResult.colors);
    }
}

activity.class
package com.world.hello.colorfullistactivity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.GridView;

public class MainActivity extends AppCompatActivity {

    private GridView mGridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mGridView = new GridView(this);
        mGridView.setNumColumns(2);
        mGridView.setAdapter(new ColorfulAdapter(this));

        setContentView(mGridView);
    }
}




2、視圖和陰影
      
        在Material Design中很重要的風格就是擬物扁平化,使用陰影和光線,在配合完美的動畫,展示出現實生活中的效果,看起來就感覺非常的美麗
 以前的UI設計都只有X、Y軸這個兩個方向,現在多出來一個垂直於手機屏幕的Z軸,那麼設置Z軸的高度,然後配合光線,然後就在UI的下方看到陰影,這樣就實現了擬物效果。
        View的Z軸由兩部分組成,elevation和translationZ,這兩個屬性都是5.0以後才引入的。elevation是靜態的成員,translationZ可以在代碼中使用來實現動畫效果,他們的關係是:

      Z = elevation + translationZ

elevation是在XML佈局文件中使用,如果android版本小於5.0設置的elevation是不生效的,只有大於5.0的android系統設置elevation纔行;下面是xm示例
<?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"
    tools:context="com.example.chengguo.paletteexample.MainActivity"
    android:background="@android:color/white">

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp" 
        android:layout_margin="10dp"
        android:background="@android:color/holo_blue_dark" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:elevation="10dp"
        android:layout_margin="10dp"
        android:background="@android:color/holo_blue_dark" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:elevation="20dp"
        android:background="@android:color/holo_blue_dark" />

</LinearLayout>

效果圖如下:



然而,在java代碼中要使用setTranslationZ()來動態改變視圖的高度
        通常是使用屬性動畫來爲視圖高改變的時候增加一個動畫效果,例如:


<strong>    if (flag){
            view.animate.translationZ(100);
            flag = false;
        }else {
            view.animate.translationZ(0);
            flag = true;
        }</strong>

3、着色和裁剪

      在andorid5.0中還增加了兩個非常實用的功能:Tinting(着色)和Clipping(裁剪)
        3.1  使用Tinting非常簡單,只需要在XML文件中使用tint和tintMode就行了,有幾種配合效果,t它的實質是通過修改圖像的Alpha遮罩層來修改圖像的顏色,從而達到重新着色的目的。對圖像處理使用起來非常方便 如下:

<?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:background="@android:color/white"
    android:gravity="center|center_horizontal"
    android:orientation="vertical"
    tools:context="com.example.chengguo.paletteexample.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="10dp"
        android:src="@mipmap/ic_launcher" />


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="10dp"
        android:src="@mipmap/ic_launcher"
        android:tint="@color/colorAccent" />


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="10dp"
        android:src="@mipmap/ic_launcher"
        android:tint="@color/colorAccent"
        android:tintMode="add" />


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="10dp"
        android:src="@mipmap/ic_launcher"
        android:tint="@color/colorAccent"
        android:tintMode="multiply" />


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="10dp"
        android:src="@mipmap/ic_launcher"
        android:tint="@color/colorAccent"
        android:tintMode="screen" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="10dp"
        android:src="@mipmap/ic_launcher"
        android:tint="@color/colorAccent"
        android:tintMode="src_atop" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="10dp"
        android:src="@mipmap/ic_launcher"
        android:tint="@color/colorAccent"
        android:tintMode="src_in" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="10dp"
        android:src="@mipmap/ic_launcher"
        android:tint="@color/colorAccent"
        android:tintMode="src_over" />
</LinearLayout>


     3.2  Clipping裁剪,它可以改變一個視圖的外觀,首先,要使用ViewOutlineProvider來修改outline,然後再通過setOutlineProvider將outline作用給視圖;下面使用IamgeView通過Clipping裁剪成圓角正方形和一個圓形;示例如下:
<?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:background="@android:color/white"
    android:gravity="center|center_horizontal"
    android:orientation="vertical"
    tools:context="com.example.chengguo.paletteexample.MainActivity">

    <ImageView
        android:id="@+id/image_rect"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:elevation="2dp" />

    <ImageView
        android:id="@+id/image_circle"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="20dp"
        android:elevation="2dp" />
</LinearLayout>

package com.example.chengguo.paletteexample;

import android.graphics.Outline;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView mRectView;
    private ImageView mCircleView;

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

        mRectView = (ImageView) findViewById(R.id.image_rect);
        mCircleView = (ImageView) findViewById(R.id.image_circle);
        //獲取outline
        ViewOutlineProvider outLine1 = new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                //修改outline爲特定形狀
                outline.setRoundRect(0,0,view.getWidth(),view.getHeight(),10);
            }
        };

        //獲取outline
        ViewOutlineProvider outline2 = new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setOval(0,0,view.getWidth(),view.getHeight());
            }
        };
        //重新爲兩個imageView設置外形
        mRectView.setOutlineProvider(outLine1);
        mCircleView.setOutlineProvider(outline2);
    }
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章