Android下正确获取屏幕触摸点下的图片像素值
来源:互联网 发布:怎么用网址搜淘宝店铺 编辑:程序博客网 时间:2024/05/21 11:19
有的时候我们可能需要获取屏幕上触碰点下的图片像素值,比如我们在ImageView控件中显示了一张图片,当我们点击图片的某位置时想要获取这个点击位置的像素,最先想到的是使用Bitmap.getPixel(x, y)函数,但是需要注意的是,这个函数的坐标是以原始图片为基准的。通常我们获取屏幕触碰点的坐标是以屏幕为基准的,这就需要对坐标进行转换,尤其是当图片有进行拉伸的时候,如果不进行坐标转换,获取的图片像素是不准确的。
由于本人十分的懒惰,实在不想自己写一篇文章来阐述如何转换坐标,不过我偶然发现了这篇文章正好满足我想说明的问题,就直接粘贴过来了。
Get touched pixel color of scaled ImageView
To get touched pixel color of a ImageView, you can implement OnTouchListener of the ImageView, get touched position by calling event.getX() and event.getY(), and get the pixel color by calling bitmap.getPixel(x, y)...IF THE BITMAP IN YOUR ImageView IS DISPLAY AT 1:1, layout_width and layout_height ="wrap_content".
If your bitmap is scaled, such as layout_width and layout_height ="match_parent". It will not work! It's a example demonstrate how to invert the touched position to get pixel from scaled ImageView.
package com.example.androidtouchedpixel;import android.os.Bundle;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Matrix;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageView;import android.widget.TextView;public class MainActivity extends Activity { TextView touchedXY, invertedXY, imgSize, colorRGB; ImageView imgSource1, imgSource2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); touchedXY = (TextView)findViewById(R.id.xy); invertedXY = (TextView)findViewById(R.id.invertedxy); imgSize = (TextView)findViewById(R.id.size); colorRGB = (TextView)findViewById(R.id.colorrgb); imgSource1 = (ImageView)findViewById(R.id.source1); imgSource2 = (ImageView)findViewById(R.id.source2); imgSource1.setOnTouchListener(imgSourceOnTouchListener); imgSource2.setOnTouchListener(imgSourceOnTouchListener); } OnTouchListener imgSourceOnTouchListener = new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { float eventX = event.getX(); float eventY = event.getY(); float[] eventXY = new float[] {eventX, eventY}; Matrix invertMatrix = new Matrix(); ((ImageView)view).getImageMatrix().invert(invertMatrix); invertMatrix.mapPoints(eventXY); int x = Integer.valueOf((int)eventXY[0]); int y = Integer.valueOf((int)eventXY[1]); touchedXY.setText( "touched position: " + String.valueOf(eventX) + " / " + String.valueOf(eventY)); invertedXY.setText( "touched position: " + String.valueOf(x) + " / " + String.valueOf(y)); Drawable imgDrawable = ((ImageView)view).getDrawable(); Bitmap bitmap = ((BitmapDrawable)imgDrawable).getBitmap(); imgSize.setText( "drawable size: " + String.valueOf(bitmap.getWidth()) + " / " + String.valueOf(bitmap.getHeight())); //Limit x, y range within bitmap if(x < 0) { x = 0; } else if(x > bitmap.getWidth()-1) { x = bitmap.getWidth()-1; } if(y < 0) { y = 0; } else if(y > bitmap.getHeight()-1) { y = bitmap.getHeight()-1; } int touchedRGB = bitmap.getPixel(x, y); colorRGB.setText("touched color: " + "#" + Integer.toHexString(touchedRGB)); colorRGB.setTextColor(touchedRGB); return true; } };}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" tools:context=".MainActivity" /> <TextView android:id="@+id/xy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="touched position: "/> <TextView android:id="@+id/invertedxy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="inverted touched position: "/> <TextView android:id="@+id/size" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="drawable size: "/> <TextView android:id="@+id/colorrgb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="touched color: "/> <ImageView android:id="@+id/source1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/> <ImageView android:id="@+id/source2" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/ic_launcher"/></LinearLayout>
- Android下正确获取屏幕触摸点下的图片像素值
- android 下怎样获取图片像素值
- android获取屏幕某一点的像素值
- 获取图片、屏幕像素并纠正bufferedimage导入失败的问题Ps:手机端android的图片和屏幕像素获取正确方式
- android 正确获取屏幕像素大小
- android 正确获取屏幕像素大小
- Android下屏幕锁屏弹窗的正确姿势
- wince下的图片像素点获取(LED显示器,或者wince上位机图片的加载)
- android获取屏幕像素
- 获取一个图片像素点RGB值的代码
- 获取一个图片像素点RGB值的代码
- java获取图片像素点的RGB值
- 获取图片像素点颜色值
- Android 获取屏幕像素的方法
- IOS获取图片中某个像素点的像素值,包括ARGB的各个值
- 获取当前屏幕的宽高和像素点
- android-学习-获取触摸点的坐标
- 获取图片的像素值
- Spring 3.0 注解注入详解
- [工具]ngrok 本地反向代理
- 微信公众帐号开发教程第10篇-解析接口中的消息创建时间CreateTime
- ruby如何查找一个方法属于哪个类
- Openvswitch GRE实现Kubernetes网络模型
- Android下正确获取屏幕触摸点下的图片像素值
- vmvare 不能使用的解决办法
- 编程基础_里氏转换的练习
- 在同一个页面使用多个不同的jQuery版本,让它们并存而不冲突
- AngularJS --app.js 学习笔记(一)
- python读取文件操作
- Win7 下VMware中如何让CentOS上网?
- 微信公众帐号开发教程第11篇-符号表情的发送(上)
- An internal error occurred during: "Add Deployment"完美解决办法