android app背景的touch事件,在網路上有資料,整理一下
1.
AndroidManifest.xml要給權限,加入
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yplin.myapplication11">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity11">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".floatActivity"></activity>
</application>
</manifest>
2.
取得WindowManager(是一個更上層的obj,可google一下)
建立dunmmy view(空的view)
設置LayoutParams
根據LayoutParams將dummy view加mWindowManager
package com.example.yplin.myapplication11;
import android.graphics.PixelFormat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
//繼承AppCompatActivityAppCompatActivity實做OnTouchListener
public class MainActivity11 extends AppCompatActivity implements View.OnTouchListener {
private LinearLayout lc=null;
private CustomView1 canvas_view =null;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main11);
lc=(LinearLayout)findViewById(R.id.view1);
canvas_view = new CustomView1(this);
Context mContext=getApplicationContext();
// 取得WindowManagerWindowManager
WindowManager mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
//建立一個dummy的view
LinearLayout mDummyView = new LinearLayout(mContext);
//新設置LayoutParams叫params,注意androidManifest.xml需給權限
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
1, /* width */ 1, /* height */
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSPARENT );
//可更改dummy view設置params
//mDummyView.setLayoutParams(params);
//dummy view設置touch傾聽事件
mDummyView.setOnTouchListener(this);
//根據params將dummy view加mWindowManager
mWindowManager.addView(mDummyView, params);
}
//實作OnTouchListener的onTouch傾聽事件方法
@Override
public boolean onTouch(View v, MotionEvent event) {
//LinearLayout lc=(LinearLayout)findViewById(R.id.view1);
if(lc.indexOfChild(canvas_view)==-1) {
lc.addView(canvas_view);
}
else {
lc.removeAllViews();
}
return false;
}
class CustomView1 extends View{
Paint paint;
public CustomView1(Context context) {
super(context);
paint = new Paint(); //設置一新畫筆
paint.setColor(Color.RED); //黄色
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(3); //設置一筆刷大小3 }
//create circle
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(100, 100, 90, paint);
}
}
}
實際結果,背景下觸摸螢幕,touch事件被這app接收了
沒有留言:
張貼留言