概念
TransitionDrawable是LayerDrawable的子類翅娶,主要用于實現(xiàn)LayerDrawable兩層之間的漸變效果,開啟第一層到第二層的漸變只需要調(diào)用startTransition(int)即可士聪,再調(diào)用resetTransition()顯示第一層。
官方文檔內(nèi)容
(1)繼承結構:
(2)xml屬性(item中的屬性,下文有例子):
Attribute Name | Description |
---|---|
android:bottom | Bottom coordinate of the layer. |
android:drawable | Drawable used to render the layer. |
android:id | Identifier of the layer. |
android:left | Left coordinate of the layer. |
android:right | Right coordinate of the layer. |
android:top | Top coordinate of the layer. |
(3)方法:
返回類型 | 方法名 | 方法描述 |
---|---|---|
void | draw(Canvas canvas) | Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter). |
boolean | isCrossFadeEnabled() | Indicates whether the cross fade is enabled for this transition. |
void | resetTransition() | Show only the first layer. |
void | reverseTransition(int duration) | Reverses the transition, picking up where the transition currently is. |
void | setCrossFadeEnabled(boolean enabled) | Enables or disables the cross fade of the drawables. |
void | startTransition(int durationMillis) | Begin the second layer on top of the first layer. |
使用方式
一 、xml中定義TransitionDrawable的方式
- 定義TransitionDrawable的xml
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/dengpao_off"/>
<item android:drawable="@drawable/dengpao_on"/>
</transition>
- 調(diào)用界面的xml布局
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LightTransitionActivity">
<ImageView
android:id="@+id/iv_light"
android:layout_width="80dp"
android:layout_height="80dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:src="@drawable/dengpao"/>
<Switch
android:id="@+id/switchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="20dp"
android:showText="true"
android:textOff="關"
android:textOn="開"
app:layout_constraintTop_toBottomOf="@+id/iv_light"/>
</androidx.constraintlayout.widget.ConstraintLayout>
- Java代碼中調(diào)用代碼片段
transitionDrawable = (TransitionDrawable) iv_light.getDrawable();
//transitionDrawable.startTransition(1000);
switchView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
transitionDrawable.reverseTransition(1000);
}
});
-
實現(xiàn)效果:
燈.gif
二犯眠、Java代碼中定義TransitionDrawable的使用方式
界面xml布局如下:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TransitionActivity">
<ImageView
android:id="@+id/iv_show"
android:layout_width="270dp"
android:layout_height="480dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
背景container,用于展示高斯模糊背景圖症革,ImageView用于展示用于高斯模糊的圖片筐咧。
Java代碼片段如下:
private void handleTransition(int position){
iv_show.setImageResource(mipmaps.get(position));
Bitmap resBlurBmp = BlurBitmapUtil.blurBitmap(this, BitmapFactory.decodeResource(getResources(),mipmaps.get(position)),15f);
// 再將resBlurBmp轉(zhuǎn)為Drawable
Drawable resBlurDrawable = new BitmapDrawable(getResources(),resBlurBmp);
// 獲取前一頁的Drawable
Drawable preBlurDrawable = preDrawable == null ? resBlurDrawable : preDrawable;
/* 以下為淡入淡出效果 */
Drawable[] drawableArr = {preBlurDrawable, resBlurDrawable};
TransitionDrawable transitionDrawable = new TransitionDrawable(drawableArr);
transitionDrawable.startTransition(500);
container.setBackground(transitionDrawable);
//更新前一次drawable為最新
preDrawable = resBlurDrawable;
}
例子中,我們用一組mipmap圖片構成一個列表噪矛,當點擊圖片時切換到下一張量蕊,循環(huán)切換,背景進行高斯模糊摩疑,同時使用TransitionDrawable來實現(xiàn)切換圖片背景的平滑過渡危融。
實現(xiàn)效果:
切換圖片平滑過渡.gif
附上高斯模糊的代碼:
public class BlurBitmapUtil {
//圖片縮放比例
private static final float BITMAP_SCALE = 0.4f;
/**
* 模糊圖片的具體方法
*
* @param context 上下文對象
* @param image 需要模糊的圖片
* @return 模糊處理后的圖片
*/
public static Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) {
// 計算圖片縮小后的長寬
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * BITMAP_SCALE);
// 將縮小后的圖片做為預渲染的圖片
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
// 創(chuàng)建一張渲染后的輸出圖片
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
// 創(chuàng)建RenderScript內(nèi)核對象
RenderScript rs = RenderScript.create(context);
// 創(chuàng)建一個模糊效果的RenderScript的工具對象
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
// 由于RenderScript并沒有使用VM來分配內(nèi)存,所以需要使用Allocation類來創(chuàng)建和分配內(nèi)存空間
// 創(chuàng)建Allocation對象的時候其實內(nèi)存是空的,需要使用copyTo()將數(shù)據(jù)填充進去
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
// 設置渲染的模糊程度, 25f是最大模糊度
blurScript.setRadius(blurRadius);
// 設置blurScript對象的輸入內(nèi)存
blurScript.setInput(tmpIn);
// 將輸出數(shù)據(jù)保存到輸出內(nèi)存中
blurScript.forEach(tmpOut);
// 將數(shù)據(jù)填充到Allocation中
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
}
最后附上一篇簡單介紹Drawable及Drawable各個子類的文章:
Drawable圖像資源抽象類