Java 在片段中集成自定义imageView类
我有一个扩展ImageView的自定义类Java 在片段中集成自定义imageView类,java,android,eclipse,Java,Android,Eclipse,我有一个扩展ImageView的自定义类 public class MyCompass extends ImageView { private float direction; // Rest Code } <ImageView class="myPakagename.MyCompass" android:layout_centerInParent="true" and
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"/>
然而,这给了我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"
您可以直接使用以下方法在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"/>
及
及
您可以直接使用以下方法在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"/>
及
及
这会产生非常奇怪的结果,图像不会显示在layoutsit中,而是会尝试进行包装内容填充父对象,这就是问题所在在自定义图像视图上尝试在Measure&onDraw上对方法进行注释(使用原始方法)为了确保注释没有问题,设置它们,但是我需要这些方法来旋转图像,我应该怎么做?这会产生非常奇怪的结果,图像不会显示在布局中,但它会尝试做包装内容,而不是填充父对象,这是问题在自定义图像视图前尝试在测量和onDraw(使用原始图像)以确保它们没有错误注释设置它们,但是我需要这些方法来旋转图像我应该怎么做?您可以发布完整的stacktrace以及自定义ImageView代码吗。基于此,我们可以进一步帮助您..请同时粘贴您的logcat输出,显示错误详细信息当我使用时,您可以发布完整的stacktrace以及您的自定义ImageView代码。基于此,我们可以进一步帮助您..当我使用此工具时,请将显示错误详细信息的logcat输出粘贴给您
<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) { }