Android仿蘋果關(guān)機(jī)界面實(shí)現(xiàn)代碼
本文實(shí)例為大家分享了Android仿蘋果關(guān)機(jī)界面的具體代碼,供大家參考,具體內(nèi)容如下
主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();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android實(shí)現(xiàn)短按電源鍵關(guān)機(jī)的實(shí)現(xiàn)代碼
- Android平臺(tái)預(yù)置GMS包后關(guān)機(jī)鬧鐘失效問題及解決方法
- Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問題
- Android 6.0開發(fā)實(shí)現(xiàn)關(guān)機(jī)菜單添加重啟按鈕的方法
- Android開發(fā)實(shí)現(xiàn)長(zhǎng)按返回鍵彈出關(guān)機(jī)框功能
- Android 修改系統(tǒng)關(guān)機(jī)動(dòng)畫的實(shí)現(xiàn)
- Android下的CMD命令之關(guān)機(jī)重啟及重啟recovery
- Android實(shí)現(xiàn)關(guān)機(jī)與重啟的幾種方式(推薦)
- Android系統(tǒng)關(guān)機(jī)的全流程解析
- Android 實(shí)現(xiàn)關(guān)機(jī)的多種方式
相關(guān)文章
Android實(shí)現(xiàn)基于ViewPager的無限循環(huán)自動(dòng)播放帶指示器的輪播圖CarouselFigureView控件
這篇文章主要介紹了Android實(shí)現(xiàn)基于ViewPager的無限循環(huán)自動(dòng)播放帶指示器的輪播圖CarouselFigureView控件,需要的朋友可以參考下2017-02-02
Android?AccessibilityService?事件分發(fā)原理分析總結(jié)
這篇文章主要介紹了Android?AccessibilityService?事件分發(fā)原理分析總結(jié),AccessibilityService有很多用來接收外部調(diào)用事件變化的方法,這些方法封裝在內(nèi)部接口Callbacks中,文章圍繞AccessibilityService相關(guān)資料展開詳情,需要的朋友可以參考一下2022-06-06
Android 個(gè)人理財(cái)工具四:添加賬單頁面 下
本文主要介紹Android 個(gè)人理財(cái)工具添加賬單頁面,這里是添加賬單的詳情頁面及如何使用Android Spinner控件的簡(jiǎn)單示例,有需要的小伙伴可以參考下2016-08-08
Android編程實(shí)現(xiàn)獲取當(dāng)前連接wifi名字的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲取當(dāng)前連接wifi名字的方法,涉及Android針對(duì)WiFi屬性操作的相關(guān)技巧,需要的朋友可以參考下2015-11-11
Android調(diào)用默認(rèn)瀏覽器打開指定Url的方法實(shí)例
業(yè)務(wù)員有需求要將一個(gè)wap站在手機(jī)上以App的形式打開,還不要嵌套WebView,只能以瀏覽器打開.查了點(diǎn)資料,就有了下面這代碼2013-09-09
Android 自定義TextView實(shí)現(xiàn)文本內(nèi)容自動(dòng)調(diào)整字體大小
本文主要介紹了Android 自定義TextView實(shí)現(xiàn)文本內(nèi)容自動(dòng)調(diào)整字體大小以適應(yīng)TextView的大小的方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
Android中pendingIntent與Intent的深入分析
這篇文章主要介紹了Android中pendingIntent的深入分析的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android編程簡(jiǎn)易實(shí)現(xiàn)XML解析的方法詳解
這篇文章主要介紹了Android編程簡(jiǎn)易實(shí)現(xiàn)XML解析的方法,結(jié)合實(shí)例形式總結(jié)分析了Android操作xml文件的各種常見技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08
android中可以通過兩種方式調(diào)用接口發(fā)送短信
調(diào)用系統(tǒng)短信接口直接發(fā)送短信;調(diào)起系統(tǒng)發(fā)短信功能,本文將給出兩種方式的實(shí)現(xiàn)代碼,感興趣的朋友可以了解下,或許對(duì)你有所幫助2013-02-02
基于android中的各種顏色在drawable.xml中的值詳解
本篇文章是對(duì)在android中的各種顏色在drawable.xml中的值進(jìn)行了詳細(xì)的介紹。需要的朋友參考下2013-05-05

