/**
* 注意:
* 如果pop包含的有类似listView这样高度不固定的控件,那么是不能直接获取pop高度的
* @author Administrator
* pop显示的位置是可以组合的,pop的大小是可以指定的
*/
public class PopListItem extends BasePop{
private Context context;
private ListView listview;
/**
* @param context 上下文
* @param targetView 基准控件
*/
public PopListItem(Context context,View targetView){
this.context=context;
standardView=targetView;
init();
}
private void init() {
// TODO Auto-generated method stub
LayoutInflater flater=LayoutInflater.from(context);
View view=flater.inflate(R.layout.pop_list_item, null);
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);//强制绘制,方便求长宽。但是ListView却是意外,高度不定
listview=(ListView) view.findViewById(R.id.listview);
filledView=listview;
pop = new PopupWindow(view, context.getResources().getDimensionPixelSize(R.dimen.j80dp), LayoutParams.WRAP_CONTENT, true);
pop.setBackgroundDrawable(new BitmapDrawable(context.getResources()));
}
public void setData(List<String> data){
if(data!=null&&data.size()>0){
listview.setAdapter(new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, android.R.id.text1, data));
// listview.setOnItemClickListener(this);
// pop_hight = pop.getHeight();//=0,此处不可直接获取
}
}
// @Override
// public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// // TODO Auto-generated method stub
// //数据是需要传递给activity的,可以使用广播和编写回调方法
// pop.dismiss();
// }
public void getClickValue(OnItemClickListener onItemClickListener){
listview.setOnItemClickListener(onItemClickListener);
}
}
//
package com.example.popupwindowdemo.util;
import com.example.popupwindowdemo.R;
import android.view.Gravity;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
/**
* 目标控件是靠边的时候,效果比较实用
* @author Administrator
*
*/
public class BasePop {
public PopupWindow pop;
/**
* 填充的View
*/
public View filledView;
/**
* 显示位置的基准View
*/
public View standardView;
/**
* PopupWindow的高度
*/
public int pop_hight;
/**
* 退出PopupWindow
*/
public void dismiss() {
if (pop != null) {
pop.dismiss();
}
}
/**
* 从左到右
*/
public void showDownLeftToRight() {
pop.setAnimationStyle(R.style.LeftToRight);//移动动画的位移相对量是pop自身长宽
pop.showAsDropDown(standardView);
}
/**
* 从右到左
*/
public void showDownRightToLeft() {
pop.setAnimationStyle(R.style.RightToLeft);
pop.showAsDropDown(standardView);
}
/**
* 从左到右,显示在参照组件之上 另外:想获取Fragement同在一个Activity里面的View,比如点击Fragement1里面某个按钮,
* 要求弹出PopupWindow显示在 下面的单选按钮组上,就需要在Fragement1里面获取单选按钮组 的
* View =getActivity().findViewById(R.id.group)
*/
public void showUpLeftToRight() {
pop.setAnimationStyle(R.style.LeftToRight);
int[] location = new int[2];
standardView.getLocationOnScreen(location);//获取目标控件左上角的坐标
//PopupWindow里面包含的listview所以高度是不能直接获取的;而宽度是指定了具体数值的,故可以直接获取
// Toast.makeText(context, "x:"+location[0]+"y:"+location[1]+"pop:"+getListViewHight(listview), 1).show();
//如果ListView里面的item比较少,可以这样通过来求listview的高度作为PopupWindow的高度
pop.showAtLocation(standardView, Gravity.NO_GRAVITY, location[0], location[1]- getListViewHight((ListView)filledView));//
//如果ListView里面的item比较多,那么还是指定PopupWindow的高度
//如果是其他固定布局,比如一个Linearlayout里面包含几个textView,可以直接获取PopupWindow的高度
// pop.showAtLocation(targetView, Gravity.NO_GRAVITY, location[0], location[1]- pop.getHeight());
}
/**
* 获取listView的高度,如果listview里面item比较多,那么还是指定Popupwindow的高度比较好
* @param listView
* @return
*/
public int getListViewHight(ListView listView){
ListAdapter mAdapter = (ListAdapter) listView.getAdapter();
if (mAdapter == null) {
return 0;
}
int totalHeight = 0;
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//mView.measure(0, 0);
totalHeight += mView.getMeasuredHeight();
// Log.w("HEIGHT" + i, String.valueOf(totalHeight));
}
return totalHeight;
}
/**
* 从下到上,显示在界面的最下方
*/
public void showBotom() {
pop.setAnimationStyle(R.style.DownToUp);
pop.showAtLocation(standardView, Gravity.BOTTOM | Gravity.CENTER, 0, 0);
}
/**
* 从左到右,显示在界面下方左侧
*/
public void showBotomLeft() {
pop.setAnimationStyle(R.style.LeftToRight);
pop.showAtLocation(standardView, Gravity.BOTTOM | Gravity.LEFT, 0, 0);
}
}
///
package com.example.popupwindowdemo.act;
import java.util.ArrayList;
import java.util.List;
import com.example.popupwindowdemo.R;
import com.example.popupwindowdemo.pop.PopListItem;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainAct extends Activity implements OnClickListener{
private TextView target;
private Button btn1,btn2,btn3;
private PopListItem popListItem;
private List<String> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.act_main);
target=(TextView) findViewById(R.id.target);
btn1=(Button) findViewById(R.id.btn1);
btn2=(Button) findViewById(R.id.btn2);
btn3=(Button) findViewById(R.id.btn3);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
target.setOnClickListener(this);
popListItem=new PopListItem(MainAct.this, target);
data=new ArrayList<String>();
data.add("价 格");
data.add("评 分");
data.add("销 量");
popListItem.setData(data);
popListItem.getClickValue(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), data.get(arg2), 1).show();
popListItem.dismiss();
}
});
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if(view.equals(btn1)){
popListItem.showDownLeftToRight();
}else if(view.equals(btn2)){
popListItem.showUpLeftToRight();
}else if(view.equals(btn3)){
popListItem.showBotom();
}else if(view.equals(target)){
Intent intent=new Intent(this,HongBaoAct.class);
startActivity(intent);
}
}
}
///
package com.example.popupwindowdemo.act;
import com.example.popupwindowdemo.R;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class HongBaoAct extends Activity{
ImageView move,caidai;
Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.act_hongbao);
move=(ImageView) findViewById(R.id.move);
caidai=(ImageView) findViewById(R.id.caidai);
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
move.setVisibility(View.VISIBLE);
ObjectAnimator objectAnimtor = (ObjectAnimator) AnimatorInflater
.loadAnimator(HongBaoAct.this, R.animator.animtor_move_x);
objectAnimtor.setTarget(move);
objectAnimtor.start();
objectAnimtor.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator arg0) {
// TODO Auto-generated method stub
AnimatorSet animationSet = new AnimatorSet();
animationSet.playTogether(
ObjectAnimator.ofFloat(caidai, "scaleX", 1,0.2f),
ObjectAnimator.ofFloat(caidai, "scaleY", 1,0.5f)
);
animationSet.setDuration(1000);
animationSet.setTarget(caidai);
animationSet.start();
}
@Override
public void onAnimationRepeat(Animator arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animator arg0) {
// TODO Auto-generated method stub
caidai.setVisibility(View.VISIBLE);
AnimatorSet animationSet = new AnimatorSet();
animationSet.playTogether(
ObjectAnimator.ofFloat(caidai, "alpha", 0f,1),
ObjectAnimator.ofFloat(caidai, "scaleX", 0.2f,1),
ObjectAnimator.ofFloat(caidai, "scaleY", 0.5f,1)
);
animationSet.setDuration(1400);
animationSet.setTarget(caidai);
animationSet.start();
Toast.makeText(HongBaoAct.this, "ok", 1).show();
}
@Override
public void onAnimationCancel(Animator arg0) {
// TODO Auto-generated method stub
}
});
}
}, 1200);
}
}
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="100%" android:toYDelta="0" android:duration="200"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="-100%" android:toYDelta="0" android:duration="200"/>
<alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="200"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:duration="200" android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="0" android:toYDelta="100%"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="0" android:toYDelta="-100%" android:duration="200"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.1" android:duration="200"/>
</set>