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

 更新时间:2017年09月20日 15:06:15   作者:solarsaber  
这篇文章主要为大家详细介绍了Android仿苹果关机界面的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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(); 
    } 
  } 

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

相关文章

最新评论