通过触摸在触摸坐标处将图像插入屏幕

时间:2017-05-09 12:53:30

标签: java android

我正在创建一个应用程序,需要将图像放在用户点击的位置的屏幕上。当用户第二次点击时,屏幕上显示的第一个图像消失了..

if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
    int x = (int) motionEvent.getX(); 
    int y = (int) motionEvent.getY(); 
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP‌​_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    ImageView iv = new ImageView(getApplicationContext()); 
    lp.setMargins(x, y, 0, 0); 
    iv.setLayoutParams(lp); 
    iv.setImageDrawable(getResources().getDrawable(R.drawable.a)‌​); 
    ((ViewGroup) view).addView(iv);
}

0 个答案:

没有答案