Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Demonstrating morphing a search icon into a search field. To do this we use an AnimatedVectorDrawable (https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable.html) made up of two paths. The first is the search icon (as a single line) the second is the horizontal bar. We then animate the 'trimPathStart' property …
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="150dp"
android:height="24dp"
android:viewportWidth="150"
android:viewportHeight="24">
<path
android:name="search"
android:pathData="@string/path_search"
android:strokeWidth="2"
android:strokeColor="#ffffff"
android:strokeAlpha="0.8"
android:strokeLineCap="round" />
<path
android:name="bar"
android:pathData="M0,23 L149,23"
android:strokeWidth="2"
android:strokeColor="#ffffff"
android:strokeAlpha="0.8"
android:strokeLineCap="square" />
</vector>
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/search_bar">
<target
android:name="search"
android:animation="@animator/anim_search_empty" />
<target
android:name="bar"
android:animation="@animator/anim_bar_fill" />
</animated-vector>
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="trimPathStart"
android:valueFrom="0"
android:valueTo="1"
android:valueType="floatType"
android:duration="@integer/duration_search"
android:interpolator="@android:interpolator/fast_out_slow_in" />
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="trimPathStart"
android:valueFrom="1"
android:valueTo="0"
android:valueType="floatType"
android:duration="@integer/duration_bar"
android:interpolator="@android:interpolator/linear_out_slow_in" />
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<FrameLayout
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:colorPrimary"
tools:context=".MainActivity"
android:onClick="animate">
<ImageView
android:id="@+id/search"
android:layout_gravity="center"
android:layout_width="150dp"
android:layout_height="24dp"
android:src="@drawable/search_bar"
android:onClick="animate" />
<EditText
android:id="@+id/text"
android:layout_width="150dp"
android:layout_height="22dp"
android:layout_gravity="center"
android:background="@null"
android:textSize="12sp"
android:paddingStart="4dp"
android:textColor="?android:colorAccent"
android:alpha="0" />
</FrameLayout>
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.example.searchbar;
import android.app.Activity;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ImageView iv;
private TextView text;
private AnimatedVectorDrawable searchToBar;
private AnimatedVectorDrawable barToSearch;
private float offset;
private Interpolator interp;
private int duration;
private boolean expanded = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.search);
text = (TextView) findViewById(R.id.text);
searchToBar = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.anim_search_to_bar);
barToSearch = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.anim_bar_to_search);
interp = AnimationUtils.loadInterpolator(this, android.R.interpolator.linear_out_slow_in);
duration = getResources().getInteger(R.integer.duration_bar);
// iv is sized to hold the search+bar so when only showing the search icon, translate the
// whole view left by half the difference to keep it centered
offset = -71f * (int) getResources().getDisplayMetrics().scaledDensity;
iv.setTranslationX(offset);
}
public void animate(View view) {
if (!expanded) {
iv.setImageDrawable(searchToBar);
searchToBar.start();
iv.animate().translationX(0f).setDuration(duration).setInterpolator(interp);
text.animate().alpha(1f).setStartDelay(duration - 100).setDuration(100).setInterpolator(interp);
} else {
iv.setImageDrawable(barToSearch);
barToSearch.start();
iv.animate().translationX(offset).setDuration(duration).setInterpolator(interp);
text.setAlpha(0f);
}
expanded = !expanded;
}
}
@eyecatchup

This comment has been minimized.

Show comment Hide comment
@eyecatchup

eyecatchup Feb 26, 2015

Nice 👍

Nice 👍

@david4034647

This comment has been minimized.

Show comment Hide comment
@david4034647

david4034647 Feb 27, 2015

so cool

so cool

@Frank-Zhu

This comment has been minimized.

Show comment Hide comment
@Frank-Zhu

Frank-Zhu Feb 27, 2015

cool

cool

@mustafasevgi

This comment has been minimized.

Show comment Hide comment
@mustafasevgi

mustafasevgi Feb 27, 2015

awesome

awesome

@chrissd23

This comment has been minimized.

Show comment Hide comment
@chrissd23

chrissd23 Feb 27, 2015

so cool 👍

so cool 👍

@kongnanlive

This comment has been minimized.

Show comment Hide comment
@kongnanlive

kongnanlive Feb 28, 2015

cool

cool

@kongnanlive

This comment has been minimized.

Show comment Hide comment
@kongnanlive

kongnanlive Feb 28, 2015

android:pathData="" path_search?

android:pathData="" path_search?

@lvleo

This comment has been minimized.

Show comment Hide comment
@lvleo

lvleo Feb 28, 2015

so cool!

lvleo commented Feb 28, 2015

so cool!

@MarkMjw

This comment has been minimized.

Show comment Hide comment
@MarkMjw

MarkMjw Feb 28, 2015

"anim_bar_to_search.xml" and "" is missing?

MarkMjw commented Feb 28, 2015

"anim_bar_to_search.xml" and "" is missing?

@crosswall

This comment has been minimized.

Show comment Hide comment
@crosswall

crosswall Feb 28, 2015

cool

cool

@baoyongzhang

This comment has been minimized.

Show comment Hide comment
@baoyongzhang

baoyongzhang Feb 28, 2015

cool !!

cool !!

@cokeby190

This comment has been minimized.

Show comment Hide comment
@cokeby190

cokeby190 Feb 28, 2015

Super cool!

Super cool!

@AndroidDeveloperLB

This comment has been minimized.

Show comment Hide comment
@AndroidDeveloperLB

AndroidDeveloperLB Feb 28, 2015

There are missing files... How did you guys test this?
Also, why can't it be in a normal Github repository?

There are missing files... How did you guys test this?
Also, why can't it be in a normal Github repository?

@qiugang

This comment has been minimized.

Show comment Hide comment
@qiugang

qiugang Mar 1, 2015

so cool!

qiugang commented Mar 1, 2015

so cool!

@ZhaoYukai

This comment has been minimized.

Show comment Hide comment
@ZhaoYukai

ZhaoYukai Mar 1, 2015

"anim_bar_to_search.xml" and "" is missing

"anim_bar_to_search.xml" and "" is missing

@Fichardu

This comment has been minimized.

Show comment Hide comment
@Fichardu

Fichardu Mar 1, 2015

anim_bar_to_search.xml

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
                 android:drawable="@drawable/search_bar" >
    <target
        android:animation="@animator/anim_search_fill"
        android:name="search"/>
    <target
        android:animation="@animator/anim_bar_empty"
        android:name="bar"/>
</animated-vector>

"M141,17 A9,9 0 1,1 142,16 L149,23"

Fichardu commented Mar 1, 2015

anim_bar_to_search.xml

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
                 android:drawable="@drawable/search_bar" >
    <target
        android:animation="@animator/anim_search_fill"
        android:name="search"/>
    <target
        android:animation="@animator/anim_bar_empty"
        android:name="bar"/>
</animated-vector>

"M141,17 A9,9 0 1,1 142,16 L149,23"
@interchen

This comment has been minimized.

Show comment Hide comment
@interchen

interchen Mar 2, 2015

DIAO!

DIAO!

@RoadsInFarAway

This comment has been minimized.

Show comment Hide comment
@RoadsInFarAway

RoadsInFarAway Mar 5, 2015

very nb!

very nb!

@blackiedm

This comment has been minimized.

Show comment Hide comment
@blackiedm

blackiedm Mar 9, 2015

very nb!

very nb!

@AndroidDeveloperLB

This comment has been minimized.

Show comment Hide comment
@AndroidDeveloperLB

AndroidDeveloperLB Mar 9, 2015

@Fichardu but now "anim_bar_empty" and "anim_search_fill" are missing...
Can anyone please publish the full sample?

@Fichardu but now "anim_bar_empty" and "anim_search_fill" are missing...
Can anyone please publish the full sample?

@SrishtiRoy

This comment has been minimized.

Show comment Hide comment
@SrishtiRoy

SrishtiRoy Mar 10, 2015

so cool

so cool

@MikeWong

This comment has been minimized.

Show comment Hide comment
@MikeWong

MikeWong Mar 10, 2015

nice

nice

@liuchenx

This comment has been minimized.

Show comment Hide comment
@wangyeming

This comment has been minimized.

Show comment Hide comment
@wangyeming

wangyeming May 6, 2015

cool!

cool!

@YoungLeeForeverBoy

This comment has been minimized.

Show comment Hide comment
@YoungLeeForeverBoy

YoungLeeForeverBoy Sep 8, 2015

cool!

@anjanibansal

This comment has been minimized.

Show comment Hide comment
@anjanibansal

anjanibansal Dec 23, 2015

Could you please share the complete project? For people like me, who know little about vectors or animations, it's a little difficult to get this to work. This is pretty cool and we can learn a lot from it. Please consider my request.

Could you please share the complete project? For people like me, who know little about vectors or animations, it's a little difficult to get this to work. This is pretty cool and we can learn a lot from it. Please consider my request.

@Jason-Lee-1001

This comment has been minimized.

Show comment Hide comment
@Jason-Lee-1001

Jason-Lee-1001 Apr 16, 2016

anim_bar_empty.xml
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:propertyName="trimPathStart" android:valueFrom="1" android:valueTo="0" android:valueType="floatType" android:duration="500" android:interpolator="@android:interpolator/fast_out_slow_in" />

anim_search_fill.xml
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:propertyName="trimPathStart" android:valueFrom="0" android:valueTo="1" android:valueType="floatType" android:duration="500" android:interpolator="@android:interpolator/linear_out_slow_in" />

anim_bar_empty.xml
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:propertyName="trimPathStart" android:valueFrom="1" android:valueTo="0" android:valueType="floatType" android:duration="500" android:interpolator="@android:interpolator/fast_out_slow_in" />

anim_search_fill.xml
<?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:propertyName="trimPathStart" android:valueFrom="0" android:valueTo="1" android:valueType="floatType" android:duration="500" android:interpolator="@android:interpolator/linear_out_slow_in" />

@Github-lxz

This comment has been minimized.

Show comment Hide comment
@Github-lxz

Github-lxz Aug 20, 2016

楼主,我还是菜鸟,求完整的代码,发到我QQ邮箱:455207024@qq.com可以么?好希望楼主能给,菜鸟成长之路好艰难啊,求给无私的帮助。感谢

Github-lxz commented Aug 20, 2016

楼主,我还是菜鸟,求完整的代码,发到我QQ邮箱:455207024@qq.com可以么?好希望楼主能给,菜鸟成长之路好艰难啊,求给无私的帮助。感谢

@huilinger

This comment has been minimized.

Show comment Hide comment
@huilinger

huilinger Mar 26, 2017

good o( ̄▽ ̄)d

good o( ̄▽ ̄)d

@gukandanmobile

This comment has been minimized.

Show comment Hide comment
@gukandanmobile

gukandanmobile Mar 31, 2017

very nice

very nice

@whenthemorningcomes

This comment has been minimized.

Show comment Hide comment
@whenthemorningcomes

whenthemorningcomes Jun 21, 2017

How to show only the search icon at the start of activity? And after this search icon click event, the animation should start. Any idea?

How to show only the search icon at the start of activity? And after this search icon click event, the animation should start. Any idea?

@NinjaChen

This comment has been minimized.

Show comment Hide comment
@NinjaChen

NinjaChen Aug 17, 2017

lacks the string file ""

lacks the string file ""

@weizhizhanghao

This comment has been minimized.

Show comment Hide comment
@weizhizhanghao

weizhizhanghao Aug 17, 2017

very nb

very nb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment