TransitionDrawable的基本使用

概念

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圖像資源抽象類

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末畏铆,一起剝皮案震驚了整個濱河市雷袋,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖楷怒,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蛋勺,死亡現(xiàn)場離奇詭異,居然都是意外死亡鸠删,警方通過查閱死者的電腦和手機抱完,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來刃泡,“玉大人巧娱,你說我怎么就攤上這事『嫣” “怎么了禁添?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長桨踪。 經(jīng)常有香客問我老翘,道長,這世上最難降的妖魔是什么锻离? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任铺峭,我火速辦了婚禮,結果婚禮上汽纠,老公的妹妹穿的比我還像新娘卫键。我一直安慰自己,他們只是感情好疏虫,可當我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布永罚。 她就那樣靜靜地躺著,像睡著了一般卧秘。 火紅的嫁衣襯著肌膚如雪呢袱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天翅敌,我揣著相機與錄音羞福,去河邊找鬼。 笑死蚯涮,一個胖子當著我的面吹牛治专,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播遭顶,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼张峰,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了棒旗?” 一聲冷哼從身側響起喘批,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎季惩,沒想到半個月后降淮,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體烂完,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡勘伺,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年适室,在試婚紗的時候發(fā)現(xiàn)自己被綠了嚷兔。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片虐骑。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡叽奥,死狀恐怖俱两,靈堂內(nèi)的尸體忽然破棺而出饱狂,到底是詐尸還是另有隱情,我是刑警寧澤宪彩,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布嗡官,位于F島的核電站,受9級特大地震影響毯焕,放射性物質(zhì)發(fā)生泄漏衍腥。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一纳猫、第九天 我趴在偏房一處隱蔽的房頂上張望婆咸。 院中可真熱鬧,春花似錦芜辕、人聲如沸尚骄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽倔丈。三九已至,卻和暖如春状蜗,著一層夾襖步出監(jiān)牢的瞬間需五,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工轧坎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留宏邮,地道東北人。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓缸血,卻偏偏與公主長得像蜜氨,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子捎泻,可洞房花燭夜當晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容