English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
最近产品出了个新需求,页面上出现浮层并且可点击,代码实现如下:
Activity中实现浮层图片:
@Override public void onResume() { super.onResume(); createView(); } @Override public void onPause() { super.onPause(); / 在程序退出(Activity销毁)时销毁悬浮窗口 if(floatView!=null && windowManager !=null) { windowManager.removeView(floatView); floatView=null; windowManager = null; windowManagerParams = null; }} private void createView() { if(floatView!=null) return ; CmsAPI cmsAPI = RestAdapterUtils.getRestAPI(Config.NEW_CMS_URL, CmsAPI.class, this); cmsAPI.getFloatingAd(new Callback<AdFloating>() {//请求数据 @Override public void success(AdFloating adFloating, Response response) {}} if (adFloating != null && "0".equals(adFloating.getErrorCode())) { long startTime = adFloating.getStarttime(); long endTime = adFloating.getEndtime(); long currentTime = System.currentTimeMillis(); // LOGD(startTime + " +++++ "+endTime +" "+currentTime +" "+(currentTime > startTime && currentTime < endTime)); if (currentTime > startTime && currentTime < endTime) {//Dauer der Aktivität floatView = new FloatView(getApplicationContext()); floatView.setOnClickListener(MainActivity.this); int height = 240; int width = 110; float ratio= 1.35f; if (!TextUtils.isEmpty(adFloating.getImg2)) { try { height = Integer.parseInt(adFloating.getImg2h()); width = Integer.parseInt(adFloating.getImg2w()); ratio = (float) width / height; } catch (Exception e) { ratio = 1.35f; } } // floatView.setAspectRatio(ratio);//Bildgröße floatView.setImageURI(Uri.parse(adFloating.getImg2))));//Setzen Sie die Netzwerkadresse des Bildes // floatView.setImageResource(R.drawable.face_icon); // Hier wird eine einfache Demonstration mit dem integrierten Icon durchgeführt // Get WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // Set parameters related to LayoutParams (global variables) windowManagerParams = ((MiGuApplication) getApplication()).getWindowParams(); windowManagerParams.type = WindowManager.LayoutParams.TYPE_PHONE; // Set window type windowManagerParams.format = PixelFormat.RGBA_8888; // Set image format, effect is background transparent // Set Window flag windowManagerParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; /* * Note that the value of flag can be: * LayoutParams.FLAG_NOT_TOUCH_MODAL does not affect subsequent events * LayoutParams.FLAG_NOT_FOCUSABLE not focusable * LayoutParams.FLAG_NOT_TOUCHABLE not touchable */ // Adjust the floating window to the top-left corner for easy adjustment of coordinates windowManagerParams.gravity = Gravity.LEFT | Gravity.TOP; // Set the initial values of x and y with the top-left corner of the screen as the origin DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int screenWidth = dm.widthPixels; int screenHeigh = dm.heightPixels; int x = screenWidth - SystemTools.dip2px(MainActivity.this, 100); int y= screenHeigh - SystemTools.dip2px(MainActivity.this, 200); windowManagerParams.x = x; windowManagerParams.y = y; // Die Abmessungen des schwebenden Fensters einstellen windowManagerParams.width = width;//Die Breite des Fensters auf die Bildgröße einstellen windowManagerParams.height = height;//Die Höhe des Fensters auf die Bildgröße einstellen // windowManagerParams.width = WindowManager.LayoutParams.WRAP_CONTENT; // windowManagerParams.height = WindowManager.LayoutParams.WRAP_CONTENT; // Das Bild von myFloatView anzeigen windowManager.addView(floatView, windowManagerParams); return; } } } @Override public void failure(RetrofitError error) {//Fehlschlag bei der Netzwerkdatenanfrage LOGE(error.getMessage()); } }); } public void onClick(View v) {//Klickereignis des Bildes Intent intent = new Intent(MainActivity.this, ActivitiesDetails.class); startActivity(intent); }
Bildeingabeanlage:
public class FloatView extends SimpleDraweeView { private float mTouchX; private float mTouchY; private float x; private float y; private float mStartX; private float mStartY; private OnClickListener mClickListener; private WindowManager windowManager = (WindowManager) getContext(); .getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // dieser windowManagerParams Variable ist die erfasste globale Variable, um die Eigenschaften des floating Fensters zu speichern private WindowManager.LayoutParams windowManagerParams = ((MiGuApplication) getContext() .getApplicationContext()).getWindowParams(); public FloatView(Context context) { super(context); } public FloatView(Context context, AttributeSet attrs) { super(context, attrs); } private long curtime=0; @Override public boolean onTouchEvent(MotionEvent event) { //Erfassen Sie die Höhe des Statusbars Rect frame = new Rect(); getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; System.out.println("statusBarHeight:")+statusBarHeight); // Erfassen Sie die relative Koordinate zum Bildschirm, d.h. mit dem linken oberen Ecke des Bildschirms als Ursprung x = event.getRawX(); y = event.getRawY() - statusBarHeight; // statusBarHeight ist die Höhe des Systems Statusbars switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // Erfassen Sie den Druck der Fingerspitze // Erfassen Sie die relative Koordinate zum View, d.h. mit dem linken oberen Ecke dieses Views als Ursprung mTouchX = event.getX(); mTouchY = event.getY(); mStartX = x; mStartY = y; break; case MotionEvent.ACTION_MOVE: // Erfassen Sie den Bewegung der Fingerspitze updateViewPosition(); curtime=System.currentTimeMillis(); break; case MotionEvent.ACTION_UP: // Erfassen Sie den Abgang der Fingerspitze // if(System.currentTimeMillis())-curtime>}100){ // break; // } updateViewPosition(); mTouchX = mTouchY = 0; if (Math.abs(x - mStartX) < 5 && Math.abs(y - mStartY) < 5) {//轻微拖动算点击 if(mClickListener!=null) { mClickListener.onClick(this); } } break; } return true; } @Override public void setOnClickListener(OnClickListener l) { this.mClickListener = l; } private void updateViewPosition() { // 更新浮动窗口位置参数 windowManagerParams.x = (int) (x - mTouchX); windowManagerParams.y = (int) (y - mTouchY); windowManager.updateViewLayout(this, windowManagerParams); // 刷新显示 } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
声明:本文内容来源于互联网,版权归原作者所有。内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#oldtoolbag.com(在发送邮件时,请将#更换为@进行举报,并提供相关证据。一经查实,本站将立即删除涉嫌侵权内容。)