子类自定义 ImageView 的问题

Problems sub-classing a custom ImageView

我正在编写一个应用程序,我需要在其中创建自定义 ImageView(以覆盖其 onDraw)。在几周未能做到这一点后,我创建了一个最小的、可重现的示例来捕获问题。这是代码:

package com.nzeldes.mytestapp;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.AttributeSet;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    CustomImageView myImageView;
    Bitmap myBitmap;
 
    class CustomImageView extends androidx.appcompat.widget.AppCompatImageView {

        public CustomImageView(@NonNull Context context) {
            super(context);
        }

        public CustomImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }

        public CustomImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int displaywidth = Resources.getSystem().getDisplayMetrics().widthPixels;
        myBitmap = Bitmap.createBitmap(displaywidth, displaywidth, Bitmap.Config.ARGB_8888);
        myBitmap.eraseColor(Color.BLACK);
        myImageView = findViewById(R.id.imageView);  // Get a reference to the ImageView
        myImageView.setImageBitmap(myBitmap);                    // Associate the bitmap to the ImageView

    }
}

和 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=".MainActivity">
<ImageView
    android:id="@+id/imageView"
    android:layout_width="403dp"
    android:layout_height="366dp"
    android:layout_marginTop="72dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_launcher_background" />
</androidx.constraintlayout.widget.ConstraintLayout>*

但是,在源 window 中,我在 myImageView = findViewById(R.id.imageView) 上收到“意外隐式转换为 CustomImageView:布局标记为 ImageView”的警告; 如果我尝试 运行 应用程序,它会因来自 Logcat:

的错误而崩溃
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to com.nzeldes.mytestapp.MainActivity$CustomImageView

我承认我是 Android 的新手(把锁定时间用于学习新东西!)...任何帮助将不胜感激。

对于内部 类 语法变为:

<view class="{package}.{ParentClass}${innerClass}"

请按照下面的代码更改您的布局

<?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">
    <view class="com.nzeldes.mytestapp.MainActivity$CustomImageView"
        android:id="@+id/imageView"
        android:layout_width="403dp"
        android:layout_height="366dp"
        android:layout_marginTop="72dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_background" />
</androidx.constraintlayout.widget.ConstraintLayout>