Android_仿微信的底部弹出框(带动画)


Android_仿微信的底部弹出框(带动画)先来看运行的效果:



































逻辑如下:
package com.aoyi.popwindow_donghua;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private PopupWindow popupWindow;
private TextView tv_tanchu;
private View view;
private RelativeLayout layout_zong;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_tanchu = (TextView) findViewById(R.id.tv_tanchu);
layout_zong = (RelativeLayout) findViewById(R.id.layout_zong);

tv_tanchu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.pop, null);
//给popwindow加上动画效果
LinearLayout layout_pop = (LinearLayout) view.findViewById(R.id.layout_pop);
view.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in));
layout_pop.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_bottom_in));
//设置popwindow的宽高,这里我直接获取了手机屏幕的宽,高设置了600DP
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
popupWindow = new PopupWindow(view, dm.widthPixels, 600);

// 使其聚集
popupWindow.setFocusable(true);
// 设置允许在外点击消失
popupWindow.setOutsideTouchable(true);

// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
popupWindow.setBackgroundDrawable(new BitmapDrawable());
backgroundAlpha(120f); //透明度
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
backgroundAlpha(1f);

}
});
//弹出的位置
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
popupWindow.showAtLocation(layout_zong, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}
});
}

/**
* 设置添加屏幕的背景透明度
*
* @param bgAlpha
*/
public void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getWindow().setAttributes(lp);
}
}



里面解释的很明白了,里面用到了动画,下面是动画,放在anim文件里:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="400"
android:fromAlpha="0.0"
android:toAlpha="1.0" />



<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="400"
android:fromYDelta="100%"
android:interpolator="@android:anim/decelerate_interpolator"
android:toYDelta="0" />




最后就是一个弹出框的布局了:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ffffff"
android:id="@+id/layout_pop"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="18sp"
android:gravity="center"
android:text="Hello world"/>
</LinearLayout>



智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告