Android 图片设置坐标点

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'//添加扩展
}


class MainActivity : AppCompatActivity() {
    var downX: Float = 0f;var downY: Float = 0f
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setImageXY()//图片设置坐标点 也就是设置图片左上的点
        setRLOnTouch()

    }

    private fun setRLOnTouch() {
        rl_test.setOnTouchListener(object : View.OnTouchListener {
            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
                when (event?.action) {
                    MotionEvent.ACTION_DOWN -> getXY(event)
                }
                return false
            }
        })
    }
    private fun setImageXY() {
        val bmp = BitmapFactory.decodeResource(getResources(), R.drawable.test)//获取图片资源
        iv_test.setImageBitmap(bmp)
        iv_test.x=100f
        iv_test.y=100f
    }

    private fun getXY(event: MotionEvent) {
        downX = event.getX()
        downY = event.getY()
        Log.e("x1", "downX:" + downX + " downY:" + downY)
        SharePreUtil.putFloat(this,"downX",100f);
        SharePreUtil.putFloat(this,"downY",100f);
    }
}
SharePreUtil类
object SharePreUtil {
    /**
     * 配置文件,文件名
     */
    private val SHARE_NAME = "config"

    /**
     * 存字符串
     *
     * @param context 上下文
     * @param key     键
     * @param values  值
     */
    fun putString(context: Context, key: String?, values: String?) {
        val sp: SharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE)
        sp.edit().putString(key, values).apply()
    }

    /**
     * 取字符串
     *
     * @param context 上下文
     * @param key     键
     * @param values  默认值
     * @return 取出的值
     */
    fun getString(context: Context, key: String?, values: String?): String? {
        val sp: SharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE)
        return sp.getString(key, values)
    }

    /**
     * 存布尔值
     *
     * @param context 上下文
     * @param key     键
     * @param values  值
     */
    fun putBoolean(context: Context, key: String?, values: Boolean) {
        val sp: SharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE)
        sp.edit().putBoolean(key, values).apply()
    }

    /**
     * 取布尔值
     *
     * @param context 上下文
     * @param key     键
     * @param values  默认值
     * @return true/false
     */
    fun getBoolean(context: Context, key: String?, values: Boolean): Boolean {
        val sp: SharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE)
        return sp.getBoolean(key, values)
    }

    /**
     * 存int值
     *
     * @param context 上下文
     * @param key     键
     * @param values  值
     */
    fun putInt(context: Context, key: String?, values: Int) {
        val sp: SharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE)
        sp.edit().putInt(key, values).apply()
    }

    /**
     * 取int值
     *
     * @param context 上下文
     * @param key     键
     * @param values  默认值
     * @return
     */
    fun getInt(context: Context, key: String?, values: Int): Int {
        val sp: SharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE)
        return sp.getInt(key, values)
    }

    /**
     * 存int值
     *
     * @param context 上下文
     * @param key     键
     * @param values  值
     */
    fun putFloat(context: Context, key: String?, values: Float) {
        val sp: SharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE)
        sp.edit().putFloat(key, values).apply()
    }

    /**
     * 取int值
     *
     * @param context 上下文
     * @param key     键
     * @param values  默认值
     * @return
     */
    fun getFloat(context: Context, key: String?, values: Float): Float {
        val sp: SharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE)
        return sp.getFloat(key, values)
    }

    /**
     * 删除一条字段
     *
     * @param context 上下文
     * @param key     键
     */
    fun deleShare(context: Context, key: String?) {
        val sp: SharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE)
        //单个清理
        sp.edit().remove(key).apply()
    }

    /**
     * 删除全部数据
     *
     * @param context 上下文
     */
    fun deleShareAll(context: Context) {
        val sp: SharedPreferences = context.getSharedPreferences(SHARE_NAME, Context.MODE_PRIVATE)
        //全部清理
        sp.edit().clear().apply()
    }

    /**
     * 查看SharedPreferences的内容
     */
    fun lookSharePre(context: Context): String {
        try {
            val stream = FileInputStream(
                File(
                    "/data/data/" +
                            context.getPackageName().toString() + "/shared_prefs",
                    SHARE_NAME + ".xml"
                )
            )
            val bff = BufferedReader(InputStreamReader(stream))
            var line: String?
            val sb = StringBuilder()
            while ((bff.readLine().also { line = it }) != null) {
                sb.append(line)
                sb.append("\n")
            }
            return sb.toString()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return "未找到当前配置文件!"
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值