1

我正在尝试创建一个具有不同基本形状(例如圆形、矩形等)的“调色板”的应用程序,用户可以将它们的副本拖放到画布上。我的形状是在 xml 文件中定义的。目前我可以将它们移动到画布上,但随后它们会从调色板中消失(因为我正在移动原始视图),所以我需要一种方法来在用户单击时创建形状的新副本并且将其拖到画布上。我已经尝试寻找解决方案,并查看了 LayoutInflaters 但无法让它们工作。LayoutInflaters 是要走的路还是有更好的解决方案?请在下面查看我的代码片段。

谢谢

在我的 onDrag() 中:

    case DragEvent.ACTION_DROP:
    // Dropped, reassign View to ViewGroup
    View view = (View) event.getLocalState();
    ViewGroup owner = (ViewGroup) view.getParent();
    //owner.removeView(view);
    LinearLayout container = (LinearLayout) v;
    View copyview = getLayoutInflater().inflate(R.drawable.rectangle, null);
    container.addView(copyview);
    copyview.setVisibility(View.VISIBLE);
    break;

编辑:

矩形的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <stroke
        android:width="2dp"
        android:color="#FFFFFFFF" />

    <solid android:color="#ffff00ff" />

</shape> 

日志猫:

10-18 19:59:12.461: I/dalvikvm(830): threadid=3: reacting to signal 3
10-18 19:59:12.741: I/dalvikvm(830): Wrote stack traces to '/data/anr/traces.txt'
10-18 19:59:12.961: I/dalvikvm(830): threadid=3: reacting to signal 3
10-18 19:59:13.101: I/dalvikvm(830): Wrote stack traces to '/data/anr/traces.txt'
10-18 19:59:13.372: D/android.widget.GridLayout(830): vertical constraints: y1 - y0 > 160, y2 - y1 > 1038, y2 - y0 < 1038 are inconsistent; permanently removing: y2 - y0 < 1038. 
10-18 19:59:13.481: I/dalvikvm(830): threadid=3: reacting to signal 3
10-18 19:59:13.542: I/dalvikvm(830): Wrote stack traces to '/data/anr/traces.txt'
10-18 19:59:13.661: D/gralloc_goldfish(830): Emulator without GPU emulation detected.
10-18 19:59:15.531: D/AndroidRuntime(830): Shutting down VM
10-18 19:59:15.531: W/dalvikvm(830): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
10-18 19:59:15.631: E/AndroidRuntime(830): FATAL EXCEPTION: main
10-18 19:59:15.631: E/AndroidRuntime(830): android.view.InflateException: Binary XML file line #2: Error inflating class shape
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-18 19:59:15.631: E/AndroidRuntime(830):  at com.neonwizard.shapes.DragActivity$MyDragListener.onDrag(DragActivity.java:68)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.View.dispatchDragEvent(View.java:13461)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1155)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1141)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1141)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1141)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1141)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:3471)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2620)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.os.Looper.loop(Looper.java:137)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.app.ActivityThread.main(ActivityThread.java:4424)
10-18 19:59:15.631: E/AndroidRuntime(830):  at java.lang.reflect.Method.invokeNative(Native Method)
10-18 19:59:15.631: E/AndroidRuntime(830):  at java.lang.reflect.Method.invoke(Method.java:511)
10-18 19:59:15.631: E/AndroidRuntime(830):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-18 19:59:15.631: E/AndroidRuntime(830):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-18 19:59:15.631: E/AndroidRuntime(830):  at dalvik.system.NativeStart.main(Native Method)
10-18 19:59:15.631: E/AndroidRuntime(830): Caused by: java.lang.ClassNotFoundException: android.view.shape
10-18 19:59:15.631: E/AndroidRuntime(830):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
10-18 19:59:15.631: E/AndroidRuntime(830):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
10-18 19:59:15.631: E/AndroidRuntime(830):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.LayoutInflater.createView(LayoutInflater.java:552)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:636)
10-18 19:59:15.631: E/AndroidRuntime(830):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
10-18 19:59:15.631: E/AndroidRuntime(830):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
10-18 19:59:15.631: E/AndroidRuntime(830):  ... 20 more
10-18 19:59:16.251: I/dalvikvm(830): threadid=3: reacting to signal 3
10-18 19:59:16.282: I/dalvikvm(830): Wrote stack traces to '/data/anr/traces.txt'
4

0 回答 0