【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”
public class MapFragment extends Fragment { private PopMenu popMenu; private View imageview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.map, null); findViews(view); initView() ; return view; } private void initView() { // TODO Auto-generated method stub popMenu = new PopMenu(this, R.drawable.tips_bg); imageview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popMenu.show(v, MapFragment.this); } }); } public void findViews(View view) { // TODO Auto-generated method stub imageview= view.findViewById(R.id.imgview); } public void onDestroyView() { super.onDestroy(); } @Override public void onDestroy() { super.onDestroy(); } }
以下是我在网上找到的一个菜单的代码
public class PopMenu { // 存储每个按钮点击后对应生成的drawable private HashMap<View, Drawable> mapDrawable = new HashMap<View, Drawable>(); private Context context; private PopupWindow popupWindow; private ViewPager viewPager; private ArrayList<View> listViews; private int screenwidth;// 屏幕的宽度 private int currentView = 0;// 当前视图 private int viewOffset;// 动画图片偏移量 private int imgWidth;// 图片宽度 private ImageView iv_cursor;// 动画图片 private TextView tv_main; private TextView tv_utils; private TextView tv_set; private int popupWindowHeight = 0;// popupWindow的高度 private Drawable popBg; // 背景图片 private Drawable bg;// 生成的背景图片 public PopMenu(Context context, int drawableID) { this.context = context; LayoutInflater inflater = ((Activity) context).getLayoutInflater(); View view = inflater.inflate(R.layout.popmenu_layout, null); tv_main = (TextView) view.findViewById(R.id.tv_main); tv_utils = (TextView) view.findViewById(R.id.tv_utils); tv_set = (TextView) view.findViewById(R.id.tv_set); /*this.tv_main.setOnClickListener(new myOnClick(0)); this.tv_utils.setOnClickListener(new myOnClick(1)); this.tv_set.setOnClickListener(new myOnClick(2));*/ iv_cursor = (ImageView) view.findViewById(R.id.iv_cursor); setCursorWidth(); viewPager = (ViewPager) view.findViewById(R.id.viewPagerw); viewPager.setFocusableInTouchMode(true); viewPager.setFocusable(true); viewPager.setAdapter(new myPagerAdapter()); viewPager.setOnPageChangeListener(new MyOnPageChangeListener()); popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT, context .getResources().getDimensionPixelSize(R.dimen.popmenu_h)); // 生成drawable this.popBg = context.getResources().getDrawable(drawableID); } public void setCursorWidth() { screenwidth = getScreenWidth(); imgWidth = BitmapFactory.decodeResource(context.getResources(), R.drawable.img_cursor).getWidth();// 获取图片宽度 viewOffset = (screenwidth / 3 - imgWidth) / 2; Matrix matrix = new Matrix(); matrix.postTranslate(viewOffset, 0); iv_cursor.setImageMatrix(matrix); Log.e("TAG", screenwidth + ""); } public int getScreenWidth() { DisplayMetrics dm = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay() .getMetrics(dm); int screenW = dm.widthPixels; return screenW; } public void show(View parent, Fragment mapFragment) { bg = mapDrawable.get(parent);// 为节省资源,map中会保存以前生成的背景,根据父控件来获得 popupWindowHeight = popupWindow.getHeight();// 得到popupWindow的高度,在popupWindow构造完后才能获取 if (bg == null)// 背景为空 { createDrawable(mapFragment);// 重新生成背景图 mapDrawable.put(parent, bg);// 保存到map中 } popupWindow.setBackgroundDrawable(bg);// 给popupWindow设置背景 popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, parent.getHeight());// 距离底部的位置 popupWindow.setAnimationStyle(R.style.popwin_anim_style); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.update(); } public void dismiss() { popupWindow.dismiss(); } /** * 以一个Bitmap为画布,画上一个Bitmap * * @param canvasBitmap * 作为画布的Bitmap * @param drawBitmap * 要被绘制的Bitmap * @param top * 从画布的距离顶部的top位置开始 * @param left * 从画布的距离左边的left位置开始 */ private void drawbitMap(Bitmap canvasBitmap, Bitmap drawBitmap, int top, int left) { Canvas localCanvas = new Canvas(canvasBitmap);// 以canvasBitmap生成画布 localCanvas.drawBitmap(drawBitmap, left, top, null);// 在画布上移left和top左标开始绘制drawBitmap localCanvas.save(Canvas.ALL_SAVE_FLAG);// 保存 localCanvas.restore(); drawBitmap.recycle();// 释放掉drawBitmap,防止内存泄漏 } /** * 把Drawable生成对应的Bitmap * * @param paramRect * 生成的Bitmap大小等一些参数 * @param drawable * 要绘制的drawable * @param canvasBitmap * 将drawable绘制到canvasBitmap中 */ private void getBitMap(Rect paramRect, Drawable drawable, Bitmap canvasBitmap) { drawable.setBounds(0, 0, paramRect.right, paramRect.bottom); // 用canvasBitmap生成一个画布 Canvas localCanvas = new Canvas(canvasBitmap); drawable.draw(localCanvas);// 使用drawable的draw方法画到画布上 localCanvas.save(Canvas.ALL_SAVE_FLAG);// 保存 localCanvas.restore(); } private void createDrawable(Fragment mapFragment) { WindowManager wm = (WindowManager) ((Fragment) mapFragment) .getSystemService(Context.WINDOW_SERVICE); @SuppressWarnings("deprecation") int width = wm.getDefaultDisplay().getWidth(); // 图片的大小等一些参数 Rect arrayOfRect = new Rect(); arrayOfRect.top = 0; arrayOfRect.left = 0; arrayOfRect.right = width; arrayOfRect.bottom = this.popupWindowHeight; // 生成背景 bg = getDrawable(mapFragment, arrayOfRect, popBg); } /** * 生成背景图 */ private Drawable getDrawable(Fragment mapFragment, Rect ArrayOfRect, Drawable ArrayOfDrawable) { Bitmap.Config localConfig = Bitmap.Config.ARGB_8888; // 先更具popupWindow的大小生成一个Bitmap Bitmap paramBitmap = Bitmap.createBitmap(screenwidth, popupWindowHeight, localConfig); Bitmap localBitmap = Bitmap.createBitmap(ArrayOfRect.right, ArrayOfRect.bottom, localConfig); getBitMap(ArrayOfRect, ArrayOfDrawable, localBitmap);// 得到相应的drawable的BitMap drawbitMap(paramBitmap, localBitmap, ArrayOfRect.top, ArrayOfRect.left);// 在paramBitmap中绘制localBitmap localBitmap.recycle();// 释放掉,要不多次运行有可能会内存泄漏 return new BitmapDrawable(mapFragment.getResources(), paramBitmap); } public class myPagerAdapter extends PagerAdapter { @Override public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView(listViews.get(arg1)); } @Override public int getCount() { return listViews.size(); }