2017年3月15日 星期三

android app背景的touch事件

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接收了



video















































read more...

2017年3月12日 星期日

用SIKULI去android手機內抓圖做點擊

一般網路上看到的,都是用電腦接手機投射到電腦螢幕,抓電腦手機螢幕的圖,在外國網站有人用sikuli直接去手機內抓螢幕的圖,控制手機,而不是在電腦螢幕去做控制,終於試出來了,你要先安裝adb,若你有安裝android studio,其實就有adb了,在安裝目錄下.../Android/sdk/platform-tools/adb device -l

1.
terminal下執行下面指令連接手機

../Android/sdk/platform-tools/adb device -l

2.
sikuli API android目錄有一個ADBScreen class,import它,叫用start函式,會傳回android螢幕物件screen,我們就可在android手機內做動作,譬如螢幕抓圖,螢幕點擊.....等,下面是一個簡單抓圖和螢幕點擊

import org.sikuli.android.ADBScreen as Ascreen #把ADBScreen直接設成Ascreen
x=Ascreen.start()      #傳回androi screen


3.
SIKULI-IDE的python程式

#####################convert######################
#請用android studio看jar的程式
assert load('/Users/yplin/sikulix.jar')
import org.sikuli.android.ADBScreen as Ascreen #把ADBScreen直接設成Ascreen
import subprocess
#有安裝android studio找到adb用adb devices -l連接手機
#subprocess.call("/Users/yplin/Library/Android/sdk/platform-tools/adb devices -ladb devices -l".split())
#import org.sikuli.android.ADBClient as Aclient
#print(Aclient.getDevice())

x=Ascreen.start()      #傳回androi screen
use(x)                 #use
x.needsUnLock = False
wakeUp(2)
#store_path_base = os.getenv("HOME")
#new_path=os.path.join(store_path_base,"SIKULI/PNG/t.png")
new_path="/Users/yplin"
reg=x.newRegion(0,0,1000,1000)  #手機區域region
#reg.highlight(1)
img=x.capture(reg)      #抓手機螢幕圖
click(reg)              #點擊手機螢幕
img.save(new_path)      #存檔

popup("capture picture ok")

抓到圖的結果(左邊下面的小圖):

read more...