ImageView в адаптере для Android convertView измеряется как 0 ширина и 0 высота - PullRequest
0 голосов
/ 24 июня 2018

Я пытаюсь добавить масштабированное изображение в классе адаптера в 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
    }

Ответы [ 2 ]

0 голосов
/ 24 июня 2018

Попробуйте сделать это

 imageView.measure(0, 0);
        imageView.getMeasuredHeight();
        imageView.getMeasuredWidth();
0 голосов
/ 24 июня 2018

Ширина и высота дочернего элемента не известны, пока представление элемента не будет создано в getView () и привязано к listview и requestLayout ().Это естественный процесс с точки зрения процесса компоновки, и он, вероятно, получит значения ширины и высоты со времени перезапуска сгенерированного представления.Может быть способ переопределить onLayout в ImageView для обработки его при первом вызове.

...