0
如何將數據從自定義視圖傳遞給父活動?我有自定義視圖中的哪個框被觸摸的數據。我想要在接觸盒子時將這些數據傳遞給父項活動。將觸摸事件從自定義視圖傳遞到活動android
這裏是我的活動佈局: 1
編輯: 這裏的父活動:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible">
<SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
</SearchView>
</RelativeLayout>
<com.example.gaurav.dummyapp.FaceOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/face_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
FaceOverlayView.java
公共類FaceOverlayView擴展視圖{
private Bitmap mBitmap;
private SparseArray<Face> mFaces;
float left = 0;
float top = 0;
float right = 0;
float bottom = 0;
double sc;
private String faceData;
public FaceOverlayView(Context context) {
this(context, null);
}
public FaceOverlayView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public FaceOverlayView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setBitmap(Bitmap bitmap) {
mBitmap = bitmap;
FaceDetector detector = new FaceDetector.Builder(getContext())
.setTrackingEnabled(true)
.setLandmarkType(FaceDetector.ALL_LANDMARKS)
.setMode(FaceDetector.ACCURATE_MODE)
.build();
if (!detector.isOperational()) {
//Handle contingency
} else {
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
mFaces = detector.detect(frame);
detector.release();
}
logFaceData();
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if ((mBitmap != null) && (mFaces != null)) {
double scale = drawBitmap(canvas);
drawFaceBox(canvas, scale);
}
}
private double drawBitmap(Canvas canvas) {
double viewWidth = canvas.getWidth();
double viewHeight = canvas.getHeight();
double imageWidth = mBitmap.getWidth();
double imageHeight = mBitmap.getHeight();
double scale = Math.min(viewWidth/imageWidth, viewHeight/imageHeight);
sc = scale;
Rect destBounds = new Rect(0, 0, (int)(imageWidth * scale), (int)(imageHeight * scale));
canvas.drawBitmap(mBitmap, null, destBounds, null);
return scale;
}
private void drawFaceBox(Canvas canvas, double scale) {
//This should be defined as a member variable rather than
//being created on each onDraw request, but left here for
//emphasis.
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
for(int i = 0; i < mFaces.size(); i++) {
Face face = mFaces.valueAt(i);
left = (float) (face.getPosition().x * scale);
top = (float) (face.getPosition().y * scale);
right = (float) scale * (face.getPosition().x + face.getWidth());
bottom = (float) scale * (face.getPosition().y + face.getHeight());
canvas.drawRect(left, top, right, bottom, paint);
}
}
private void drawFaceLandmarks(Canvas canvas, double scale) {
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
for(int i = 0; i < mFaces.size(); i++) {
Face face = mFaces.valueAt(i);
for (Landmark landmark : face.getLandmarks()) {
int cx = (int) (landmark.getPosition().x * scale);
int cy = (int) (landmark.getPosition().y * scale);
canvas.drawCircle(cx, cy, 10, paint);
}
}
}
private void logFaceData() {
float smilingProbability;
float leftEyeOpenProbability;
float rightEyeOpenProbability;
float eulerY;
float eulerZ;
for(int i = 0; i < mFaces.size(); i++) {
Face face = mFaces.valueAt(i);
smilingProbability = face.getIsSmilingProbability();
leftEyeOpenProbability = face.getIsLeftEyeOpenProbability();
rightEyeOpenProbability = face.getIsRightEyeOpenProbability();
eulerY = face.getEulerY();
eulerZ = face.getEulerZ();
Log.e("Face Detection", "Smiling: " + smilingProbability);
Log.e("Face Detection", "Left eye open: " + leftEyeOpenProbability);
Log.e("Face Detection", "Right eye open: " + rightEyeOpenProbability);
Log.e("Face Detection", "Euler Y: " + eulerY);
Log.e("Face Detection", "Euler Z: " + eulerZ);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction() & MotionEvent.ACTION_MASK;
int x = (int) event.getX();
int y = (int) event.getY();
float eulerY;
float eulerZ;
switch(action) {
case MotionEvent.ACTION_DOWN : {
for(int i = 0; i < mFaces.size(); i++) {
Face face = mFaces.valueAt(i);
left = (float) (face.getPosition().x * sc);
top = (float) (face.getPosition().y * sc);
right = (float) sc * (face.getPosition().x + face.getWidth());
bottom = (float) sc * (face.getPosition().y + face.getHeight());
if((x > left && x < right) && (y > top && y < bottom)){
Log.e("BOX>>>>>>>>>>", String.valueOf(i));
}
}
break;
}
case MotionEvent.ACTION_MOVE : {
//path.lineTo(event.getX(), event.getY());
break;
}
}
invalidate();
return super.onTouchEvent(event);
}
活動課: 公共類dummyActivity擴展AppCompatActivity {
private FaceOverlayView fooView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy);
fooView.setBitmap("bitmap");
}
數據是被點擊的框的整數值。 –
給我的佈局.xml,但即時編輯它 – user2582318
我正在做臉部檢測,這些箱子是臉部框。我已將這些框的索引作爲我必須傳遞給活動的數據。 –