Android 获取控件大小(Android版 Flutter LayoutBuilder)

只是提供思路,可能有bug

package com.example.myapplication

import android.content.Context
import android.util.AttributeSet
import android.view.ViewGroup
import java.lang.RuntimeException

typealias SizeCallBack = (width: Int, height: Int) -> Unit

class LayoutBuilder : ViewGroup {

    var sizeListener: SizeCallBack? = null

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes)
    
    fun removeSizeListener() {
        sizeListener = null
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        if (childCount > 1) {
            throw RuntimeException("LayoutBuilder的子组件最多只能有一个")
        }
        val height = b - t - paddingTop - paddingBottom
        val width = r - l - paddingLeft - paddingRight
        if (sizeListener != null) {
            sizeListener?.invoke(width, height)
        }
        if (childCount == 1) {
            getChildAt(0).layout(
                paddingLeft,
                paddingTop,
                paddingLeft + width,
                paddingTop + height
            )
        }
    }
}

Demo

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <com.example.myapplication.LayoutBuilder
        android:id="@+id/builder"
        android:padding="50dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </com.example.myapplication.LayoutBuilder>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplication.databinding.ActivityMainBinding
import com.squareup.picasso.Picasso


class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    private val handler = Handler(Looper.myLooper()!!)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

		binding.builder.sizeListener = { width, height ->
            Log.d("图片", "$width,$height")
            binding.builder.removeSizeListener()
        }

        Picasso.get()
           	.load("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbbsfiles.vivo.com.cn%2Fvivobbs%2Fattachment%2Fforum%2F201708%2F13%2F121110z9967752ll6b2652.jpg&refer=http%3A%2F%2Fbbsfiles.vivo.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1622369506&t=c228c2f4f64f5eb7e8344cd01e7b4c5f")
            .placeholder(ColorDrawable(Color.BLUE))
            .error(ColorDrawable(Color.RED))
            .into(binding.image2)
    }
}
2021-04-30 18:22:59.828 14446-14446/com.example.myapplication D/图片: 1090,2074
Android 获取控件大小(Android版 Flutter LayoutBuilder)
上一篇:Android TextView跑马灯效果与设置文字阴影


下一篇:Element Select 二次封装