1

我有一个扩展 ImageView 的自定义类

    public class MyCompass extends ImageView 
    {
         private float direction;
// Rest Code

    }
      <ImageView class="myPakagename.MyCompass"
                android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/immg"
                android:src="@drawable/hub"/>

我试图在我的片段中将其称为

   myCompass = (MyCompass) view.findViewById(R.id.immg);

然而,这给了我classCastException。如何让我的类扩展 ImageView?

@SuppressLint("DrawAllocation")
public class MyCompass extends ImageView 
{
     private float direction;

     public MyCompass(Context context) 
     {
      super(context);
      setImageResource(R.drawable.qibla_compass_hub);
     }

     public MyCompass(Context context, AttributeSet attrs) {
      super(context, attrs);
      this.setImageResource(R.drawable.qibla_compass_hub);        
      }

     public MyCompass(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      this.setImageResource(R.drawable.qibla_compass_hub);                                
      }

     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      setMeasuredDimension(
        MeasureSpec.getSize(widthMeasureSpec),
        MeasureSpec.getSize(heightMeasureSpec));
     }

     @Override
     protected void onDraw(Canvas canvas) {
      int w = getMeasuredWidth();
      int h = getMeasuredHeight();
      canvas.rotate(direction, w / 2, h / 2);

     }

     public void update(float dir){
      direction = dir;
      invalidate();
     }

我的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/qiblaBg"
    android:background="@drawable/qibla_bg">    

    <RelativeLayout 
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/qibla_compass_outer">

          <mypakage.MyCompass
              android:contentDescription="@string/qibla"
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/qiblaImg"

// android:src=@"img" 不起作用,但 android:background 起作用,但它再次拉伸到全屏。/>

                            <RelativeLayout 
                                android:id="@+id/qiblaArrow"
                                android:layout_centerInParent="true"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:orientation="vertical" 
                                android:background="@drawable/qibla_compass_arrow" />      
    </RelativeLayout>


</RelativeLayout>

使用这个,我的 MyCompass ImageView 在布局中延伸到全屏并且不显示任何图像。

4

1 回答 1

1

您可以直接使用以下方法在 xml 中引用您的自定义图像视图:

<myPakagename.MyCompass
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/immg"
            android:src="@drawable/hub"/>

并确保您的自定义视图覆盖:

public MyCompass(Context context) {}

public MyCompass(Context context, AttributeSet attrs) {}

public MyCompass(Context context, AttributeSet attrs, int defStyle) { }
于 2013-01-30T07:58:17.277 回答