XML
文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<com.gallery20.app.MyImageView
android:id="@+id/real_iv"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#00000000"/>
</LinearLayout>
Activity
代码
const val TAG = "Yang"
class MainActivity : AppCompatActivity() {
var mRealView: MyImageView? = null
var tempBitmap: Bitmap? = null
var screenWidth = 0
var screenHeight = 0
var mDestRect = RectF()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mRealView = findViewById(R.id.real_iv)
screenWidth = resources.displayMetrics.widthPixels
screenHeight = resources.displayMetrics.heightPixels
val tempRect = RectF(0f, 0f, screenWidth.toFloat() / 2, screenHeight.toFloat() / 2)
CoroutineScope(Dispatchers.IO).launch {
tempBitmap = getBitmap(resources, tempRect, R.drawable.fake)
withContext(Dispatchers.Main) {
mRealView?.setImageBitmap(tempBitmap)
}
}
}
}
fun getBitmap(resources : Resources, destRect : RectF, imageId: Int): Bitmap? {
var imageWidth = -1
var imageHeight = -1
val preOption = BitmapFactory.Options().apply {
inJustDecodeBounds = true
BitmapFactory.decodeResource(resources, imageId, this)
}
imageWidth = preOption.outWidth
imageHeight = preOption.outHeight
val scaleMatrix = Matrix()
var srcRect = RectF(0f, 0f, imageWidth.toFloat(), imageHeight.toFloat())
scaleMatrix.setRectToRect(srcRect, destRect, Matrix.ScaleToFit.CENTER)
scaleMatrix.mapRect(srcRect)
val finalOption = BitmapFactory.Options().apply {
if (imageHeight > 0 && imageWidth > 0) {
inPreferredConfig = Bitmap.Config.ARGB_8888
inSampleSize = calculateInSampleSize(
imageWidth,
imageHeight,
srcRect.width().toInt(),
srcRect.height().toInt()
)
}
}
return BitmapFactory.decodeResource(resources, imageId, finalOption)
}
fun calculateInSampleSize(fromWidth: Int, fromHeight: Int, toWidth: Int, toHeight: Int): Int {
var bitmapWidth = fromWidth
var bitmapHeight = fromHeight
if (fromWidth > toWidth|| fromHeight > toHeight) {
var inSampleSize = 1
while (bitmapWidth >= toWidth && bitmapHeight >= toHeight) {
bitmapWidth /= 2
bitmapHeight /= 2
inSampleSize *= 2
}
return inSampleSize
}
return 1
}
自定义View
代码
class MyImageView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatImageView(context, attrs, defStyleAttr){
private var lastX = 0f
private var lastY = 0f
override fun onTouchEvent(event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
lastX = event.rawX
lastY = event.rawY
}
MotionEvent.ACTION_MOVE -> {
val dx = event.rawX - lastX
val dy = event.rawY - lastY
val left = left + dx.toInt()
val top = top + dy.toInt()
val right = right + dx.toInt()
val bottom = bottom + dy.toInt()
layout(left, top, right, bottom)
lastX = event.rawX
lastY = event.rawY
}
}
return true
}
}
event.x
和 event.y
返回的是触摸点相对于当前View
左上角(0, 0)
的坐标, event.rawX
和 event.rawY
返回的是触摸位置相对于整个屏幕左上角的坐标- 在
ACTION_DOWN
和ACTION_MOVE
事件中使用event.rawX
和 event.rawY
计算的水平和垂直方向移动的距离是相对整个屏幕左上角的移动距离,以整个屏幕为参照系,用于需要以屏幕为基准进行操作的情况,比如移动或者拖拽一个View - 在
ACTION_DOWN
和ACTION_MOVE
事件中使用event.X
和 event.Y
计算的水平和垂直方向移动的距离是相对当前View
左上角(0, 0)
的移动距离,以当前View
为参照系,用于在固定不移动的View
内部进行操作,比如处理一个ViewGroup
的里某个View
的旋转、平移、缩放
效果图