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 "未找到当前配置文件!"
}
}