如何触摸图像视图中的特定位置

时间:2015-04-16 07:08:31

标签: android

我正在开发应用程序“KISS ME”,其中当用户触摸背景图像时,留下的粘贴留在他触摸图像的位置....我希望每当用户触摸除了图像之外的图像女孩的脸,粘贴不显示...粘贴只显示在脸上我怎么能只为脸做?请帮助我,我用谷歌搜索它但找不到任何东西

这是我的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;

}
}

这是我的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>

2 个答案:

答案 0 :(得分:1)

您需要将图像中的面部表示为几何形状,例如方形或圆形。假设您将其表示为具有中心(c1,c2)和半径r的圆。现在,每当用户触摸imageView时,您将获得触摸点的x和y。您计算(c1,c2)和(x,y)之间的距离。如果距离大于r,则用户触摸圆外的点,即面部外。如果距离小于r,则用户触摸在面部内部,然后显示粘贴。

如果你看图片,脸部由黄色圆圈表示,圆圈的中心位于(xc,yc),半径为r。现在假设用户在Touch 1(xt,yt)点触摸ImageView。您计算(xc,yc)和(xt,yt)之间的距离。如果距离大于半径r,那么触摸在圆圈之外,因此在脸部外面。另一方面,如果用户触摸点触摸2.距离将小于半径,所以在脸部内。

两点(x1,y1)和(x2,y2)之间的距离公式为:sqroot((x2−x1)^2+(y2−y1)^2)

Example

希望这会有所帮助。如果您有任何问题,请告诉我。

答案 1 :(得分:0)

这可能很有用。

Create a Circular ImageView in Android

通过使用自定义ImageView,您将能够使用onClick事件来确定何时显示粘贴。