在Android

时间:2016-02-24 04:50:42

标签: android android-service

我需要创建一个服务,不断检查Android屏幕的坐标是否有像素的颜色。

例如

Bitmap bitmap = ((BitmapDrawable)getBackground()).getBitmap();
int pixel = bitmap.getPixel(x,y);

if(pixel == Color.MAGENTA){
   //Stop Service
}

然而,不仅仅是我的应用程序,我需要它在后台服务中,即使我切换应用程序也会不断检查颜色。

假设设备已植根,是否有任何超级用户命令可以满足我的需求?

4 个答案:

答案 0 :(得分:3)

public class TouchView extends ImageView {

 Bitmap bitmap;
 double bmWidth, bmHeight; 

 public TouchView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  init();
 }

 public TouchView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
  init();
 }

 public TouchView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
  init();
 }

 private void init(){

  bitmap = ((BitmapDrawable)getBackground()).getBitmap();
  bmWidth = (double)bitmap.getWidth();
  bmHeight = (double)bitmap.getHeight();
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  // TODO Auto-generated method stub
  setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
       MeasureSpec.getSize(heightMeasureSpec));
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  // TODO Auto-generated method stub


  switch(event.getAction()){
  case MotionEvent.ACTION_DOWN:
  case MotionEvent.ACTION_MOVE:
   float x = event.getX();
   float y = event.getY();

   int color = getColor(x, y);
      ((AndroidDetechTouchActivity)getContext()).updateMsg("Touched@" + x + " : " + y, color);

   break;
  case MotionEvent.ACTION_UP:
   ((AndroidDetechTouchActivity)getContext()).updateMsg("", 0);
   break;
  }

  return true;
 }

 private int getColor(float x, float y){

  if ( x < 0 || y < 0 || x > (float)getWidth() || y > (float)getHeight()){
   return 0; //Invalid, return 0
  }else{
   //Convert touched x, y on View to on Bitmap
   int xBm = (int)(x * (bmWidth / (double)getWidth()));
      int yBm = (int)(y * (bmHeight / (double)getHeight()));

   return bitmap.getPixel(xBm, yBm);
  }

 }

}

修改主要活动AndroidDetechTouchActivity.java的updateMsg()方法,以包含颜色并更新TextView的TextColor。

public class AndroidDetechTouchActivity extends Activity {

 TextView msg;
 TouchView touchView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        msg = (TextView)findViewById(R.id.msg);
        touchView = (TouchView)findViewById(R.id.touchview);

    }

    public void updateMsg(String tMsg, int color){
     msg.setTextColor(color);
     msg.setText(tMsg);
    }

}

答案 1 :(得分:1)

对于那些需要做我喜欢的事情的人,

我使用系统的命令/ system / bin / screencap -p捕获屏幕并检查屏幕截图上的像素。

此方法需要root权限

private void getPixelColor(int xcoord, int ycoord)
{
    try {
        Process sh = Runtime.getRuntime().exec("su", null,null);

        OutputStream os = sh.getOutputStream();
        os.write(("/system/bin/screencap -p " + "/sdcard/colorPickerTemp.png").getBytes("ASCII"));
        os.flush();

        os.close();
        sh.waitFor();

        Bitmap screen = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + File.separator + "colorPickerTemp.png");
        int pixel = screen.getPixel(xcoord ,ycoord + statusHeight);
        Log.d("pixel color", "Pixel Color: + " + Integer.toHexString(pixel) + " at x:" + xcoord + " y:" + ycoord);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

答案 2 :(得分:0)

您可以绘制到位图并从位图中获取颜色,代码如下:

Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), 
view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view.draw(c);
int color = bitmap.getPixel(x, y);

答案 3 :(得分:-1)

如上所述。效率低下......可能是您实施应用程序的更好方法。

 public boolean checkColor() {
       View rootView = findViewById(android.R.id.content).getRootView();
       rootView.setDrawingCacheEnabled(true);
       Bitmap bitmap = rootView.getDrawingCache();
       rootView.setDrawingCacheEnabled(false);
       int pixel = bitmap.getPixel(x,y);
       if(pixel == Color.MAGENTA){
           return true;
       }
       return false;
    }