Android Bitmap.getPixel() 非常不准确
项目概览:有两张图片。单击顶部图像(在我的应用程序中,我使用猫图片),它将把底部图像背景设置为您单击位置的像素颜色。
问题:背景颜色与单击位置的像素颜色不匹配,有时是随机颜色,有时是有一些色差。这很奇怪。
这是主要的 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>
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, 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;
}
}
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论