简体   繁体   中英

PhotoView Library get color of pixels

In my app I'm using the PhotoView Library by chrisbanes, which I use to make the image zoomable. Then I need to get the color of the pixel that is touched on the image. It works as long as the image is not moved, but when it is eg zoomed in, the returned colors doesn't match to the color of the image. What is necessary so the device recognizes that the image was moved and returns the right color?

The code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final PhotoView photoView = (PhotoView) findViewById(R.id.photo_view);
    photoView.setImageResource(R.drawable.karte);

    photoView.setScaleType(ImageView.ScaleType.FIT_XY);
    photoView.setDrawingCacheEnabled(true);
    photoView.buildDrawingCache(true);

}


 photoView.setOnViewTapListener(new OnViewTapListener() {
       @Override
        public void onViewTap(View view, float x, float y) {

            bitmap = photoView.getDrawingCache();
            int pixel = bitmap.getPixel((int) x, (int) y);
            String text = "x = " + x + ", y = " + y;
            Log.d("Position", text);
            int redValue = Color.red(pixel);
            int greenValue = Color.green(pixel);
            int blueValue = Color.blue(pixel);

            String hex = String.format("%02x%02x%02x", redValue, greenValue, blueValue);

        }
 });

Looks like the reason you are getting wrong colors when zoomed-in, is that getDrawingCache() returns the original drawable, not the zoomed-in one.

You need to invalidate the view so that getDrawingCache() to return a fresh bitmap.

@Override
public void onViewTap(View view, float x, float y) {
    photoView.invalidate()
    bitmap = photoView.getDrawingCache();
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM