【发布时间】:2019-07-07 21:10:31
【问题描述】:
我正在尝试在图像视图中加载从设备相机返回的图像并获取我触摸的像素的颜色。
我尝试缩放 xml 文件中的图像,但是当我这样做时,虽然我看到图像适合 imageview,但 touchlistener 会以图像的原始尺寸工作。 如果我不缩放它,我只会看到适合 imageview 的图像部分,而 touchlistener 会得到实际的像素。
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath)
ivCamera.setImageBitmap(bitmap)
ivCamera.setOnTouchListener { view, motionEvent ->
val bmp = (ivCamera.drawable as BitmapDrawable).bitmap
val pixel = bmp.getPixel(motionEvent!!.x.toInt(), motionEvent.y.toInt())
pixelRed = Color.red(pixel)
pixelGreen = Color.green(pixel)
pixelBlue = Color.blue(pixel)
tvColor.setBackgroundColor(Color.rgb(pixelRed!!, pixelGreen!!, pixelBlue!!))
true
}
}
}
<ImageView android:layout_width="match_parent"
android:layout_weight="0.7"
android:layout_height="0dp"
android:id="@+id/ivCamera"
android:scaleType="matrix"
android:background="@android:drawable/ic_menu_report_image"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintHorizontal_bias="0.498"
android:layout_margin="10dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="true"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="PicureTaken"/>
如果我在 xml 文件中缩放图像,我会看到图像适合图像视图,但触摸监听器以图像的原始尺寸工作。 如果我不缩放它,我只会看到适合 imageview 的图像部分,而 touchlistener 会得到实际的像素。
【问题讨论】:
-
“缩放”是指代码:
scaleType="matrix"? -
好吧,很明显我不是“专业人士”,所以谷歌帮助我更接近我想做的是 scaleType。
-
嗯?我只是问一个问题以帮助理解,以便我可以正确回答
-
无意侮辱你,只是说明这种方法可能是完全错误的原因
-
不用担心,给出了答案,但它是一个奇怪的答案!不是我通常的知识领域
标签: android kotlin android-imageview dimensions onactivityresult