补5-22博客 获取点击坐标并将控件移动到点击位置
2017-05-23 10:41
465 查看
package doge.healthcare.activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.Toast; import doge.healthcare.R; import doge.healthcare.base.BaseActivity; /** * Created by doge on 2017/5/22. */ public class Measure extends BaseActivity { private RelativeLayout rl_map; private ImageView iv_location; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second_measure); rl_map= (RelativeLayout) findViewById(R.id.rl_map); iv_location=(ImageView)findViewById(R.id.iv_location); //添加触摸事件 rl_map.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { //获取点击位置的坐标 int x = (int) arg1.getX(); int y = (int) arg1.getY(); Toast.makeText(getApplicationContext(),"横坐标"+x+"纵坐标"+y,Toast.LENGTH_SHORT).show(); //设定imageview的位置到点击处 ViewGroup.MarginLayoutParams margin=new ViewGroup.MarginLayoutParams(iv_location.getLayoutParams()); margin.setMargins(x,y, x+margin.width, y+margin.height); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin); iv_location.setLayoutParams(layoutParams); return false; } }); } }xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/title" ></include> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rl_map" android:clickable="true"> <ImageView android:id="@+id/iv_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/icon_xihuan_dianji"/> </RelativeLayout> </LinearLayout>
效果如下图
相关文章推荐
- MFC 获取picture控件的鼠标点击坐标位置的方法
- MFC 获取picture控件的鼠标点击坐标位置的方法(PreTranslateMessag)
- MFC 获取picture控件的鼠标点击坐标位置的方法
- 获取光标位置 并将其位置映射为上一博客中picture控件的逻辑坐标
- MFC 获取picture控件的鼠标点击坐标位置的方法
- 获取鼠标点击处的控件并获取其坐标
- android 获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度
- vb Postmessage后台模拟点击句柄控件指定坐标位置
- android获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度
- 用户控件 + ScriptManager 实现点击后弹出窗口且窗口坐标追随点击位置效果
- android获取控件位置坐标,屏幕尺寸,标题栏,状态栏高度
- 在WebBrowser控件中获取鼠标在网页上点击的位置
- 在WebBrowser控件中获取鼠标在网页上点击的位置
- 基于JavaScript实现 获取鼠标点击位置坐标的方法
- JavaScript 获取鼠标点击位置坐标
- 在WebBrowser控件中获取鼠标在网页(不是浏览器窗口)上点击的位置,
- vb Postmessage后台模拟点击句柄控件指定坐标位置
- 如何获取控件在窗体上的位置坐标和大小值
- js获取控件坐标以及位置定位
- 在PV3D中获取鼠标点击位置的空间坐标信息