布局:

main:

<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

pop:

<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"
/>

代码:

package com.example.my_popwindow_donghua;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow; public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private PopupWindow popupWindow;
private Button btn;
private ImageView iv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(); } private void initView() {
btn = (Button) findViewById(R.id.btn); View inflate = LayoutInflater.from(this).inflate(R.layout.pop, null);
iv = (ImageView) inflate.findViewById(R.id.iv);
popupWindow = new PopupWindow(inflate, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
btn.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
// AlphaAnimation alphaAnimation = new AlphaAnimation(1,0); // alphaAnimation.setDuration(2000);
Animation alphaAnimation = AnimationUtils.loadAnimation(this,R.anim.bib);
iv.startAnimation(alphaAnimation); if (popupWindow.isShowing()){ popupWindow.dismiss(); }else { popupWindow.showAsDropDown(btn);
} break;
}
}
}
04-22 15:32