Android 带伸缩动画的布局

activity_main.xml布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:orientation=“vertical”

android:padding=“20dp”

android:layout_height=“match_parent”

tools:context=“.MainActivity”>

<RelativeLayout

android:layout_gravity=“right”

android:id=“@+id/lay_search”

android:layout_width=“48dp”

android:layout_height=“48dp”

android:background=“@drawable/shape_search_bg”>

<LinearLayout

android:gravity=“center”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“horizontal”>

<ImageView

android:id=“@+id/iv_search”

android:layout_width=“36dp”

android:layout_height=“36dp”

android:padding=“6dp”

android:onClick=“onClick”

android:src=“@mipmap/icon_search_white” />

<EditText

android:visibility=“gone”

android:id=“@+id/ed_search”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”

android:layout_alignParentTop=“true”

android:layout_marginTop=“0dp”

android:background=“@null”

android:imeOptions=“actionSearch”

android:textCursorDrawable=“@drawable/cursor_style”

android:ems=“8”

android:singleLine=“true”

android:padding=“5dp”

android:textColor=“#FFF”

android:textSize=“14sp”/>

<ImageView

android:id=“@+id/iv_close”

android:layout_width=“36dp”

android:layout_height=“36dp”

android:padding=“8dp”

android:onClick=“onClick”

android:visibility=“gone”

android:src=“@mipmap/icon_colse_white” />

MainActivity代码:

package com.llw.searchanimation;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.TargetApi;

import android.content.Context;

import android.graphics.Color;

import android.os.Build;

import android.os.Bundle;

import android.text.TextUtils;

import android.transition.AutoTransition;

import android.transition.TransitionManager;

import android.util.DisplayMetrics;

import android.view.KeyEvent;

import android.view.MotionEvent;

import android.view.View;

import android.view.ViewGroup;

import android.view.WindowManager;

import android.view.inputmethod.EditorInfo;

import android.view.inputmethod.InputMethodManager;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.RelativeLayout;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private ImageView ivSearch,ivClose;

private EditText edSearch;

private RelativeLayout laySearch;

private AutoTransition autoTransition;

private int width;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

private void initView() {

laySearch = (RelativeLayout) findViewById(R.id.lay_search);

ivSearch = (ImageView) findViewById(R.id.iv_search);

ivClose = (ImageView) findViewById(R.id.iv_close);

edSearch = (EditText) findViewById(R.id.ed_search);

WindowManager manager = getWindowManager();

DisplayMetrics metrics = new DisplayMetrics();

manager.getDefaultDisplay().getMetrics(metrics);

width = metrics.widthPixels; //获取屏幕的宽度 像素

/**

  • 输入法键盘的搜索监听

*/

edSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {

@Override

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

if (actionId == EditorInfo.IME_ACTION_SEARCH) {

String city = edSearch.getText().toString();

if (!TextUtils.isEmpty(city)) {

Toast.makeText(MainActivity.this,city,Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(MainActivity.this,“请输入内容”,Toast.LENGTH_SHORT).show();

}

}

return false;

}

});

}

/**

  • 点击事件

  • @param v

*/

public void onClick(View v) {

switch (v.getId()) {

case R.id.iv_search://点击搜索 伸展

initExpand();

break;

case R.id.iv_close://点击close 关闭

initClose();

break;

}

}

/设置伸展状态时的布局/

public void initExpand() {

edSearch.setHint(“输入城市名”);

edSearch.setHintTextColor(Color.WHITE);

edSearch.setVisibility(View.VISIBLE);

ivClose.setVisibility(View.VISIBLE);

LinearLayout.LayoutParams LayoutParams = (LinearLayout.LayoutParams) laySearch.getLayoutParams();

LayoutParams.width = dip2px(px2dip(width)-40);

LayoutParams.setMargins(dip2px(0), dip2px(0), dip2px(0), dip2px(0));

laySearch.setPadding(14,0,14,0);

laySearch.setLayoutParams(LayoutParams);

edSearch.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

edSearch.setFocusable(true);

edSearch.setFocusableInTouchMode(true);

return false;

}

});

//开始动画

beginDelayedTransition(laySearch);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值