-1

I am working on the App "KISS ME" , in which when a user touch the background image a paste of the lips left behind where he touches the image .... I want that whenever a user touches the image other than the face of the girl , the paste don't show up ...Paste only shows on the face how can i do it only for the face??? Kindly help me out i have googled it but couldn't find anything

Here is my Main.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    linearLayout = (RelativeLayout) findViewById(R.id.kt);
    spool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
    spool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId,
                int status) {
            Log.i("OnLoadCompleteListener", "Sound " + sampleId
                    + " loaded.");
            boolean loaded = true;

        }
    });

    linearLayout.setOnTouchListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    Log.d("TAG for onCreatMenu", "Called");
    MenuInflater mInflater = getMenuInflater();
    mInflater.inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.lt:
        Intent i = new Intent(Main.this, LipseListView.class);
        startActivity(i);
        return true;

    case R.id.bi:
        Intent b = new Intent(Main.this, BackgroundList.class);
        startActivity(b);
        return true;

    case R.id.ls:
        Intent s = new Intent(Main.this, SoundList.class);
        startActivity(s);
        return true;

    case R.id.exit:
        AlertDialog alertbox = new AlertDialog.Builder(this)
                .setMessage("Do you want to exit application?")
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface arg0,
                                    int arg1) {

                                finish();

                            }
                        })
                .setNegativeButton("No",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface arg0,
                                    int arg1) {
                            }
                        }).show();

    default:
        return false;
    }

}

@Override
protected void onResume() {
    super.onResume();
    linearLayout
            .setBackgroundResource(BackgroundList.images[BackgroundList.position2]);
    this.soundId = this.spool.load(this,
            SoundList.sound[SoundList.position3], 1);
}

@Override
public boolean onTouch(View arg0, MotionEvent event) {

    int action = event.getAction();
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        downx = event.getX();
        downy = event.getY();
        runImage = new ImageView(Main.this);
        runImage.setBackgroundResource(LipseListView.images[LipseListView.position]);
        androidanimation = (AnimationDrawable) runImage.getBackground();

        runImage.setX(downx);
        runImage.setY(downy);
        linearLayout.addView(runImage);
        androidanimation.start();

        spool.play(soundId, 1, 1, 1, 0, 1);

        break;

    case MotionEvent.ACTION_UP:
        upx = event.getX();
        upy = event.getY();
        this.linearLayout.invalidate();
        runImage.setBackgroundResource(0);

        ImageView img = new ImageView(this);
        img.setX(upx);
        img.setY(upy);
        img.setBackgroundResource(LipseListView.images[LipseListView.position]);

        linearLayout.addView(img);

        Bitmap cameraBitmap = BitmapFactory.decodeResource(getResources(),
                BackgroundList.images[BackgroundList.position2]);
        int wid = cameraBitmap.getWidth();
        int hgt = cameraBitmap.getHeight();

        Bitmap newBitmap = Bitmap.createBitmap(wid, hgt,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawBitmap(cameraBitmap, 0f, 0f, null);
        Drawable drawable = getResources().getDrawable(
                R.drawable.ic_launcher);
        drawable.setBounds(20, 30, drawable.getIntrinsicWidth() + 20,
                drawable.getIntrinsicHeight() + 30);
        drawable.draw(canvas);

        break;

    case MotionEvent.ACTION_CANCEL:
        break;
    default:
        break;
    }
    return true;

}
}

Here is my main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/kt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
tools:context=".Main" >

</RelativeLayout>
VadymVL
  • 5,366
  • 3
  • 26
  • 41

2 Answers2

1

You need to represent the face in the image as a geometric shape such as a square or a circle. Say you represented it as a circle with center (c1,c2) and radius r. Now everytime the user touches the imageView, you will get the x and y of the touch point. You calculate the distance between (c1,c2) and (x,y). If the distance is greater than r then the user touched a point outside the circle, meaning outside the face. If the distance was less than r, then the user touch was inside the face, then you show up the paste.

If you look at the picture, the face is represented by the yellow circle that has a center at (xc, yc) and a radius r. Say now the user touches the ImageView at the point Touch 1 (xt,yt). You calculate the distance between (xc, yc) and (xt, yt). If the distance is greater than the radius r then the touch was outside the circle, so outside the face. On the other hand, if user touches at point Touch 2. The distance will be less than the radius so inside the face.

Distance formula between two points (x1,y1) and (x2,y2) is: sqroot((x2−x1)^2+(y2−y1)^2)

Example

Hope this helps. Let me know if you have any questions.

Ziad Halabi
  • 964
  • 11
  • 31
0

This might be useful.

Create a Circular ImageView in Android

By using a custom ImageView you will be able to use the onClick event to figure out when to show the paste.

Community
  • 1
  • 1