自定义缩放缩放视图

时间:2012-10-02 20:58:18

标签: android xml zoom

我正在尝试在我正在处理的Android应用上实现Pinch To Zoom。为此,我使用Android开发人员的网站创建了一个名为PinchToZoom的Java类。但是,如果我尝试启动我的应用程序,我的XML文件中会出错。我很困惑为什么,因为我以为我已经正确实现了它。这是XML代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#222222"
android:orientation="vertical" >

<com.andriesse.athena.PinchToZoom
    android:id="@+id/selectedImage"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:adjustViewBounds="true"
    android:contentDescription="@string/content" />

</LinearLayout>

我希望你们能帮助我!

修改

我之前忘了添加错误日志,所以这里是:

10-03 13:35:40.207: E/AndroidRuntime(2421): FATAL EXCEPTION: main
10-03 13:35:40.207: E/AndroidRuntime(2421): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andriesse.athena/com.andriesse.athena.ImageEditing}: android.view.InflateException: Binary XML file line #8: Error inflating class com.andriesse.athena.PinchToZoom
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.os.Looper.loop(Looper.java:137)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.app.ActivityThread.main(ActivityThread.java:4441)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at java.lang.reflect.Method.invokeNative(Native Method)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at java.lang.reflect.Method.invoke(Method.java:511)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at dalvik.system.NativeStart.main(Native Method)
10-03 13:35:40.207: E/AndroidRuntime(2421): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.andriesse.athena.PinchToZoom
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.view.LayoutInflater.createView(LayoutInflater.java:589)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.app.Activity.setContentView(Activity.java:1835)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at com.andriesse.athena.ImageEditing.onCreate(ImageEditing.java:26)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.app.Activity.performCreate(Activity.java:4465)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
10-03 13:35:40.207: E/AndroidRuntime(2421):     ... 11 more
10-03 13:35:40.207: E/AndroidRuntime(2421): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
10-03 13:35:40.207: E/AndroidRuntime(2421):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at java.lang.Class.getConstructor(Class.java:431)
10-03 13:35:40.207: E/AndroidRuntime(2421):     at android.view.LayoutInflater.createView(LayoutInflater.java:561)
10-03 13:35:40.207: E/AndroidRuntime(2421):     ... 22 more

2 个答案:

答案 0 :(得分:2)

这是关键路线:

Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

确保自定义列表视图包含以下构造函数:

public PinchToZoom(Context context) {
    this(context, null);
}

public PinchToZoom(Context context, AttributeSet attrs) {   // <--- missing?
    this(context, attrs, 0);
}

public PinchToZoom(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    // initialisation code here
}

我已经用这种方式链接了它们,所以除了第3个构造函数之外你永远不需要做任何事情。如果您有这些构造函数,并且它们没有只能在运行时解析的外部依赖项,那么您的自定义列表视图也将正确预览。

答案 1 :(得分:0)

不是创建类型为com.andriesse.athena.PinchToZoom的xml节点,而是尝试创建一个类型视图并为其提供class =“com.andriesse.athena.PinchToZoom”的类属性,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#222222"
    android:orientation="vertical" >

    <view
        class="com.andriesse.athena.PinchToZoom"
        android:id="@+id/selectedImage"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:adjustViewBounds="true"
        android:contentDescription="@string/content" />

</LinearLayout>

编辑:另外,您可能不想编写自己的缩放代码,而是尝试使用此代码:https://github.com/jasonpolites/gesture-imageview

我在之前的项目中使用它,它对我很有用。