定制小部件集成

时间:2013-02-19 16:43:09

标签: android android-view custom-widgets

我创建了一个我想要使用的视图,就像在我的布局中使用自定义小部件一样。但一开始我有这个错误,我可以过去。所以请帮忙!

这是我的主要布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <com.example.SignatureView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

这是我的SignatureView类:

public class SignatureView extends View {

      private static final float STROKE_WIDTH = 5f;

      /** Need to track this so the dirty region can accommodate the stroke. **/
      private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;

      private Paint paint = new Paint();
      private Path path = new Path();

      /**
       * Optimizes painting by invalidating the smallest possible area.
       */
      private float lastTouchX;
      private float lastTouchY;
      private final RectF dirtyRect = new RectF();

      public SignatureView(Context context, AttributeSet attrs, int background) {
        super(context, attrs);
        setBackgroundResource(background);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(STROKE_WIDTH);

      }

      public void setColor(int color){
          paint.setColor(color);
      }

      /**
       * Erases the signature.
       */
      public void clear() {
        path.reset();

        // Repaints the entire view.
        invalidate();
      }

      @Override
      protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);
      }

      @Override
      public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            lastTouchX = eventX;
            lastTouchY = eventY;
            // There is no end point yet, so don't waste cycles invalidating.
            return true;

          case MotionEvent.ACTION_MOVE:
          case MotionEvent.ACTION_UP:
            // Start tracking the dirty region.
            resetDirtyRect(eventX, eventY);

            // When the hardware tracks events faster than they are delivered, the
            // event will contain a history of those skipped points.
            int historySize = event.getHistorySize();
            for (int i = 0; i < historySize; i++) {
              float historicalX = event.getHistoricalX(i);
              float historicalY = event.getHistoricalY(i);
              expandDirtyRect(historicalX, historicalY);
              path.lineTo(historicalX, historicalY);
            }

            // After replaying history, connect the line to the touch point.
            path.lineTo(eventX, eventY);
            break;

          default:
//          Log.("Ignored touch event: " + event.toString());
            return false;
        }

        // Include half the stroke width to avoid clipping.
        invalidate(
            (int) (dirtyRect.left - HALF_STROKE_WIDTH),
            (int) (dirtyRect.top - HALF_STROKE_WIDTH),
            (int) (dirtyRect.right + HALF_STROKE_WIDTH),
            (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));

        lastTouchX = eventX;
        lastTouchY = eventY;

        return true;
      }

      /**
       * Called when replaying history to ensure the dirty region includes all
       * points.
       */
      private void expandDirtyRect(float historicalX, float historicalY) {
        if (historicalX < dirtyRect.left) {
          dirtyRect.left = historicalX;
        } else if (historicalX > dirtyRect.right) {
          dirtyRect.right = historicalX;
        }
        if (historicalY < dirtyRect.top) {
          dirtyRect.top = historicalY;
        } else if (historicalY > dirtyRect.bottom) {
          dirtyRect.bottom = historicalY;
        }
      }

      /**
       * Resets the dirty region when the motion event occurs.
       */
      private void resetDirtyRect(float eventX, float eventY) {

        // The lastTouchX and lastTouchY were set when the ACTION_DOWN
        // motion event occurred.
        dirtyRect.left = Math.min(lastTouchX, eventX);
        dirtyRect.right = Math.max(lastTouchX, eventX);
        dirtyRect.top = Math.min(lastTouchY, eventY);
        dirtyRect.bottom = Math.max(lastTouchY, eventY);
      }
    }

这就是我在Draw主课程中的内容:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        }

最后这就是我在logCat中得到的东西

02-19 17:41:51.708: E/AndroidRuntime(7530): FATAL EXCEPTION: main
02-19 17:41:51.708: E/AndroidRuntime(7530): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.Draw}: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.SignatureView
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1662)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1678)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.ActivityThread.access$1500(ActivityThread.java:118)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:932)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.os.Looper.loop(Looper.java:130)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.ActivityThread.main(ActivityThread.java:3698)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at java.lang.reflect.Method.invokeNative(Native Method)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at java.lang.reflect.Method.invoke(Method.java:507)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at dalvik.system.NativeStart.main(Native Method)
02-19 17:41:51.708: E/AndroidRuntime(7530): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.SignatureView
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.createView(LayoutInflater.java:508)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.Activity.setContentView(Activity.java:1657)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at com.example.Draw.onCreate(Draw.java:13)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1626)
02-19 17:41:51.708: E/AndroidRuntime(7530):     ... 11 more
02-19 17:41:51.708: E/AndroidRuntime(7530): Caused by: java.lang.NoSuchMethodException: SignatureView(Context,AttributeSet)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at java.lang.Class.getMatchingConstructor(Class.java:643)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at java.lang.Class.getConstructor(Class.java:472)
02-19 17:41:51.708: E/AndroidRuntime(7530):     at android.view.LayoutInflater.createView(LayoutInflater.java:480)
02-19 17:41:51.708: E/AndroidRuntime(7530):     ... 21 more

3 个答案:

答案 0 :(得分:4)

错误非常自我解释:

Caused by: java.lang.NoSuchMethodException: SignatureView(Context,AttributeSet)

您没有只接受Context和AttributeSet的Contructor。将该构造函数添加到SignatureView类中,如下所示:

public SignatureView(Context c, AttributeSet as){
    super(c, as);
}

答案 1 :(得分:1)

您需要一个接受Context和AttributeSet的构造函数。您为自己添加了第三个参数。通过xml进行充气时,这不起作用,它需要精确的签名才能工作。

此外,它会使事情更加混乱,因为如果应用默认样式,则xml inflater会查找3参数版本(第三个是主题的资源ID),这会错误地调用当前的3参数方法。最终的结果是你试图设置一个随机主题id的backgroudn资源,我不知道结果会是什么,但我的猜测是崩溃。

答案 2 :(得分:1)

02-19 17:41:51.708: E/AndroidRuntime(7530): Caused by: java.lang.NoSuchMethodException: ***SignatureView(Context,AttributeSet)***

看起来你错过了一个构造函数?尝试为SignatureView(Context,AttributeSet)添加构造函数,看看它是如何工作的。

而不是在构造函数中传递int background,尝试将其作为xml属性传递。这样你仍然可以回复默认的构造函数,它可以让你灵活地从xml中设置背景。