如何触摸特定现在的位置在图像视图 [英] How to touch the specific postion in the image view

查看:249
本文介绍了如何触摸特定现在的位置在图像视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AppKISS ME,其中当用户触摸的背景图像左侧,在那里他接触到形象背后的嘴唇贴合工作....我想,只要用户触摸以外的图像女孩的脸上,贴显示不出来...粘贴只显示在脸上我怎么能只为脸部做?请帮助我,我用Google搜索,但没有找到任何东西。

下面是我的Main.java

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    的LinearLayout =(RelativeLayout的)findViewById(R.id.kt);
    阀芯=新的Soundpool(3,AudioManager.STREAM_MUSIC,0);
    spool.setOnLoadCompleteListener(新OnLoadCompleteListener(){
        @覆盖
        公共无效的onLoadComplete(的Soundpool的Soundpool,INT sampleId,
                INT状态){
            Log.i(OnLoadCompleteListener,声音+ sampleId
                    +加载。);
            布尔装= TRUE;        }
    });    linearLayout.setOnTouchListener(本);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    // TODO自动生成方法存根
    Log.d(TAG为onCreatMenu,被称为);
    MenuInflater mInflater = getMenuInflater();
    mInflater.inflate(R.menu.main,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){    开关(item.getItemId()){
    案例R.id.lt:
        意图I =新意图(Main.this,LipseListView.class);
        startActivity(ⅰ);
        返回true;    案例R.id.bi:
        意向B =新意图(Main.this,BackgroundList.class);
        startActivity(二);
        返回true;    案例R.id.ls:
        意图S =新意图(Main.this,SoundList.class);
        startActivity(多个);
        返回true;    案例R.id.exit:
        AlertDialog alertbox =新AlertDialog.Builder(本)
                .setMessage(你想退出应用程序吗?)
                .setPositiveButton(是,
                        新DialogInterface.OnClickListener(){
                            公共无效的onClick(DialogInterface为arg0,
                                    INT ARG1){                                完();                            }
                        })
                .setNegativeButton(否,
                        新DialogInterface.OnClickListener(){                            公共无效的onClick(DialogInterface为arg0,
                                    INT ARG1){
                            }
                        })。显示();    默认:
        返回false;
    }}@覆盖
保护无效onResume(){
    super.onResume();
    的LinearLayout
            .setBackgroundResource(BackgroundList.images [BackgroundList.position2]);
    this.soundId = this.spool.load(这一点,
            SoundList.sound [SoundList.position3],1);
}@覆盖
公共布尔onTouch(查看arg0中,MotionEvent事件){    INT行动= event.getAction();
    开关(动作){
    案例MotionEvent.ACTION_DOWN:
        downx = event.getX();
        柔和= event.getY();
        runImage =新ImageView的(Main.this);
        runImage.setBackgroundResource(LipseListView.images [LipseListView.position]);
        androidanimation =(AnimationDrawable)runImage.getBackground();        runImage.setX(downx);
        runImage.setY(柔和);
        linearLayout.addView(runImage);
        androidanimation.start();        spool.play(soundId,1,1,1,0,1);        打破;    案例MotionEvent.ACTION_UP:
        UPX = event.getX();
        upy = event.getY();
        this.linearLayout.invalidate();
        runImage.setBackgroundResource(0);        ImageView的IMG =新ImageView的(本);
        img.setX(UPX);
        img.setY(upy);
        img.setBackgroundResource(LipseListView.images [LipseListView.position]);        linearLayout.addView(IMG);        位图cameraBitmap = BitmapFactory.de codeResource(getResources()
                BackgroundList.images [BackgroundList.position2]);
        INT妇女参与发展= cameraBitmap.getWidth();
        INT HGT = cameraBitmap.getHeight();        位图newBitmap = Bitmap.createBitmap(WID,HGT,
                Bitmap.Config.ARGB_8888);
        帆布帆布=新的Canvas(newBitmap);
        canvas.drawBitmap(cameraBitmap,0F,0F,NULL);
        可绘制可绘制= getResources()。getDrawable(
                R.drawable.ic_launcher);
        drawable.setBounds(20,30,drawable.getIntrinsicWidth()+ 20,
                drawable.getIntrinsicHeight()+ 30);
        drawable.draw(画布);        打破;    案例MotionEvent.ACTION_CANCEL:
        打破;
    默认:
        打破;
    }
    返回true;}
}

下面是我的main.xml

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:ID =@ + ID / KT
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:可点击=假
工具:主上下文=>< / RelativeLayout的>


解决方案

您需要重新present图像作为几何形的脸,如方形或圆形。假设你重新presented它作为中心的圆(C1,C2)和半径r。现在每次用户触摸的ImageView,你会得到触摸点的X和Y。你计算之间(C1,C2)和(x,y)的距离。如果该距离大于r则用户触摸的点的圆外,面对外的意思。如果该距离小于r,则用户触摸了面部内,则显示的糊状物。

如果你看看图片,脸再由具有在中心的黄圈psented $ P $(XC,YC)和半径r。现在说用户触摸的点触摸1(XT,YT)的ImageView的。你计算(XC,YC)和(XT,YT)之间的距离。如果该距离大于半径r大于所述触摸是外循环,因此面外。另一方面,如果用户触摸在点触摸2.距离将小于面部内的半径,以便

两个点(X1,Y1)和(X2,Y2)之间

距离计算公式为: sqroot((X2-X1)^ 2 +(Y2-Y1)^ 2)

希望这有助于。让我知道如果你有任何问题。

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>

解决方案

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)

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

这篇关于如何触摸特定现在的位置在图像视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆