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();
    }
  }

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

(0)

相关推荐

  • android源码探索之定制android关机界面的方法

    本文实例讲述了android源码探索之定制android关机界面的方法.分享给大家供大家参考.具体如下: 在Android系统中,长按Power键默认会弹出对话框让你选择"飞行模式","静音","关机"等功能.如下图所示: 但这些功能都对Android-x86和其他终端产品就没什么必要了.本文就简单介绍下如何定制关机界面. 我的目标是长按Power键,将会关机,弹出"设备将要关机"选择对话框.如果可以选择"是&quo

  • 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.Remote

  • Android仿微信主界面的实现方法

    本文实例为大家分享了Android模仿微信主界面展示的具体代码,供大家参考,具体内容如下 先看一下效果图 实现的原理: ViewPager+FragmentPagerAdapter 主界面可分为三部分: top标题栏就是一个TextView 中间的ViewPager作为显示的容器,填充Fragment bottom是一个RadioGroup 这里为了布局的优化,将top和bottom抽取出来 ,然后用include将其导入主布局,如下 <LinearLayout xmlns:android=&quo

  • Android 仿苹果底部弹出Dialog

    style文件 <style name="ActionSheetDialogStyle" parent="@android:style/Theme.Dialog"> <!-- 背景透明 --> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:wind

  • Android仿通话来电界面效果

    Android仿通话来电界面,供大家参考,具体内容如下 简介:开发中需要模拟来电时的通话界面,仿照来电界面实现来电时播放铃声,界面通过动画模拟来电动效. 效果图: 自定义图片背景,图片由小变大的动态效果. shap_circle.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/androi

  • Android 仿苹果IOS6开关按钮

    先给大家展示下效果图: 不知道大家对效果图感觉怎么样,个人觉还不错,感兴趣的朋友可以参考下实现代码哦. public class ToggleButton extends View { private SpringSystem springSystem; private Spring spring ; /** */ private float radius; /** 开启颜色*/ private int onColor = Color.parseColor("#4ebb7f"); /*

  • Android仿微信之界面导航篇(1)

    微信是现在比较流行的应用了,在各大安卓市场几乎都是名列前茅了. 说实话不得不羡慕腾讯庞大的用户群体,只要腾讯敢做,就会有很多人去用. 废话不多说,下面就开始说一下如何实现微信的第一次启动程序的用户导航,ViewPager相信大家都不陌生了,是google放出的一个安卓低版本的兼容包android-support-v4.jar,里面有很多类我们可以去使用.那这个导航就是使用这个类来辅助完成的,在每一个View里放置一个图片,当我们使用ViewPager滑动界面的时候,就会看到一张张图片,从而实现这

  • Android仿新浪微博启动界面或登陆界面(1)

    本文为大家分享了Android模仿新浪微博启动界面&登陆界面的具体实现代码,供大家参考,具体内容如下 启动界面 主要有两个功能: 1.加载启动动画 2.判断网络,有者直接进入登陆界面,否则去设置网络 代码较简单,主要采用AlphaAnimation()方法和动画监听器,使一张图片产生渐变动画.在动画启动的时候判断网络,动画结束时完成判断并进入登陆界面. /** * Created by D&LL on 2016/5/25. * 初始页面加载界面 */ public class Splash

  • Android 仿京东侧滑筛选实例代码

    简单介绍 这个demo写的是仿京东的侧滑筛选页面,点击进入筛选后进入二级筛选,两次侧滑的筛选,还包括ListView+CheckBox滑动冲突,ListView+ GridView显示一行问题解决,接口回调传递数据等 效果图 简单得代码介绍 1.首页侧滑用的是安卓官方V4包中的DrawerLayout <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLa

  • Android仿微信主界面设计

    先来一张效果图 一.ActionBar的设计 首先是main.xml,先定义这些菜单,界面稍后在调整 <menu xmlns:android="http://schemas.android.com/apk/res/android" tools:context=".MainActivity"> <item android:id="@+id/action_search" android:actionViewClass="a

  • Android 应用的欢迎界面实现代码

    本文详细描述了如何实现如下图中的微信启动界面. 该类启动界面的特点是在整个Application的生命周期里, 它只会出现在第一次进入应用时, 即便按回退键到桌面之后. 使用该类启动界面的应用还有: QQ, QQ音乐, 网易云音乐和微博等等. 知识要点: AndroidManifest.xml 中 activity 的 android:noHistory 属性, 即 Intent.FLAG_ACTIVITY_NO_HISTORY 隐式Intent 回退栈(BackStack) 详细内容见官方文档

随机推荐