我创建我的活动:
class ActivityMain : AppCompatActivity() {
private var count = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binder()
}
private fun binder() {
val button_addButton = findViewById<Button>(R.id.button_addButton)
val button_addCustomWidget = findViewById<Button>(R.id.button_addCustomWidget)
val layout = findViewById<LinearLayout>(R.id.testLayout)
val scrollView = findViewById<ScrollView>(R.id.testScroll)
val upButton = findViewById<Button>(R.id.upButton)
val downButton = findViewById<Button>(R.id.downButton)
upButton.setOnClickListener {
scrollView.fullScroll(ScrollView.FOCUS_UP)
}
downButton.setOnClickListener {
scrollView.fullScroll(ScrollView.FOCUS_DOWN)
}
button_addCustomWidget.setOnClickListener {
val widget = ChannelMessageWidget(this, null, 0)
widget.setLayoutParams(LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
))
widget.setAvatar(
BitmapFactory.decodeResource(getResources(),
R.drawable.default_avatar))
layout.addView(widget)
}
button_addButton.setOnClickListener {
count ++
val button = Button(this)
button.text = "Кнопка $count!"
button.setOnClickListener {
Toast.makeText(this, "Привет, $count", Toast.LENGTH_SHORT).show()
}
layout.addView(button)
}
}
}
所有小部件均已初始化并出现一个窗口。这是它的 XML:
<?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=".ActivityMain">
<Button
android:id="@+id/button_addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AB"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ScrollView
android:id="@+id/testScroll"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/button_addButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/testLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="UselessLeaf" />
</ScrollView>
<Button
android:id="@+id/upButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Up"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button_addCustomWidget"
app:layout_constraintStart_toEndOf="@+id/downButton" />
<Button
android:id="@+id/downButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Down"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button_addCustomWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ACW"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button_addButton"
app:layout_constraintStart_toEndOf="@+id/upButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
我还创建了一个自定义小部件(我正在尝试绘制圆形图片):
fun dp2pixel(dp: Float, context: Context): Float {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.resources.displayMetrics)
}
class ChannelMessageWidget @JvmOverloads constructor(
private val context: Context, attrs: AttributeSet? = null, defStyleAttr: Int
) : View(context, attrs, defStyleAttr) {
private var text: String = "null"
private lateinit var avatar: Bitmap
private var displayed = false
private val radius = 128f
// @SuppressLint("DrawAllocation")
// override fun onDraw(canvas: Canvas) {
// super.onDraw(canvas)
//
// val scaledBitmap = Bitmap.createScaledBitmap(avatar, dp2pixel(radius, context).toInt(), dp2pixel(radius, context).toInt(), true)
//
// val outputBitmap = Bitmap.createBitmap(scaledBitmap.width, scaledBitmap.height, Bitmap.Config.ARGB_8888)
//
// val paint = Paint().apply {
// isAntiAlias = true
// color = Color.BLACK
// style = Paint.Style.FILL
// }
//
// val radius = scaledBitmap.width / 2f
// canvas.drawCircle(radius, radius, radius, paint)
//
// paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
// canvas.drawBitmap(outputBitmap, 0f, 0f, paint)
//
// val rect = Rect(0f, 0f, scaledBitmap.width.toFloat(), scaledBitmap.height.toFloat())
//
// displayed = true
// }
// @SuppressLint("DrawAllocation")
// override fun onDraw(canvas: Canvas) {
// super.onDraw(canvas)
//
// val scaledBitmap = Bitmap.createScaledBitmap(avatar, dp2pixel(radius, context).toInt(), dp2pixel(radius, context).toInt(), true)
//
// val outputBitmap = Bitmap.createBitmap(scaledBitmap.width, scaledBitmap.height, Bitmap.Config.ARGB_8888)
// val outputCanvas = Canvas(outputBitmap) // Создаем Canvas для outputBitmap
//
// val paint = Paint().apply {
// isAntiAlias = true
// color = Color.BLACK
// style = Paint.Style.FILL
// }
//
// val radius = scaledBitmap.width / 2f
// outputCanvas.drawCircle(radius, radius, radius, paint) // Рисуем круг на outputCanvas
//
// paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
// outputCanvas.drawBitmap(scaledBitmap, 0f, 0f, paint) // Рисуем отмасштабированное изображение на outputCanvas
//
// canvas.drawBitmap(outputBitmap, 0f, 0f, null) // Рисуем outputBitmap на холсте canvas
// }
@SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val paint = Paint().apply {
isAntiAlias = true
color = Color.BLACK
style = Paint.Style.FILL
}
val rect = Rect(0, 0, 100, 50) // Прямоугольник с шириной экрана и высотой 50 пикселей
canvas.drawRect(rect, paint) // Отрисовка прямоугольника на холсте
}
fun setAvatar(avatar: Bitmap) {
this.avatar = avatar
}
fun setText(text: String) {
this.text = text
}
}
这些方法都不起作用。控制台不显示任何错误,我的自定义元素也没有添加到布局中。虽然按钮很容易添加、滚动等。
问题是由于硬件调用,小部件的尺寸没有明确指定。
setMeasuredDimension()
帮助了。