Android ImageView中的图像消失
在Android ImageView中的图像消失,android,kotlin,dialog,Android,Kotlin,Dialog,在maintactivtiy中,我们使用Glide将URL图像加载到imgSignature 单击imgSignature时,会弹出一个自定义对话框,它将在imgSign中显示图像。我们的问题是,当我们单击自定义对话框中的“完成”按钮时,imgSignature中的图像变为空,并获取此toast消息bgDrawable null 为什么imgSignature中的图像会消失 lateinit var signDialog: Dialog override fun onVie
maintactivtiy
中,我们使用Glide
将URL图像加载到imgSignature
单击imgSignature
时,会弹出一个自定义对话框,它将在imgSign
中显示图像。我们的问题是,当我们单击自定义对话框中的“完成”按钮时,imgSignature
中的图像变为空,并获取此toast消息bgDrawable null
为什么imgSignature中的图像会消失
lateinit var signDialog: Dialog
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
signDialog = Util().dialogSignature(getActivity())
var mSignature = signature(activity, null)
signDialog.relativeLayout2.addView(mSignature)
var image: Bitmap? = null
if (obj?.signature_image?.url != null) {
Glide.with(activity)
.load(obj?.signature_image?.url.toString())
.into(imgSignature)
}
imgSignature.setOnClickListener {
signDialog.show()
if (obj?.signature_image?.url != " ") {
Glide.with(activity)
.load(obj?.signature_image?.url.toString())
.into(signDialog.imgSign);
}
}
signDialog.doneTxt.setOnClickListener {
signDialog.dismiss()
imgSignature.setImageBitmap(getBitmapFromView(mSignature))
}
}
fun getBitmapFromView(view: View): Bitmap {
//Define a bitmap with the same size as the view
val returnedBitmap = Bitmap.createBitmap(250, 250, Bitmap.Config.ARGB_8888)
//Bind a canvas to it
val canvas = Canvas(returnedBitmap)
//Get the view's background
val bgDrawable = view.background
if (bgDrawable != null) {
longToast("bgDrawable not null")
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas)
}
else {
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE)
// draw the view on the canvas
view.draw(canvas)
longToast("bgDrawable null")
}
//return the bitmap
return returnedBitmap
}
}
Util
fun dialogSignature(context: Context?):Dialog{
var dialog = Dialog(context)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setContentView(R.layout.dialog_signature)
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
return dialog
}
对话框\u签名
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="230dp"
android:orientation="vertical"
android:background="@android:color/white">
<LinearLayout android:layout_width="0dp" android:layout_height="wrap_content"
android:background="@color/colorPrimaryShadow"
android:orientation="horizontal"
android:id="@+id/linearLayout1"
android:gravity="center"
android:layout_marginBottom="2dp" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/relativeLayout2" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:layout_marginLeft="10dp"
android:layout_weight="0.4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Place Signature"
android:textSize="17sp"
android:layout_gravity="right"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:id="@+id/doneTxt"
android:text="Done"
android:textColor="@color/colorDarkBlue"/>
</LinearLayout>
<RelativeLayout android:layout_width="0dp" android:layout_height="0dp"
android:id="@+id/relativeLayout2"
android:background="@color/colorWhite"
app:layout_constraintTop_toBottomOf="@+id/linearLayout1" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0">
<ImageView android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/imgSign"/>
</RelativeLayout>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:textColor="@color/colorDarkBlue"
android:text="Clear" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" app:layout_constraintHorizontal_bias="1.0"
android:layout_marginBottom="16dp" app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/clearTxt"/>
</android.support.constraint.ConstraintLayout>
inner class signature(context: Context, attrs: AttributeSet?) : View(context, attrs) {
private val paint = Paint()
private val path = Path()
private var lastTouchX: Float = 0.toFloat()
private var lastTouchY: Float = 0.toFloat()
private val dirtyRect = RectF()
private val STROKE_WIDTH = 5f
private val HALF_STROKE_WIDTH = STROKE_WIDTH / 2
init {
paint.setAntiAlias(true)
paint.setColor(Color.BLACK)
paint.setStyle(Paint.Style.STROKE)
paint.setStrokeJoin(Paint.Join.ROUND)
paint.setStrokeWidth(STROKE_WIDTH)
}
override fun onDraw(canvas: Canvas) {
canvas.drawPath(path, paint)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
val eventX = event.x
val eventY = event.y
// mGetSign.setEnabled(true)
when (event.action) {
MotionEvent.ACTION_DOWN -> {
path.moveTo(eventX, eventY)
lastTouchX = eventX
lastTouchY = eventY
return true
}
MotionEvent.ACTION_MOVE,
MotionEvent.ACTION_UP -> {
resetDirtyRect(eventX, eventY)
val historySize = event.historySize
for (i in 0 until historySize) {
val historicalX = event.getHistoricalX(i)
val historicalY = event.getHistoricalY(i)
expandDirtyRect(historicalX, historicalY)
path.lineTo(historicalX, historicalY)
}
path.lineTo(eventX, eventY)
}
else -> {
debug("Ignored touch event: $event")
return false
}
}
invalidate(
(dirtyRect.left - HALF_STROKE_WIDTH).toInt(),
(dirtyRect.top - HALF_STROKE_WIDTH).toInt(),
(dirtyRect.right + HALF_STROKE_WIDTH).toInt(),
(dirtyRect.bottom + HALF_STROKE_WIDTH).toInt()
)
lastTouchX = eventX
lastTouchY = eventY
return true
}
private fun debug(string: String) {
// Log.v("log_tag", string)
}
private fun expandDirtyRect(historicalX: Float, historicalY: Float) {
if (historicalX < dirtyRect.left) {
dirtyRect.left = historicalX
} else if (historicalX > dirtyRect.right) {
dirtyRect.right = historicalX
}
if (historicalY < dirtyRect.top) {
dirtyRect.top = historicalY
} else if (historicalY > dirtyRect.bottom) {
dirtyRect.bottom = historicalY
}
}
private fun resetDirtyRect(eventX: Float, eventY: Float) {
dirtyRect.left = Math.min(lastTouchX, eventX)
dirtyRect.right = Math.max(lastTouchX, eventX)
dirtyRect.top = Math.min(lastTouchY, eventY)
dirtyRect.bottom = Math.max(lastTouchY, eventY)
}
}
在线
val bgDrawable = (view as ImageView).drawable
签名
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="230dp"
android:orientation="vertical"
android:background="@android:color/white">
<LinearLayout android:layout_width="0dp" android:layout_height="wrap_content"
android:background="@color/colorPrimaryShadow"
android:orientation="horizontal"
android:id="@+id/linearLayout1"
android:gravity="center"
android:layout_marginBottom="2dp" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/relativeLayout2" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:layout_marginLeft="10dp"
android:layout_weight="0.4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Place Signature"
android:textSize="17sp"
android:layout_gravity="right"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:id="@+id/doneTxt"
android:text="Done"
android:textColor="@color/colorDarkBlue"/>
</LinearLayout>
<RelativeLayout android:layout_width="0dp" android:layout_height="0dp"
android:id="@+id/relativeLayout2"
android:background="@color/colorWhite"
app:layout_constraintTop_toBottomOf="@+id/linearLayout1" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0">
<ImageView android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/imgSign"/>
</RelativeLayout>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:textColor="@color/colorDarkBlue"
android:text="Clear" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" app:layout_constraintHorizontal_bias="1.0"
android:layout_marginBottom="16dp" app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/clearTxt"/>
</android.support.constraint.ConstraintLayout>
inner class signature(context: Context, attrs: AttributeSet?) : View(context, attrs) {
private val paint = Paint()
private val path = Path()
private var lastTouchX: Float = 0.toFloat()
private var lastTouchY: Float = 0.toFloat()
private val dirtyRect = RectF()
private val STROKE_WIDTH = 5f
private val HALF_STROKE_WIDTH = STROKE_WIDTH / 2
init {
paint.setAntiAlias(true)
paint.setColor(Color.BLACK)
paint.setStyle(Paint.Style.STROKE)
paint.setStrokeJoin(Paint.Join.ROUND)
paint.setStrokeWidth(STROKE_WIDTH)
}
override fun onDraw(canvas: Canvas) {
canvas.drawPath(path, paint)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
val eventX = event.x
val eventY = event.y
// mGetSign.setEnabled(true)
when (event.action) {
MotionEvent.ACTION_DOWN -> {
path.moveTo(eventX, eventY)
lastTouchX = eventX
lastTouchY = eventY
return true
}
MotionEvent.ACTION_MOVE,
MotionEvent.ACTION_UP -> {
resetDirtyRect(eventX, eventY)
val historySize = event.historySize
for (i in 0 until historySize) {
val historicalX = event.getHistoricalX(i)
val historicalY = event.getHistoricalY(i)
expandDirtyRect(historicalX, historicalY)
path.lineTo(historicalX, historicalY)
}
path.lineTo(eventX, eventY)
}
else -> {
debug("Ignored touch event: $event")
return false
}
}
invalidate(
(dirtyRect.left - HALF_STROKE_WIDTH).toInt(),
(dirtyRect.top - HALF_STROKE_WIDTH).toInt(),
(dirtyRect.right + HALF_STROKE_WIDTH).toInt(),
(dirtyRect.bottom + HALF_STROKE_WIDTH).toInt()
)
lastTouchX = eventX
lastTouchY = eventY
return true
}
private fun debug(string: String) {
// Log.v("log_tag", string)
}
private fun expandDirtyRect(historicalX: Float, historicalY: Float) {
if (historicalX < dirtyRect.left) {
dirtyRect.left = historicalX
} else if (historicalX > dirtyRect.right) {
dirtyRect.right = historicalX
}
if (historicalY < dirtyRect.top) {
dirtyRect.top = historicalY
} else if (historicalY > dirtyRect.bottom) {
dirtyRect.bottom = historicalY
}
}
private fun resetDirtyRect(eventX: Float, eventY: Float) {
dirtyRect.left = Math.min(lastTouchX, eventX)
dirtyRect.right = Math.max(lastTouchX, eventX)
dirtyRect.top = Math.min(lastTouchY, eventY)
dirtyRect.bottom = Math.max(lastTouchY, eventY)
}
}
内部类签名(上下文:上下文,属性集?):视图(上下文,属性集){
私有val paint=paint()
私有值路径=路径()
私有变量lastTouchX:Float=0.toFloat()
私有变量lastTouchY:Float=0.toFloat()
private val dirtyRect=RectF()
私有值笔划_宽度=5f
私有值半笔划宽度=笔划宽度/2
初始化{
paint.setAntiAlias(真)
paint.setColor(颜色:黑色)
绘制.设置样式(绘制.样式.笔划)
绘制.设置行程连接(绘制.连接.圆形)
油漆.设置行程宽度(行程宽度)
}
覆盖onDraw(画布:画布){
canvas.drawPath(路径,绘制)
}
重写事件(事件:MotionEvent):布尔值{
val eventX=event.x
val eventY=event.y
//mGetSign.setEnabled(真)
何时(事件、动作){
MotionEvent.ACTION\u向下->{
path.moveTo(eventX,eventY)
lastTouchX=eventX
最后时刻
返回真值
}
MotionEvent.ACTION\u移动,
MotionEvent.ACTION\u UP->{
resetDirtyRect(eventX,eventY)
val historySize=event.historySize
对于(i在0中,直到historySize){
val historicalX=event.getHistoricalX(i)
val historicali=event.gethistoricali(i)
expandDirtyRect(历史学、历史学)
path.lineTo(历史的,历史的)
}
path.lineTo(eventX,eventY)
}
其他->{
调试(“忽略的触摸事件:$event”)
返回错误
}
}
使无效(
(dirtyRect.left-半冲程宽度).toInt(),
(dirtyRect.top-HALF_-STROKE_-WIDTH).toInt(),
(直接右+半笔划宽度).toInt(),
(dirtyRect.bottom+HALF_STROKE_WIDTH).toInt()
)
lastTouchX=eventX
最后时刻
返回真值
}
私有趣味调试(字符串:字符串){
//Log.v(“日志标签”,字符串)
}
private fun expandDirtyRect(historicalX:Float,historicalY:Float){
if(历史XdirtyRect.right){
dirtyRect.right=historicalX
}
if(历史目录底部){
dirtyRect.bottom=historicali
}
}
私人娱乐重置目录(eventX:Float,eventY:Float){
dirtyRect.left=Math.min(lastTouchX,eventX)
dirtyRect.right=Math.max(lastTouchX,eventX)
dirtyRect.top=Math.min(lastTouchY,eventY)
dirtyRect.bottom=Math.max(lastTouchY,eventY)
}
}
首先,您的mSignature不包含任何图像,因此当您尝试从中检索图像(或背景)时,返回null。使用imgSignature删除mSignature应该可以工作。但我仍然不确定为什么需要签名类
lateinit var signDialog: Dialog
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
signDialog = Util().dialogSignature(getActivity())
//var mSignature = signature(activity, null)
//signDialog.relativeLayout2.addView(mSignature)
var image: Bitmap? = null
if (obj?.signature_image?.url != null) {
Glide.with(activity)
.load(obj?.signature_image?.url.toString())
.into(imgSignature)
}
imgSignature.setOnClickListener {
signDialog.show()
if (obj?.signature_image?.url != " ") {
Glide.with(activity)
.load(obj?.signature_image?.url.toString())
.into(signDialog.imgSign);
}
}
signDialog.doneTxt.setOnClickListener {
signDialog.dismiss()
// code change goes here imgSignature
imgSignature.setImageBitmap(getBitmapFromView(imgSignature))
}
}
您使用的是view.background而不是view.drawable,因为glide使用src属性设置了Imageview。你必须为可拉深的部分做些挫折。请在下面查找更改
fun getBitmapFromView(view: View): Bitmap {
//Define a bitmap with the same size as the view
val returnedBitmap = Bitmap.createBitmap(250, 250, Bitmap.Config.ARGB_8888)
//Bind a canvas to it
val canvas = Canvas(returnedBitmap)
//Get the Imageview's src drawable
val bgDrawable = (view as ImageView).drawable
if (bgDrawable != null) {
bgDrawable.setbounds(10,10,240,240); // setting bounds with padding of 10
longToast("bgDrawable not null")
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas)
}
else {
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE)
// draw the view on the canvas
view.draw(canvas)
longToast("bgDrawable null")
}
//return the bitmap
return returnedBitmap
}
}
新年快乐:)无法转换为android.widget.ImageView mSignature变量包含什么?这个方法签名是什么(活动,空)?编辑了答案。请检查@hoomSignature变量包含什么?此方法签名是什么(活动,null)@不要使用getBitmapFromView。在第一次使用glilde加载时,使用
Glide.with(activity).load(obj?.signature\u image?.url.toString()).diskCacheStrategy(diskCacheStrategy.ALL).into(signDialog.imgSign)启用图像缓存代码>只需第二次使用Glide在imageview中加载图像,它将从缓存中加载。第二次在这里阅读更多关于如何从缓存加载的信息?更新了答案@Hoo