Android5.0的Palette(調色板)、視圖陰影、着色和裁剪介紹
-
Vibrant : 充滿活力的,
-
Vibrant dark :充滿活力的黑
-
Vibrant light :充滿活力的亮
-
Muted :柔和的
-
Muted dark : 柔和的黑
-
Muted light : 柔和的亮
compile 'com.android.support:palette-v7:23.4.0'
<?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>
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);
}
}
<?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>
<strong> if (flag){
view.animate.translationZ(100);
flag = false;
}else {
view.animate.translationZ(0);
flag = true;
}</strong>
<?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>
<?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);
}
}