Android手機懸浮視窗小案例_Android

來源:互聯網
上載者:User

本文執行個體為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內容如下

–首頁面——–

//布局中就一個Buttonpublic class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View view) {   //目的就是啟動Service來開啟懸浮表單    startService(new Intent(MainActivity.this, FloatService.class));    finish();   }  }); }}

—Service開啟懸浮表單——-

/** * description:通過Service來開啟FloatView * 作者:ldm * 時間:20162016/8/17 14:03 * 郵箱:1786911211@qq.com */public class FloatService extends Service { @Override public void onCreate() {  super.onCreate();  CustomViewManager.getInstance(this).showFloatViewOnWindow(); } @Override public IBinder onBind(Intent intent) {  return null; }}

—懸浮表單管理工具類——-

/** * description: * 作者:ldm * 時間:20162016/8/17 11:57 * 郵箱:1786911211@qq.com */public class CustomViewManager { //上下文 private Context mContext; //本類執行個體 private static CustomViewManager instance; //自訂的FloatView private FloatView mFloatView; //視窗管理類 private WindowManager mWindowManager; private CustomViewManager(Context context) {  this.mContext = context;  mFloatView = new FloatView(mContext);  mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); } /**  * @param  * @description 通過單例模式擷取執行個體對象  * @author ldm  * @time 2016/8/17 11:59  */ public static CustomViewManager getInstance(Context mContext) {  if (null == instance) {   synchronized (CustomViewManager.class) {    if (null == instance) {     instance = new CustomViewManager(mContext);    }   }  }  return instance; } /**  * @param  * @description 在手機螢幕上顯示自訂的FloatView  * @author ldm  * @time 2016/8/17 13:47  */ public void showFloatViewOnWindow() {  WindowManager.LayoutParams parmas = new WindowManager.LayoutParams();  parmas.width = mFloatView.getFloatWidth();  parmas.height = mFloatView.getFloatHeight();  //視窗圖案置放位置  parmas.gravity = Gravity.LEFT | Gravity.CENTER;  // 如果忽略gravity屬性,那麼它表示視窗的絕對X位置。  parmas.x = 0;  //如果忽略gravity屬性,那麼它表示視窗的絕對Y位置。  parmas.y = 0;  ////電話視窗。它用於電話互動(特別是呼入)。它置於所有應用程式之上,狀態列之下。  parmas.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;  //FLAG_NOT_FOCUSABLE讓window不能獲得焦點,這樣使用者快就不能向該window發送按鍵事件及按鈕事件  //FLAG_NOT_TOUCH_MODAL即使在該window在可獲得焦點情況下,仍然把該window之外的任何event發送到該window之後的其他window.  parmas.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;  // 期望的位元影像格式。預設為不透明。參考android.graphics.PixelFormat。  parmas.format = PixelFormat.RGBA_8888;  mWindowManager.addView(mFloatView, parmas); }}

—自訂的FloatView——-

/** * description: * 作者:ldm * 時間:20162016/8/17 11:17 * 郵箱:1786911211@qq.com */public class FloatView extends View { //懸浮球寬度 private int floatWidth = 150; //懸浮球高度 private int floatHeight = 150; //懸浮球畫筆 private Paint mPaint; //繪製文字畫筆 private Paint mTextPaint; private String text = "50%"; public FloatView(Context context) {  super(context);  initPaint(); } public int getFloatWidth() {  return floatWidth; } public int getFloatHeight() {  return floatHeight; } public FloatView(Context context, AttributeSet attrs) {  super(context, attrs);  initPaint(); } public FloatView(Context context, AttributeSet attrs, int defStyleAttr) {  super(context, attrs, defStyleAttr);  initPaint(); } /**  * @param  * @description 初始化畫筆  * @author ldm  * @time 2016/8/17 11:37  */ private void initPaint() {  //設定懸浮球畫筆  mPaint = new Paint();  mPaint.setColor(Color.GREEN);  mPaint.setAntiAlias(true);  mPaint.setDither(true);  //設定文字畫筆  mTextPaint = new Paint();  mTextPaint.setTextSize(25);  mPaint.setAntiAlias(true);  mTextPaint.setColor(Color.WHITE);  mTextPaint.setFakeBoldText(true); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  //設定大小  setMeasuredDimension(floatWidth, floatHeight); } /**  * @param  * @description 繪製圖案  * @author ldm  * @time 2016/8/17 11:44  */ @Override protected void onDraw(Canvas canvas) {  super.onDraw(canvas);  //繪製懸浮球  canvas.drawCircle(floatWidth / 2, floatHeight / 2, floatWidth / 2, mPaint);  //繪製文字  Paint.FontMetrics metrics = mTextPaint.getFontMetrics();  //文字大小計算可以參考:http://mikewang.blog.51cto.com/3826268/871765/  float textWidth = mTextPaint.measureText(text);  float x = floatWidth / 2 - textWidth / 2;  float dy = -(metrics.descent + metrics.ascent) / 2;  float y = floatHeight / 2 + dy;  canvas.drawText(text, x, y, mTextPaint); }}

最後,在AndroidManifest.xml中不要忘記添加許可權:

 <!--添加許可權--> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

當然也要記得添加Service組件

 <service android:name=".service.FloatService" />

有的手機運行後,發現沒有出現我們想要的懸浮表單,可以進入手機設定中心,點擊應用設定,在指定的應用使用權限設定中開啟懸浮表單相應的設定開關即可。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.