ConstraintLayout動態(tài)代碼與動畫

大家好似嗤,ConstraintLayout翻譯成中文也稱為約束布局旷偿,在整個使用體驗(yàn)過程中真的是貫穿約束二字砚作,這一節(jié)先來介紹一些基礎(chǔ)使用懂扼,后面你就會慢慢感受到約束布局的魅力禁荸。

首先看一下實(shí)現(xiàn)的效果吧,


首先介紹布局文件代碼實(shí)現(xiàn)activiy_main:


xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/contentPanel"

tools:context="com.example.edianzu.nweproject.MainActivity"

tools:layout_editor_absoluteY="81dp"

tools:layout_editor_absoluteX="0dp">

android:id="@+id/start"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Change"

app:layout_constraintEnd_toEndOf="parent"

android:layout_marginEnd="8dp"

app:layout_constraintStart_toStartOf="parent"

android:layout_marginStart="8dp"

app:layout_constraintTop_toTopOf="parent"

android:layout_marginTop="8dp"

app:layout_constraintBottom_toBottomOf="parent"

android:layout_marginBottom="8dp"

app:layout_constraintHorizontal_bias="0.028"

app:layout_constraintVertical_bias="0.0"/>

android:id="@+id/reset"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Reset"

app:layout_constraintEnd_toEndOf="parent"

android:layout_marginEnd="8dp"

app:layout_constraintStart_toStartOf="parent"

android:layout_marginStart="8dp"

app:layout_constraintTop_toTopOf="parent"

android:layout_marginTop="8dp"

app:layout_constraintBottom_toBottomOf="parent"

android:layout_marginBottom="8dp"

app:layout_constraintVertical_bias="0.0"

android:layout_marginRight="0dp"

app:layout_constraintRight_toRightOf="parent"

android:layout_marginLeft="8dp"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintHorizontal_bias="0.944"/>

android:id="@+id/imageView"

android:layout_width="50dp"

android:layout_height="50dp"

android:layout_marginLeft="32dp"

android:layout_marginRight="8dp"

app:layout_constraintHorizontal_bias="0.214"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:srcCompat="@mipmap/ic_launcher"

app:layout_constraintTop_toTopOf="parent"

android:layout_marginTop="8dp"

app:layout_constraintBottom_toBottomOf="parent"

android:layout_marginBottom="8dp"/>

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="TextView"

app:layout_constraintTop_toTopOf="parent"

android:layout_marginTop="8dp"

app:layout_constraintBottom_toBottomOf="parent"

android:layout_marginBottom="8dp"

app:layout_constraintLeft_toRightOf="@+id/imageView"

android:layout_marginLeft="8dp"

android:layout_marginRight="8dp"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintHorizontal_bias="0.375"/>


布局代碼activity2:


xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/contentPanel"

tools:context="com.example.edianzu.nweproject.MainActivity"

tools:layout_editor_absoluteY="81dp"

tools:layout_editor_absoluteX="0dp">

android:id="@+id/start"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Change"

app:layout_constraintEnd_toEndOf="parent"

android:layout_marginEnd="8dp"

app:layout_constraintStart_toStartOf="parent"

android:layout_marginStart="8dp"

app:layout_constraintTop_toTopOf="parent"

android:layout_marginTop="8dp"

app:layout_constraintBottom_toBottomOf="parent"

android:layout_marginBottom="8dp"

app:layout_constraintHorizontal_bias="0.028"

app:layout_constraintVertical_bias="0.0"/>

android:id="@+id/reset"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Reset"

app:layout_constraintEnd_toEndOf="parent"

android:layout_marginEnd="8dp"

app:layout_constraintStart_toStartOf="parent"

android:layout_marginStart="8dp"

app:layout_constraintTop_toTopOf="parent"

android:layout_marginTop="8dp"

app:layout_constraintBottom_toBottomOf="parent"

android:layout_marginBottom="8dp"

app:layout_constraintVertical_bias="0.0"

android:layout_marginRight="0dp"

app:layout_constraintRight_toRightOf="parent"

android:layout_marginLeft="8dp"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintHorizontal_bias="0.944"/>

android:id="@+id/imageView"

android:layout_width="150dp"

android:layout_height="150dp"

android:layout_marginLeft="32dp"

android:layout_marginRight="8dp"

app:layout_constraintHorizontal_bias="0.438"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:srcCompat="@mipmap/ic_launcher"

app:layout_constraintTop_toTopOf="parent"

android:layout_marginTop="8dp"

app:layout_constraintBottom_toBottomOf="parent"

android:layout_marginBottom="8dp"/>

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="TextView"

android:layout_marginTop="8dp"

app:layout_constraintBottom_toBottomOf="parent"

android:layout_marginBottom="8dp"

app:layout_constraintVertical_bias="0.231"

app:layout_constraintTop_toBottomOf="@+id/imageView"

android:layout_marginLeft="8dp"

app:layout_constraintLeft_toLeftOf="parent"

android:layout_marginRight="8dp"

app:layout_constraintRight_toRightOf="parent"/>


主頁代碼實(shí)現(xiàn)MainActivity:

packagecom.example.edianzu.nweproject;

importandroid.os.Build;

importandroid.os.Bundle;

importandroid.support.annotation.RequiresApi;

importandroid.support.constraint.ConstraintLayout;

importandroid.support.constraint.ConstraintSet;

importandroid.support.transition.TransitionManager;

importandroid.support.v7.app.AppCompatActivity;

importandroid.view.View;

importandroid.widget.Button;

public classMainActivityextendsAppCompatActivity {

privateConstraintSetconstraintSet1,constraintSet2;

privateConstraintLayoutfirstLayout;

privateButtonstart;

privateButtonreset;

booleanisFirst=true;

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

constraintSet1=newConstraintSet();

constraintSet2=newConstraintSet();

firstLayout= (ConstraintLayout) findViewById(R.id.contentPanel);

start= (Button) findViewById(R.id.start);

reset= (Button) findViewById(R.id.reset);

constraintSet1.clone(firstLayout);

constraintSet2.clone(this,R.layout.activity2);

start.setOnClickListener(newView.OnClickListener() {

@RequiresApi(api=Build.VERSION_CODES.KITKAT)

@Override

public voidonClick(View view) {

constraintSet2.applyTo(firstLayout);

}

});

reset.setOnClickListener(newView.OnClickListener() {

@RequiresApi(api=Build.VERSION_CODES.KITKAT)

@Override

public voidonClick(View view) {

constraintSet1.applyTo(firstLayout);

}

});

}

}

當(dāng)前代碼實(shí)現(xiàn)后的效果是這樣的沒有過渡動畫效果:


想要實(shí)現(xiàn)動畫的過渡效果需要引入一個庫:

compile'com.android.support:transition:25.3.1'

然后在代碼中的? ? constraintSet1.applyTo之前添加這一句

TransitionManager.beginDelayedTransition(firstLayout);

就可以實(shí)現(xiàn)動畫過渡效果了

在整個ConstraintLayout的使用過程中微王,遇到了很多的問題屡限,問題不在與xml界面的調(diào)整,而在于代碼中ConstraintSet的使用炕倘,對于控件的設(shè)置上有很多屬性钧大,在很大方面滿足了我們做需要的實(shí)現(xiàn)效果

Demo下載地址https://github.com/jinhui1314/ConstraintLayout

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市罩旋,隨后出現(xiàn)的幾起案子啊央,更是在濱河造成了極大的恐慌,老刑警劉巖涨醋,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瓜饥,死亡現(xiàn)場離奇詭異,居然都是意外死亡浴骂,警方通過查閱死者的電腦和手機(jī)乓土,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人趣苏,你說我怎么就攤上這事狡相。” “怎么了食磕?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵尽棕,是天一觀的道長。 經(jīng)常有香客問我彬伦,道長滔悉,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任单绑,我火速辦了婚禮回官,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘搂橙。我一直安慰自己孙乖,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布份氧。 她就那樣靜靜地躺著唯袄,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蜗帜。 梳的紋絲不亂的頭發(fā)上恋拷,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天,我揣著相機(jī)與錄音厅缺,去河邊找鬼蔬顾。 笑死,一個胖子當(dāng)著我的面吹牛湘捎,可吹牛的內(nèi)容都是我干的诀豁。 我是一名探鬼主播,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼窥妇,長吁一口氣:“原來是場噩夢啊……” “哼舷胜!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起活翩,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤烹骨,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后材泄,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體沮焕,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年拉宗,在試婚紗的時候發(fā)現(xiàn)自己被綠了峦树。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辣辫。...
    茶點(diǎn)故事閱讀 40,498評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖魁巩,靈堂內(nèi)的尸體忽然破棺而出络它,到底是詐尸還是另有隱情,我是刑警寧澤歪赢,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站单料,受9級特大地震影響埋凯,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜扫尖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一白对、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧换怖,春花似錦甩恼、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至铸屉,卻和暖如春钉蒲,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背彻坛。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工顷啼, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人昌屉。 一個月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓钙蒙,卻偏偏與公主長得像,于是被迫代替她去往敵國和親间驮。 傳聞我的和親對象是個殘疾皇子躬厌,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,507評論 2 359

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