繁体   English   中英

PhotoView库获取像素的颜色

[英]PhotoView Library get color of pixels

在我的应用中,我使用的是chrisbanes的PhotoView库,它用于使图像可缩放。 然后,我需要获取图像上触摸的像素的颜色。 只要不移动图像,它就可以工作,但是例如在放大时,返回的颜色与图像的颜色不匹配。 设备需要什么才能识别出图像已移动并返回正确的颜色?

编码:

@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);

        }
 });

看起来在放大时出现错误的颜色的原因是getDrawingCache()返回原始可绘制对象,而不是放大的对象。

您需要使视图无效,以便getDrawingCache()返回新的位图。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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