Я пытаюсь добавить масштабированное изображение в классе адаптера в gridView.
Но высота и ширина iView почему-то увеличиваются до 0, когда измеряются для уменьшения изображения.
Вот код:
override fun getView(i: Int, convertView: View?, viewGroup: ViewGroup): View {
var cView = convertView
val (_, name, company, _, imagePath) = mSitesData[i]
if (cView == null) {
val layoutInflater = LayoutInflater.from(context)
cView = layoutInflater.inflate(R.layout.sites_grid_layout, null)
}
val iView = cView!!.findViewById(R.id.imageview_cover_art) as ImageView
val siteName = cView.findViewById(R.id.site_name) as TextView
val siteCompany = cView.findViewById(R.id.company_name) as TextView
if (imagePath.length<2){
iView.setImageResource(R.drawable.camera_item)
} else {
val targetW = iView.getWidth() ***// GIVING 0 AS RESULT***
val targetH = iView.getHeight()
// Get the dimensions of the bitmap
val bmOptions = BitmapFactory.Options() // Returns Option Object
bmOptions.inJustDecodeBounds = true // So it does not run out of memory if image is big
BitmapFactory.decodeFile(imagePath, bmOptions) // But can still query the size of the image
val photoW = bmOptions.outWidth
val photoH = bmOptions.outHeight
// Determine how much to scale down the image
val scaleFactor = Math.min(photoW / targetW, photoH / targetH)
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false
bmOptions.inSampleSize = scaleFactor
val bitmap = BitmapFactory.decodeFile(imagePath, bmOptions)
iView.setImageBitmap(bitmap)
}
siteName.text = name
siteCompany.text = company
return cView
}