欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android仿苹果关机界面实现代码

程序员文章站 2023-12-16 15:31:04
本文实例为大家分享了android仿苹果关机界面的具体代码,供大家参考,具体内容如下 主class 用来控制viewdialog的显示 package com....

本文实例为大家分享了android仿苹果关机界面的具体代码,供大家参考,具体内容如下

主class 用来控制viewdialog的显示

package com.android.server.policy; 

  import android.app.alertdialog; 
  import android.app.statusbarmanager; 
  import android.os.handler; 
  import android.os.message; 
  import android.os.remoteexception; 
  import android.os.servicemanager; 
  import android.os.systemclock; 
  import android.util.log; 
  import android.view.iwindowmanager; 
  import android.view.keyevent; 
  import android.view.layoutinflater; 
  import android.view.view; 
  import android.view.window; 
  import android.view.windowmanager; 
  import android.view.animation.animation; 
  import android.view.animation.translateanimation; 
  import android.widget.button; 
  import android.widget.linearlayout; 
  import android.content.broadcastreceiver; 
  import android.content.context; 
  import android.content.intent; 
  import android.content.intentfilter; 
  //import android.widget.slidebar; 
  import com.android.internal.r; 
  import android.widget.imageview; 
  import android.view.motionevent; 
  import android.view.view.ontouchlistener; 
  import android.util.log; 
  import android.widget.framelayout; 
  import android.graphics.canvas; 
  import android.graphics.color; 
  import android.graphics.paint; 
  import android.graphics.path; 
  import android.graphics.rectf; 
  import android.graphics.bitmap; 
  import android.graphics.pixelformat; 
  import android.graphics.drawable.drawable; 
  import android.util.attributeset; 
  import android.graphics.drawable.bitmapdrawable; 
  import android.view.display; 
  import android.view.windowmanager; 



  //import com.android.internal.widget.slideview; 

  //import android.view.windowmanagerpolicy.windowmanagerfuncs; 
  import android.view.windowmanagerpolicy.windowmanagerfuncs; 
  import android.view.gravity; 
  import android.app.wallpapermanager; 
  import android.graphics.bitmap.config; 
  import android.graphics.drawable.bitmapdrawable; 

  //added by wangchengju for shutdown 

  public class iphoneshutdowndialog { 

    private static string tag = "iphoneshutdowndialog"; 
    private int mstyleid; 
    private boolean misrestore = false; 
    private view view ; 
    private statusbarmanager mstatusbar; 
    private context mcontext; 
    private final windowmanagerfuncs mwindowmanagerfuncs;//added by wangchengju for shutdown 
    private static final int message_dismiss = 0; 
    static final string system_dialog_reason_key = "reason"; 
    static final string system_dialog_reason_global_actions = "globalactions"; 
    int mdownx,mdowny; 
    boolean ispowerdown=false; 
    private wallpapermanager wpm;  
    private framelayout mgradientview; 
    private windowmanager mwindowmanager; 
    private windowmanager.layoutparams mframelayoutparams; 
    private framelayout mframelayout; 



    public iphoneshutdowndialog(context context, windowmanagerfuncs windowmanagerfuncs) 

     { 
      log.d(tag,"denghaigui construction"); 
      mcontext = context; 
      mwindowmanagerfuncs = windowmanagerfuncs;//added by wangchengju for shutdown 
      mstatusbar = (statusbarmanager)mcontext.getsystemservice(context.status_bar_service); 
      intentfilter filter = new intentfilter(); 
      filter.addaction(intent.action_close_system_dialogs); 
      filter.addaction(intent.action_screen_off); 
      filter.addaction(intent.action_wallpaper_changed); 
      context.registerreceiver(mbroadcastreceiver, filter); 
        wpm = (wallpapermanager) mcontext.getsystemservice(mcontext.wallpaper_service); 

    } 
     public bitmap drawabletobitmap(drawable drawable) { 
      bitmap bitmap = bitmap.createbitmap( 
      drawable.getintrinsicwidth(), 
      drawable.getintrinsicheight(), 
      drawable.getopacity() != pixelformat.opaque ? bitmap.config.argb_8888 
      : bitmap.config.rgb_565); 
      canvas canvas = new canvas(bitmap); 
      // canvas.setbitmap(bitmap); 
      drawable.setbounds(0, 0, drawable.getintrinsicwidth(), 
          drawable.getintrinsicheight()); 
      drawable.draw(canvas); 
      return bitmap; 
    } 
    /* public drawable getbitmapdrawable(view views){ 
      windowmanager windowmanager = (windowmanager)mcontext.getsystemservice(context.window_service); 
         display display = windowmanager.getdefaultdisplay(); 
         int w = display.getwidth(); 
         int h = display.getheight(); 

         bitmap bmp = bitmap.createbitmap( w, h, config.argb_8888 );   
         view decorview = views.getwindow().getdecorview();  
         decorview.setdrawingcacheenabled(true);  
         bmp = decorview.getdrawingcache();  
      drawable drawable =new bitmapdrawable(bmp); 
      return drawable; 
    }*/ 

        private void initwindowsview() { 
      if (mwindowmanager == null) 
        mwindowmanager = (windowmanager) mcontext.getsystemservice(context.window_service); 

      mframelayoutparams = new windowmanager.layoutparams(windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen); 
      mframelayoutparams.type = windowmanager.layoutparams.type_system_error;// 
      mframelayoutparams.format = pixelformat.rgba_8888; 
      resetwindowparamsflags(); 
      mframelayoutparams.gravity = gravity.top | gravity.start; 
      mframelayout = new framelayout(mcontext); 

         mframelayout.setsystemuivisibility( 
        view.system_ui_flag_layout_stable 
           | view.system_ui_flag_layout_hide_navigation 
           | view.system_ui_flag_layout_fullscreen 
           | view.system_ui_flag_immersive_sticky 
           | view.system_ui_flag_hide_navigation); 
      mwindowmanager.addview(mframelayout, mframelayoutparams); 
    } 

      private void resetwindowparamsflags() { 
     mframelayoutparams.flags = windowmanager.layoutparams.flag_keep_screen_on 
          | windowmanager.layoutparams.flag_dismiss_keyguard 
          | windowmanager.layoutparams.flag_fullscreen 
          | windowmanager.layoutparams.flag_not_focusable 
          | windowmanager.layoutparams.flag_layout_no_limits 
          | windowmanager.layoutparams.flag_layout_inset_decor; 



    } 
    private void removeview(){ 

       mframelayout.removeallviews(); 
         mwindowmanager.removeviewimmediate(mframelayout); 
        view=null; 
        mframelayout = null; 
         mwindowmanager = null; 

       } 
    public void showdialog(boolean keyguardshowing) 

    { 
    log.d("phoneshutdowndialog","denghaigui showdialog"); 
      initwindowsview(); 
      //create dialog 
      if(view == null){ 
         layoutinflater inflater = layoutinflater.from(mcontext) ; 
         view = inflater.inflate(r.layout.iphone_shutdown_dialog, null, false) ;    
       mframelayout.addview(view); 
       bitmap image = drawabletobitmap(wpm.getdrawable()); 
        image = blur.fastblur(mcontext, image,25); 
        bitmapdrawable bt = new bitmapdrawable(image); 
        blur.setdrawable(bt); 

      }else{ 
        log.d("phoneshutdowndialog","denghaigui view is not null"); 
     }     

       drawable bt = blur.getdrawable(); 
      if(bt!=null){ 
        view.setbackground(bt); 
      } 

     mgradientview = (framelayout)view.findviewbyid(r.id.gradientview_layout); 
     framelayout slide_shutdown = (framelayout)view.findviewbyid(r.id.slide_layout); 
     slideview mslideview=new slideview(mcontext); 
     android.widget.framelayout.layoutparams layoutparams = new android.widget.framelayout.layoutparams(524, 150,gravity.center); 
     slide_shutdown.addview(mslideview,0,layoutparams); 
      final linearlayout cancelshutdown = (linearlayout)view.findviewbyid(r.id.cancelshutdown) ; 

      button cancelshutdownbtn = (button)view.findviewbyid(r.id.cancelshutdownbtn) ; 
      cancelshutdownbtn.setpressed(false); 
      cancelshutdownbtn.setonclicklistener(new view.onclicklistener() { 
        public void onclick(view v) { 
          removeview(); 

         // playcloseanimation(shutdowntab, cancelshutdown) ; 
        } 
      }) ; 

      //play open animation 
  //    playopenanimation(shutdowntab, cancelshutdown) ; 

      mstatusbar.disable(statusbarmanager.disable_expand); 

    } 


    public void dismissdialog() 
    { 
      removeview(); 
      mstatusbar.disable(statusbarmanager.disable_none); 

    } 

    private broadcastreceiver mbroadcastreceiver = new broadcastreceiver() { 
      public void onreceive(context context, intent intent) { 
        string action = intent.getaction(); 
        if (intent.action_close_system_dialogs.equals(action) 
            || intent.action_screen_off.equals(action)) { 
          string reason = intent.getstringextra(system_dialog_reason_key); 
          if (!system_dialog_reason_global_actions.equals(reason)) { 
            mhandler.sendemptymessage(message_dismiss); 
          } 
        }  
        if(action.equals(intent.action_wallpaper_changed)){ 
          bitmap image = drawabletobitmap(wpm.getdrawable()); 
          image = blur.fastblur(context, image,25); 
          bitmapdrawable bt = new bitmapdrawable(image); 
          blur.setdrawable(bt); 
        } 
      } 
    }; 


    private handler mhandler = new handler() { 
      public void handlemessage(message msg) { 
        if (msg.what == message_dismiss) { 
          if (view != null) { 
            dismissdialog(); 
          } 
        } 
      } 
    }; 



    public class slideview extends view 
    { 
    public float mtouchx;  
    private drawable mslidedrawable; 
    public float mdownx,mdx;  


    private boolean ispower=false; 
    private context mcontext; 
    public slideview(context context, attributeset attrs) { 
      this(context); 
    } 


    public slideview(context context) { 
      super(context); 
      mcontext=context; 
      mslidedrawable = context.getresources().getdrawable(r.drawable.slide_touch_icon); 
    } 


    @override 
    protected void ondraw(canvas canvas) { 
      super.ondraw(canvas); 
      if(mdx>374)mdx=374; 
      if(mdx<0)mdx=0; 
      paint mpaint=new paint(paint.anti_alias_flag); 
      mpaint.setcolor(color.white); 
      mpaint.setalpha(200); 

      canvas.save(); 
      path path=new path(); 
      rectf rect=new rectf(mdx, 0, 434, 120); 
     canvas.drawroundrect(rect, 72, 72, mpaint); 
      canvas.restore(); 
      canvas.drawbitmap(((bitmapdrawable) mslidedrawable).getbitmap(), mdx+5, 5,null);   
   } 

    @override 
    public boolean ontouchevent(motionevent ev) {    
       int action = ev.getaction(); 
      switch (action) { 
        case motionevent.action_down:  
          mdownx=ev.getx();  
          ispower=true; 
          //mgradientview.setvisibility(8); 
         break;       

        case motionevent.action_move:   
          mgradientview.setvisibility(8); 
          mtouchx = ev.getx(); 
          if(mtouchx <= mdownx) 
          { 
            mdx=0; 
            mgradientview.setvisibility(0); 
          } 
          else 
            mdx=(mtouchx-mdownx); 
          invalidate();  
          if(mdx>374) 
            ispower=false; 
          else 
            ispower=true; 

        break; 

        case motionevent.action_up: 
        case motionevent.action_cancel:   
          if(ispower==true){ 
            mgradientview.setvisibility(0); 
            mdx=0; 
            invalidate();  
          } 
          else 
            mwindowmanagerfuncs.shutdown(false); 

          break; 
     } 
      return true; 
    } 

  } 
  } 

gradientpowerview.java 用来做滑块和写入滑块中的文字显示

  package com.android.server.policy; 



  import android.animation.valueanimator; 
  import android.animation.valueanimator.animatorupdatelistener; 
  import android.content.context; 
  import android.content.res.typedarray; 
  import android.graphics.canvas; 
  import android.graphics.color; 
  import android.graphics.lineargradient; 
  import android.graphics.paint; 
  import android.graphics.shader; 
  import android.graphics.drawable.bitmapdrawable; 
  import android.graphics.drawable.drawable; 
  import android.util.attributeset; 
  import android.util.log; 
  import android.view.view; 
  import android.view.animation.animation; 
  import com.android.internal.r; 
  import android.graphics.typeface; 

  public class gradientpowerview extends view { 


    private static final string tag = "gradientpowerview"; 
    private static final boolean debug = false; 
    private float  mindex = 0; 
    private shader mshader; 
    private int   mtextsize;  
    private static final int mupdatestep = 15; 
    private static final int mmaxwidth = 40 * mupdatestep; // 26*25  
    private static final int mminwidth = 6 * mupdatestep; // 5*25 
    int       mdefaultcolor; 
    int       mslidecolor; 
    private valueanimator animator; 
    private int mwidth,mheight; 
    private string mstringtoshow; 
    private paint mtextpaint; 
    private float mtextheight; 
    private drawable mslideicon; 
    private int mslideiconheight; 
    private static final int mslideiconoffsettop = 20; 
    private static final string android_clock_font_file = "/system/fonts/asans.ttf"; 

    private animatorupdatelistener manimatorupdatelistener = new animatorupdatelistener() { 

      @override 
      public void onanimationupdate(valueanimator animation) { 
        mindex =float.parsefloat(animation.getanimatedvalue().tostring()); 
        // radialgradient sweepgradient 
        mshader = new lineargradient(mindex - 20 * mupdatestep, 100, 
            mindex, 100, new int[] { mdefaultcolor, mdefaultcolor, mdefaultcolor,mslidecolor, 
            mslidecolor, mdefaultcolor, mdefaultcolor, mdefaultcolor }, null, 
            shader.tilemode.clamp); 

        postinvalidate(); 

      } 
    }; 

    public gradientpowerview(context context) { 
      super(context); 
    } 


    public gradientpowerview(context context, attributeset attrs) { 
      super(context, attrs); 
      typedarray a = context.obtainstyledattributes(attrs, r.styleable.gradientview); 
      mstringtoshow = a.getstring(r.styleable.gradientview_text) ; 
      mtextsize = (int)a.getdimension(r.styleable.gradientview_textsize, 40); 
      mdefaultcolor = a.getcolor(r.styleable.gradientview_textcolor, color.gray); 
      mslidecolor = a.getcolor(r.styleable.gradientview_slidecolor, color.white); 
      mslideicon = context.getresources().getdrawable(r.drawable.slide_icon); 
      mslideiconheight = mslideicon.getminimumheight(); 
      a.recycle(); 

      animator = valueanimator.offloat(mminwidth,mmaxwidth); 
      animator.setduration(1800); 
      animator.addupdatelistener(manimatorupdatelistener); 
      animator.setrepeatcount(animation.infinite);//repeat animation 
      animator.start(); 


      mtextpaint = new paint(); 
      mtextpaint.setantialias(true); 
      mtextpaint.setcolor(mslidecolor); 
      mtextpaint.settextsize(mtextsize);  
      mtextpaint.settextalign(paint.align.center); 
  //   mtextpaint.settypeface(typeface.createfromfile(android_clock_font_file));  

      mtextheight = mtextpaint.ascent(); 

      setfocusable(true); 
    } 

    @override 
    protected void ondraw(canvas canvas) { 
      if(debug) 
      log.w(tag, "b ondraw()"); 
      mtextpaint.setshader(mshader); 
          log.i("denghaigui","mwidth is: "+mwidth +"height is: "+mheight+ "mtextheight: "+mtextheight); 
      canvas.drawtext(mstringtoshow, (mwidth+30)/2, mheight / 2 - mtextheight 
          / 2 - mslideiconoffsettop, mtextpaint); // slide_unlock 
    }  



    public void stopanimatorandchangecolor() { 
      //if(debug) 
      log.w(tag, "stopgradient"); 
      animator.cancel(); 
      //reset 
      mshader = new lineargradient(0, 100, mindex, 100, 
          new int[] {mslidecolor, mslidecolor}, 
          null, shader.tilemode.clamp); 
      invalidate(); 
    } 

    public void startanimator() { 
      if(debug) 
      log.w(tag, "startgradient"); 
      animator.start(); 
    } 

    @override 
    protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { 
      super.onmeasure(widthmeasurespec, heightmeasurespec); 
      mwidth = measurespec.getsize(widthmeasurespec); 
      mheight = measurespec.getsize(heightmeasurespec); 
    } 

    public void resetcontrol(){ 
      animator.start(); 
      this.setx(0); 
      invalidate(); 
    } 
  } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: