如何在android中获得视图的绝对x和y坐标?
我正在从相机上获取实时预览,将其转换为位图,并从视图中心获取像素颜色。我尝试了view.getX()和view.getY(),但它总是给出0,0。这是我的xml代码-如何在android中获得视图的绝对x和y坐标?,android,android-camera,Android,Android Camera,我正在从相机上获取实时预览,将其转换为位图,并从视图中心获取像素颜色。我尝试了view.getX()和view.getY(),但它总是给出0,0。这是我的xml代码- <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:la
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:id="@+id/button_capture"
android:text="Capture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="onCapClick"
/>
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/iv5"
android:layout_centerInParent="true"
android:background="@drawable/square"/>
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/iv2"
android:layout_centerHorizontal="true"
android:layout_above="@id/iv5"
android:layout_marginBottom="55dp"
android:background="@drawable/square"/>
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/iv8"
android:layout_centerHorizontal="true"
android:layout_below="@id/iv5"
android:layout_marginTop="55dp"
android:background="@drawable/square"/>
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/iv6"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/iv5"
android:layout_marginLeft="55dp"
android:background="@drawable/square"/>
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/iv4"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/iv5"
android:layout_marginRight="55dp"
android:background="@drawable/square"/>
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/iv3"
android:layout_toRightOf="@id/iv5"
android:layout_marginLeft="55dp"
android:layout_above="@id/iv5"
android:layout_marginBottom="55dp"
android:background="@drawable/square"/>
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/iv9"
android:layout_toRightOf="@id/iv5"
android:layout_marginLeft="55dp"
android:layout_below="@id/iv5"
android:layout_marginTop="55dp"
android:background="@drawable/square"/>
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/iv1"
android:layout_toLeftOf="@id/iv5"
android:layout_marginRight="55dp"
android:layout_above="@id/iv5"
android:layout_marginBottom="55dp"
android:background="@drawable/square"/>
<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/iv7"
android:layout_toLeftOf="@id/iv5"
android:layout_marginRight="55dp"
android:layout_below="@id/iv5"
android:layout_marginTop="55dp"
android:background="@drawable/square"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv1"
android:text="Default"
android:textSize="30sp"
android:background="@android:color/transparent"
android:textColor="#FFFF00"/>
由于getX和getY不起作用,我已经手动输入了值,但它变成了特定于设备的。如何获得X和Y值?
这是我的班级代码-
package com.example.sid.rcs20;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.drawable.ColorDrawable;
import android.hardware.Camera;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicYuvToRGB;
import android.renderscript.Type;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.List;
public class Cameras extends AppCompatActivity implements Camera.PreviewCallback{
private Camera mCamera;
private CameraPreview mPreview;
static int width, height;
private ImageView iv1, iv2, iv3, iv4, iv5, iv6, iv7, iv8, iv9;
private FrameLayout frameLayout;
private String p51 ,p52, p53, p54, p55;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.camera_preview);
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
iv4 = (ImageView) findViewById(R.id.iv4);
iv5 = (ImageView) findViewById(R.id.iv5);
iv6 = (ImageView) findViewById(R.id.iv6);
iv7 = (ImageView) findViewById(R.id.iv7);
iv8 = (ImageView) findViewById(R.id.iv8);
iv9 = (ImageView) findViewById(R.id.iv9);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
height = displayMetrics.heightPixels;
width = displayMetrics.widthPixels;
Log.d("phone size", String.valueOf(width) + "\t" + String.valueOf(height));
if (!checkCameraHardware(this)) {
Toast.makeText(this, "NO CAMERA DETECTED", Toast.LENGTH_SHORT).show();
} else {
mCamera = getCameraInstance();
Camera.Parameters params = mCamera.getParameters();
List<Camera.Size> sizes = params.getSupportedPictureSizes();
Camera.Size size = sizes.get(0);
//Camera.Size size1 = sizes.get(0);
for (int i = 0; i < sizes.size(); i++) {
Log.d("all sizes", String.valueOf(sizes.get(i).width) + "\t" + String.valueOf(sizes.get(i).width));
if (sizes.get(i).width > size.width) {
size = sizes.get(i);
}
}
Log.d("size", String.valueOf(size.width) + "\t" + String.valueOf(size.width));
params.setPictureSize(size.width, size.height);
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
params.setSceneMode(Camera.Parameters.SCENE_MODE_AUTO);
params.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
params.setExposureCompensation(0);
params.setPictureFormat(ImageFormat.JPEG);
params.setJpegQuality(100);
params.setRotation(90);
mCamera.setDisplayOrientation(90);
mCamera.setParameters(params);
mCamera.setPreviewCallback(this);
mPreview = new CameraPreview(this, mCamera);
frameLayout.addView(mPreview);
}
}
/**
* Check if this device has a camera
*/
private boolean checkCameraHardware(Context context) {
// this device has a camera
// no camera on this device
return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
}
/**
* A safe way to get an instance of the Camera object.
*/
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
/**
* Create a File for saving an image or video
*/
@Override
protected void onPause() {
super.onPause();
releaseCamera(); // release the camera immediately on pause event
}
private void releaseCamera() {
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mPreview.getHolder().removeCallback(mPreview);
mCamera.release(); // release the camera for other applications
mCamera = null;
}
}
@Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
int frameHeight = mCamera.getParameters().getPreviewSize().height;
int frameWidth = camera.getParameters().getPreviewSize().width;
Log.d("preview size", String.valueOf(frameWidth) + "\t" + String.valueOf(frameHeight));
Bitmap bitmap = Bitmap.createBitmap(frameWidth, frameHeight, Bitmap.Config.ARGB_8888);
Allocation bmData = renderScriptNV21ToRGBA888(
this,
frameWidth,
frameHeight,
bytes);
bmData.copyTo(bitmap);
Bitmap bmp2 = RotateBitmap(bitmap, 90);
int pixel1 = bmp2.getPixel(145, 415);
int pixel2 = bmp2.getPixel(365, 415);
int pixel3 = bmp2.getPixel(585, 415);
int pixel4 = bmp2.getPixel(145, 630);
int pixel51 = bmp2.getPixel(365, 630);
int pixel52 = bmp2.getPixel(306, 570);
int pixel53 = bmp2.getPixel(425, 690);
int pixel54 = bmp2.getPixel(425, 570);
int pixel55 = bmp2.getPixel(306, 690);
int pixel6 = bmp2.getPixel(585, 630);
int pixel7 = bmp2.getPixel(145, 865);
int pixel8 = bmp2.getPixel(365, 865);
int pixel9 = bmp2.getPixel(585, 865);
changeRect(pixel1, 1);
changeRect(pixel2, 2);
changeRect(pixel3, 3);
changeRect(pixel4, 4);
changeRect(pixel51, 51);
changeRect(pixel52, 52);
changeRect(pixel53, 53);
changeRect(pixel54, 54);
changeRect(pixel55, 55);
changeRect(pixel6, 6);
changeRect(pixel7, 7);
changeRect(pixel8, 8);
changeRect(pixel9, 9);
}
private void changeRect(int pixel, int num) {
int r = Color.red(pixel);
int g = Color.green(pixel);
int b = Color.blue(pixel);
ColorUtil colorUtil = new ColorUtil();
String name = colorUtil.getColorNameFromRgb(r, g, b);
String[] idAndOurName = getDrawableIdAndOurName(name);
int d = Integer.parseInt(idAndOurName[0]);
String ourName = idAndOurName[1];
if (num == 1) {
iv1.setImageResource(d);
} else if (num == 2) {
iv2.setImageResource(d);
} else if (num == 3) {
iv3.setImageResource(d);
} else if (num == 4) {
iv4.setImageResource(d);
} else if (num == 51) {
p51 = ourName;
} else if (num == 52) {
p52 = ourName;
} else if (num == 53) {
p53 = ourName;
} else if (num == 54) {
p54 = ourName;
} else if (num == 55) {
p55 = ourName;
d = getDrawableId();
iv5.setImageResource(d);
} else if (num == 6) {
iv6.setImageResource(d);
} else if (num == 7) {
iv7.setImageResource(d);
} else if (num == 8) {
iv8.setImageResource(d);
} else if (num == 9) {
iv9.setImageResource(d);
}
Log.d("Selected Color", String.valueOf(num) + "\t" + name);
}
private int getDrawableId(){
int[] cA = new int[]{0,0,0,0,0,0};
cA = getCount(p51 , cA);
cA = getCount(p52 , cA);
cA = getCount(p53 , cA);
cA = getCount(p54 , cA);
cA = getCount(p55 , cA);
if (cA[0]>=2){
return R.drawable.yellow;
}
else if (cA[1]>=2){
return R.drawable.green;
}
else if (cA[2]>=2){
return R.drawable.blue;
}
else if (cA[3]==5){
return R.drawable.white;
}
else if (cA[5]>=1){
return R.drawable.red;
}
else if (cA[4]>=2){
return R.drawable.orange;
}
else
return R.drawable.square;
}
private int[] getCount(String s, int[] countArray){
switch (s) {
case "y":
countArray[0]++;
break;
case "g":
countArray[1]++;
break;
case "b":
countArray[2]++;
break;
case "w":
countArray[3]++;
break;
case "o":
countArray[4]++;
break;
case "r":
countArray[5]++;
break;
}
return countArray;
}
private String[] getDrawableIdAndOurName(String name){
if (name.contains("Yellow")) {
return new String[]{String.valueOf(R.drawable.yellow), "y"};
}
else if (name.contains("White")) {
return new String[]{String.valueOf(R.drawable.white), "w"};
}
else if (name.contains("Green")) {
return new String[]{String.valueOf(R.drawable.green), "g"};
}
else if (name.contains("Blue")) {
return new String[]{String.valueOf(R.drawable.blue), "b"};
}
else if (name.contains("Red")) {
return new String[]{String.valueOf(R.drawable.red), "r"};
}
else if (name.contains("Orange")) {
return new String[]{String.valueOf(R.drawable.orange), "o"};
}
else return new String[]{"" , ""};
}
public Allocation renderScriptNV21ToRGBA888(Context context, int width, int height, byte[] nv21) {
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
in.copyFrom(nv21);
yuvToRgbIntrinsic.setInput(in);
yuvToRgbIntrinsic.forEach(out);
return out;
}
public static Bitmap RotateBitmap(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
public void onCapClick(View view) {
mCamera.stopPreview();
frameLayout.setForeground(new ColorDrawable(Color.WHITE));
}
package com.example.sid.rcs20;
导入android.content.Context;
导入android.content.pm.PackageManager;
导入android.graphics.Bitmap;
导入android.graphics.Color;
导入android.graphics.ImageFormat;
导入android.graphics.Matrix;
导入android.graphics.drawable.ColorDrawable;
导入android.hardware.Camera;
导入android.os.Bundle;
导入android.renderscript.Allocation;
导入android.renderscript.Element;
导入android.renderscript.renderscript;
导入android.renderscript.ScriptIntrinsicYuvToRGB;
导入android.renderscript.Type;
导入android.support.v7.app.AppActivity;
导入android.util.DisplayMetrics;
导入android.util.Log;
导入android.view.view;
导入android.widget.FrameLayout;
导入android.widget.ImageView;
导入android.widget.Toast;
导入java.util.List;
公共类Cameras扩展AppCompatActivity实现Camera.PreviewCallback{
私人摄像机麦卡梅拉;
私人摄像审查;
静态int宽度、高度;
私人影像视图iv1、iv2、iv3、iv4、iv5、iv6、iv7、iv8、iv9;
私有框架布局;
私有字符串p51、p52、p53、p54、p55;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout=(frameLayout)findviewbyd(R.id.camera\u预览);
iv1=(ImageView)findViewById(R.id.iv1);
iv2=(ImageView)findViewById(R.id.iv2);
iv3=(ImageView)findViewById(R.id.iv3);
iv4=(ImageView)findViewById(R.id.iv4);
iv5=(ImageView)findViewById(R.id.iv5);
iv6=(ImageView)findViewById(R.id.iv6);
iv7=(ImageView)findViewById(R.id.iv7);
iv8=(ImageView)findViewById(R.id.iv8);
iv9=(ImageView)findViewById(R.id.iv9);
DisplayMetrics DisplayMetrics=新的DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
高度=displayMetrics.heightPixels;
宽度=displayMetrics.widthPixels;
Log.d(“电话大小”,String.valueOf(宽度)+“\t”+String.valueOf(高度));
如果(!检查摄像头硬件(此)){
Toast.makeText(这是“未检测到照相机”,Toast.LENGTH_SHORT).show();
}否则{
mCamera=getCameraInstance();
Camera.Parameters params=mCamera.getParameters();
列表大小=params.getSupportedPictureSizes();
Camera.Size=Size.get(0);
//Camera.Size Size Size=Size.get(0);
对于(int i=0;isize.width){
大小=大小。获取(i);
}
}
Log.d(“size”,String.valueOf(size.width)+“\t”+String.valueOf(size.width));
参数setPictureSize(大小、宽度、大小、高度);
参数设置聚焦模式(相机参数聚焦模式连续图像);
参数设置闪光模式(摄像机参数闪光模式);
参数设置场景模式(摄像机参数场景模式自动);
参数设置白平衡(摄像机参数白平衡自动);
参数setExposureCompensation(0);
参数setPictureFormat(ImageFormat.JPEG);
参数设置质量(100);
参数设置旋转(90);
mCamera.setDisplayOrientation(90);
mCamera.setParameters(参数);
mCamera.setPreviewCallback(此);
mPreview=新的CameraPreview(这是mCamera);
frameLayout.addView(mPreview);
}
}
/**
*检查此设备是否有摄像头
*/
专用布尔checkCameraHardware(上下文){
//这个装置有一个照相机
//此设备上没有摄像头
返回context.getPackageManager().hasSystemFeature(PackageManager.FEATURE\u摄像头);
}
/**
*获取摄影机对象实例的安全方法。
*/
公共静态摄像头getCameraInstance(){
摄像机c=null;
试一试{
c=Camera.open();//尝试获取一个Camera实例
}捕获(例外e){
//摄像头不可用(正在使用或不存在)
}
返回c;//如果摄像机不可用,则返回null
}
/**
*创建用于保存图像或视频的文件
*/
@凌驾
受保护的void onPause(){
super.onPause();
releaseCamera();//暂停事件时立即释放相机
}
私有无效释放摄影机(){
if(mCamera!=null){
mCamera.setPreviewCallback(null);
mPreview.getHolder().removeCallback(mPreview);
mCamera.release();//为其他应用程序释放相机
mCamera=null;
}
}
@凌驾
预览帧上的公共无效(字节[]字节,摄影机){
int frameHeight=mCamera.getParameters().getPreviewSize().height;
int frameWidth=camera.getParameters().getPreviewSize().width;
Log.d(“预览大小”,String.valueOf(frameWidth)+“\t”+String.valueOf(frameHeight));
位图Bitmap=Bitmap.createBitmap(frameWidth、frameHeight、Bitmap.Config.ARGB_8888);
分配bmData=renderscriptnv21torgb888(
这
帧宽,
框架高度,
字节);
bmData.copyTo(位图);
位图bmp2=旋转位图(位图,90);
int pixel1=bmp2.getPixel(145415);
int pixel2=bmp2.getPixel(365,415);
int pixel3=bmp2.getPixel(585415);
int pixel4=bmp2.getPixel(145630);
int pixel51=bmp2.getPixel(365630);
int pixel52=bmp2.getPixel(306570);
int pixel53=bmp2.getPixel(425690);
int pixel54=bmp2.getPixel(425570);
int pixel55=bmp2.getPixel(306690);
int pixel6=bmp2.getPixel(585
int test1[] = new int[2];
contentsView.getLocationInWindow(test1);
//Or
int test2[] = new int[2];
contentsView.getLocationOnScreen(test2);