开发者

Android Bitmap.getPixel() Is Annoyingly Inaccuracte

Project OverView : Theres two images. Click the top image (in my app i use a cat pic) and it will set the bottom image background to the color of the pixel on the location you clicked at.

The Problem: The background color doesn't match the color of the pixel at the location of your click at times its randomly colored, other times its off by a few shades. Its wierd.

This is the main java code :

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
public class ColorRead extends Activity 
implements OnTouchListener{
 Integer myColor;
 String TAG;
 Bitmap bMap,bMap2;
 ImageView image,image2;
 Button BTN;
 MotionEvent mEvent;
 Integer touchX,touchY;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle Icicle) {
        super.onCreate(Icicle);
        setContentView(R.layout.main);
        bMap = BitmapFactory.decodeResource(getResources(), R.drawable.catpic);
        image2 = (ImageView) findViewById(R.id.bitmap2);
        image = (ImageView) findViewById(R.id.bitmap);
        image.setImageBitmap(bMap);
        image.setOnTouchListener(this);
    }
    public void scanForColor(int xMouse, int yMouse)
    {
     myColor = bMap.getPixel(xMouse,yMouse);
     Log.i(TAG, " R:"+Color.red(myColor)+" G:"+Color.green(myColor)+" B:"+Color.blue(myColor));
     image2.setBackgroundColor(myColor);
    }

 @Override
 public boolean onTouch(View v, Motio开发者_如何转开发nEvent e) {
  Integer id = v.getId();
  switch(id)
  {
  case R.id.bitmap:
   if(e.getAction() == MotionEvent.ACTION_DOWN)
         {
                 touchX = (int)e.getX();
                 touchY = (int)e.getY();
         }
   scanForColor(touchX,touchY);
         Log.i(TAG,touchX.toString());
         break;
    default:
     break;
  }
        return false;
 }
}

Heres the Main.XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView android:id="@+id/bitmap"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/catpic"
    />
    <ImageView android:id="@+id/bitmap2"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:src="@drawable/catpic"
    />
    <Button android:id="@+id/button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
    android:text="@string/myButton"
    />

</LinearLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜