Android Bitmap.getPixel()令人讨厌的不准确

时间:2010-11-06 04:43:49

标签: android bitmap getpixel

Project OverView:有两张图片。单击顶部图像(在我的应用程序中,我使用猫图片),它将底部图像背景设置为您单击的位置上的像素颜色。

问题:背景颜色与点击位置处的像素颜色不一致,有时是随机颜色,有时会偏离几个阴影。它很奇怪。

这是主要的java代码:

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, MotionEvent 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;
 }
}

继承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 个答案:

没有答案