如何使用自定义的ImageView [英] how to use custom ImageView

查看:172
本文介绍了如何使用自定义的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了如何让显示器的高度和宽度从ImageView的以下code:<一href=\"http://stackoverflow.com/questions/3855218/trying-to-get-the-display-size-of-an-image-in-an-imageview\">Trying获得的图像的显示尺寸在ImageView的的。当我尝试下面的代码段布局使用它,我得到以下错误。

I have found the following code on how to get display height and width from ImageView: Trying to get the display size of an image in an ImageView . When I try to use it in the layout snippet below, I get following error.

 <com.mycompany.widget.SizeAwareImageView
                android:id="@+id/account_profile_img"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@drawable/avatar"
                android:clickable="true"
                android:gravity="bottom|center_horizontal"
                android:onClick="onEditPhotoClicked" />

ERROR:

FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.myapp/com.company.myapp.RegisterActivity}: android.view.InflateException: Binary XML file line #218: Error inflating class com.company.myapp.widget.SizeAwareImageView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
    at android.app.ActivityThread.access$700(ActivityThread.java:152)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5328)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #218: Error inflating class com.company.myapp.widget.SizeAwareImageView
    at android.view.LayoutInflater.createView(LayoutInflater.java:602)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
    at android.app.Activity.setContentView(Activity.java:1928)
    at com.company.myapp.RegisterActivity.onCreate(RegisterActivity.java:63)
    at android.app.Activity.performCreate(Activity.java:5250)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
    ... 11 more
 Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
    at java.lang.Class.getConstructorOrMethod(Class.java:460)
    at java.lang.Class.getConstructor(Class.java:431)
    at android.view.LayoutInflater.createView(LayoutInflater.java:567)
    ... 25 more

更新

从图形布局我也得到了错误

From the graphics Layout I also get the error

java.lang.NullPointerException
Exception details are logged in Window > Show View > Error Log

java.lang.NullPointerException
    at com.company.myapp.widget.SizeAwareImageView.onMeasure(SizeAwareImageView.java:66)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15848)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
    at android.view.View.measure(View.java:15848)
    at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1217)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.widget.ScrollView.onMeasure(ScrollView.java:321)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15848)

第二次修订

问题的误码线的第二个

final Drawable d = getDrawable();
final int origW = d.getIntrinsicWidth();
final int origH = d.getIntrinsicHeight();

完整code是这里堆栈溢出的<一个href=\"http://stackoverflow.com/questions/3855218/trying-to-get-the-display-size-of-an-image-in-an-imageview\">Trying获得的图像的显示尺寸在ImageView的的。这是得票最多的反应。

The complete code is here on stack overflow at Trying to get the display size of an image in an ImageView . It's the response with the most votes.

推荐答案

请确保您提供正确的构造函数XML。您的自定义视图的构造函数应该有以下方法签名:

Make sure you have provided the right constructor for the XML. Your custom Views constructor should have the following method signature:

public SizeAwareImageView(Context context, AttributeSet attrs){
  // todo
}

我猜它目前看起来是这样的:

I'm guessing it currently looks like this:

public SizeAwareImageView(Context context){
  // todo
}

另外,我会仔细检查你的意思是设置XML参数的android:背景,为ImageView的你居然设置的android:SRC

Also I'd double check you meant to set the XML parameter android:background, for ImageView you actually set android:src.

这篇关于如何使用自定义的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆