getPixel总是返回0.为什么?

时间:2014-01-30 20:02:42

标签: java android

我是Android新手,我遇到了问题。我正在使用地图,并且在地图上有不同的颜色。我试图获取颜色以确定我在地图上单击的位置,但getPixel()每次都返回0。你能帮我吗?

public class ImageZoomActivity extends Activity {

    private ImageViewTouch mImageView;
    public Bitmap map;

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );

        requestWindowFeature( Window.FEATURE_NO_TITLE );
        setContentView( R.layout.imagezoom );
        getWindow().addFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN );

        bitmap_init();
        selectImage();
        click_map();
    }

    @Override
    public void onContentChanged()
    {
        super.onContentChanged();
        mImageView = (ImageViewTouch)findViewById( R.id.imageView1 );
        click_map();
    }

    public void bitmap_init() {
        map = BitmapFactory.decodeResource(getResources(), R.drawable.map);
    }

    public void selectImage() {
        mImageView.setImageBitmapReset(map, true);
        return;
    }

    public void click_map()  {

        //final ImageView image_map = new ImageView(this);
        mImageView.setImageBitmap(map);

        mImageView.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {

                int x_coordinate  = (int)event.getX();
                int y_coordinate = (int)event.getY();

                Bitmap bitmap = ((BitmapDrawable)mImageView.getDrawable()).getBitmap();
                int pixel = bitmap.getPixel(x_coordinate,y_coordinate);

                if (event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    Log.d("Log", "Color: " + bitmap.getPixel(x_coordinate, y_coordinate) + " x: " + event.getX() + " y: " + event.getY());
                }

                /*if (event.getAction() == MotionEvent.ACTION_UP)
                {

                }*/

                return true;
            }
        });
    }
}

0 个答案:

没有答案