0

私はクロップ領域を定義するために私のアプリケーションで使用されるクロップビューのようなカスタムビューを作成しました。しかし、ビューを膨張させながらアプリがクラッシュします。私はsetColorを正しく使う方法を理解していないかもしれません。私は使用しようとしました:BoxPaint.setColor(AppShared.gResources.getColor(R.color.bwff_60))、アプリはまだクラッシュしました。私がコードを見て、私がその見方を見ることができるようにすべきすべての変更を教えてください。カスタムビューで正しく色を設定する方法

Logcat: enter image description hereenter image description here

main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:custom="http://schemas.android.com/apk/com.md.areadectest" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <FrameLayout 
     android:id="@+id/layoutMain" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
     <RelativeLayout android:layout_width="@dimen/width_320" android:layout_height="@dimen/width_240" android:layout_centerInParent="true"> 
     <SurfaceView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/preview"> 
     </SurfaceView> 
      <FrameLayout android:layout_gravity="center" android:id="@+id/layoutDetectArea" android:background="@color/blue" android:layout_width="match_parent" android:layout_height="match_parent"> 
       <com.md.areadectest.AreaDetectorView android:id="@+id/viewDetector" android:layout_width="match_parent" android:layout_height="match_parent" /> 
      </FrameLayout> 
     </RelativeLayout> 
    </FrameLayout> 

    <LinearLayout 
     android:layout_below="@id/layoutMain" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:gravity="center"> 
    </LinearLayout> 

    <ImageView android:id="@+id/myImageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_android_black_24dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/trigcount" 
     android:layout_alignTop="@+id/myImageView" 
     android:layout_centerHorizontal="true" 
     android:textColor="@android:color/white" /> 

</RelativeLayout> 

View.java

public class AreaDetectorView extends LinearLayout { 

    public static int Width; 
    public static int Height; 
    private static Paint BoxPaint = null; 
    private static Paint BoxPaint2 = null; 
    private static Paint TextPaint = null; 
    private static Paint ArrowPaint = null; 
    private static Path mPath = null; 
    private static Rect mRect = null; 
    private static int lastX, lastY = 0; 
    private static boolean mBoxTouched = false; 
    private static boolean mArrowTouched = false; 
    private static Context mContext; 
    private static int ArrowWidth = 0; 

    public AreaDetectorView(Context context) { 
     super(context); 
    } 
//attrs was not there 
    public AreaDetectorView(Context context, AttributeSet attrs) { 
     super(context,attrs); 
     // TODO Auto-generated constructor stub 
     if (!this.getRootView().isInEditMode()) { 
      ArrowWidth =GetDisplayPixel(context, 30); 
     } 

     //InitDetectionArea(); 

     InitMemberVariables(); 
     setWillNotDraw(false); 
    } 
    public static int GetDisplayPixel(Context paramContext, int paramInt) 
    { 
     return (int)(paramInt * paramContext.getResources().getDisplayMetrics().density + 0.5F); 
    } 

    public static void InitMemberVariables() { 
     if (BoxPaint == null) { 
      BoxPaint = new Paint(); 
      BoxPaint.setAntiAlias(true); 
      BoxPaint.setStrokeWidth(2.0f); 
      //BoxPaint.setStyle(Style.STROKE); 
      BoxPaint.setStyle(Style.FILL_AND_STROKE); 
      BoxPaint.setColor(ContextCompat.getColor(mContext, R.color.bwff_60)); 
     } 
     if (ArrowPaint == null) { 
      ArrowPaint = new Paint(); 
      ArrowPaint.setAntiAlias(true); 
      ArrowPaint.setColor(ContextCompat.getColor(mContext,R.color.redDD)); 
      ArrowPaint.setStyle(Style.FILL_AND_STROKE); 
     } 
     if (TextPaint == null) { 
      TextPaint = new Paint(); 
      TextPaint.setColor(ContextCompat.getColor(mContext,R.color.yellowL)); 
      TextPaint.setTextSize(16); 
      //txtPaint.setTypeface(lcd); 
      TextPaint.setStyle(Style.FILL_AND_STROKE); 
     } 
     if (mPath == null) { 
      mPath = new Path(); 
     } else { 
      mPath.reset(); 
     } 
     if (mRect == null) { 
      mRect = new Rect(); 
     } 

     if (BoxPaint2 == null) { 
      BoxPaint2 = new Paint(); 
      BoxPaint2.setAntiAlias(true); 
      BoxPaint2.setStrokeWidth(2.0f); 
      //BoxPaint.setStyle(Style.STROKE); 
      BoxPaint2.setStyle(Style.STROKE); 
      BoxPaint2.setColor(ContextCompat.getColor(mContext,R.color.bwff_9e)); 
     } 

    } 

    public static void InitDetectionArea() { 
     try { 
      int w = Preferences.DetectionArea.width(); 
      int h = Preferences.DetectionArea.height(); 
      int x = Preferences.DetectionArea.left; 
      int y = Preferences.DetectionArea.top; 

      // ver 2.6.0 
      if (Preferences.DetectionArea.left == 1 
        && Preferences.DetectionArea.top == 1 
        && Preferences.DetectionArea.right == 1 
        && Preferences.DetectionArea.bottom == 1) { 

       w = Preferences.DisplayWidth/4; 
       h = Preferences.DisplayHeight/3; 

       // ver 2.5.9 
       w = Width/4; 
       h = Height/3; 

       Preferences.DetectorWidth = w;  
       Preferences.DetectorHeight = h; 

       x = (Preferences.DisplayWidth/2) - (w/2); 
       y = (Preferences.DisplayHeight/2) - (h/2); 

       // ver 2.5.9 
       x = (Width/2) - (w/2); 
       y = (Height/2) - (h/2); 

      } 

      //Preferences.DetectionArea = new Rect(x, x, x + Preferences.DetectorWidth, x + Preferences.DetectorHeight); 
      Preferences.DetectionArea = new Rect(x, y, x + w, y + h); 

      Preferences.gDetectionBitmapInt = new int[Preferences.DetectionArea.width() * Preferences.DetectionArea.height()]; 
      Preferences.gDetectionBitmapIntPrev = new int[Preferences.DetectionArea.width() * Preferences.DetectionArea.height()]; 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void SetDetectionArea(int x, int y, int w, int h) { 
     try { 
      Preferences.DetectionArea = new Rect(x, y, w, h); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private void DrawAreaBox(Canvas canvas) { 
     try { 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void dispatchDraw(Canvas canvas) { 
     try { 
      if (this.getRootView().isInEditMode()) { 
       super.dispatchDraw(canvas); 
       return; 
      } 

      //Preferences.DetectionAreaOrient = UtilGeneralHelper.GetDetectRectByOrientation(); 

      canvas.drawColor(0); 
      mPath.reset(); 

      canvas.drawRect(Preferences.DetectionArea, BoxPaint); 

      mPath.moveTo(Preferences.DetectionArea.right - ArrowWidth, Preferences.DetectionArea.bottom); 
      mPath.lineTo(Preferences.DetectionArea.right, Preferences.DetectionArea.bottom - ArrowWidth); 
      mPath.lineTo(Preferences.DetectionArea.right, Preferences.DetectionArea.bottom); 
      mPath.lineTo(Preferences.DetectionArea.right - ArrowWidth, Preferences.DetectionArea.bottom); 
      mPath.close(); 
      canvas.drawPath(mPath, ArrowPaint); 

      mPath.reset(); 
      //canvas.drawRect(Preferences.DetectionAreaOrient, BoxPaint2); 
      //canvas.drawRect(Preferences.DetectionAreaOrientPort, BoxPaint2); 

      TextPaint.setTextSize(16); 
      TextPaint.setColor(ContextCompat.getColor(mContext,R.color.bwff)); 

      TextPaint.getTextBounds(getResources().getString(R.string.str_detectarea), 0, 1, mRect); 
      canvas.drawText(getResources().getString(R.string.str_detectarea), 
        Preferences.DetectionArea.left + 4, 
        Preferences.DetectionArea.top + 4 + mRect.height(), 
        TextPaint); 
      int recH = mRect.height(); 

      TextPaint.setStrokeWidth(1.2f); 
      TextPaint.setTextSize(18); 
      TextPaint.setColor(ContextCompat.getColor(mContext,R.color.redD_9e)); 
      TextPaint.getTextBounds(getResources().getString(R.string.str_dragandmove), 0, 1, mRect); 
      canvas.drawText(getResources().getString(R.string.str_dragandmove), 
        Preferences.DetectionArea.left + 4, 
        Preferences.DetectionArea.top + 20 + mRect.height()*2, 
        TextPaint); 

      TextPaint.getTextBounds(getResources().getString(R.string.str_scalearea), 0, 1, mRect); 
      canvas.drawText(getResources().getString(R.string.str_scalearea), 
        Preferences.DetectionArea.left + 4, 
        Preferences.DetectionArea.top + 36 + mRect.height()*3, 
        TextPaint); 

      super.dispatchDraw(canvas); 
      //canvas.restore(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     try { 
      super.onDraw(canvas); 
      invalidate(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 

     } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     boolean retValue = true; 
     int X = (int)event.getX(); 
     int Y = (int)event.getY(); 

     //AppMain.txtLoc.setText(String.valueOf(X) + ", " + String.valueOf(Y)); 

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       mBoxTouched = TouchedInBoxArea(X, Y); 

       //AppMain.txtLoc.setText("BoxTouched: " + String.valueOf(mBoxTouched)); 

       if (!mBoxTouched) break; 

       lastX = X; 
       lastY = Y; 

       BoxPaint.setStyle(Style.FILL_AND_STROKE); 
       BoxPaint.setColor(ContextCompat.getColor(mContext,R.color.redD_9e)); 

       mArrowTouched = TouchedInArrow(X, Y); 
       //AppMain.txtLoc.setText("ArrowTouched: " + String.valueOf(mBoxTouched)); 

       if (mArrowTouched) { 
        ArrowPaint.setColor(ContextCompat.getColor(mContext,R.color.bwff_9e)); 
       } 

       break; 

      case MotionEvent.ACTION_MOVE: 
       if (!mBoxTouched) break; 

       int moveX = X - lastX; 
       int moveY = Y - lastY; 

       //AppMain.txtLoc.setText("Move X, Y: " + String.valueOf(moveX) + "," + String.valueOf(moveY)); 
       if (!mArrowTouched) { 
        if (Preferences.DetectionArea.left + moveX < 0) { 
         break; 
        } 
//    if (Preferences.DetectionArea.right + moveX > Preferences.gDisplay.getWidth()) { 
//     break; 
//    } 
        // ver 2.5.9 
        if (Preferences.DetectionArea.right + moveX > Width) { 
         break; 
        } 
        if (Preferences.DetectionArea.top + moveY < 0) { 
         break; 
        } 
//    if (Preferences.DetectionArea.bottom + moveY > Preferences.gDisplay.getHeight()) { 
//     break; 
//    } 
        // ver 2.5.9 
        if (Preferences.DetectionArea.bottom + moveY > Height) { 
         break; 
        } 
       } 

       if (mArrowTouched) { 
        if ((Preferences.DetectionArea.width() + moveX) < ArrowWidth * 2){ 
         break; 
        } 
        if ((Preferences.DetectionArea.height() + moveY) < ArrowWidth * 2) { 
         break; 
        } 
        Preferences.DetectionArea.right += moveX; 
        Preferences.DetectionArea.bottom += moveY; 
        //Log.i("DBG", "W,H: " + String.valueOf(Preferences.DetectionArea.width()) + "," + String.valueOf(Preferences.DetectionArea.height())); 
       } else { 
        Preferences.DetectionArea.left += moveX; 
        Preferences.DetectionArea.right += moveX; 
        Preferences.DetectionArea.top += moveY; 
        Preferences.DetectionArea.bottom += moveY; 
       } 

       lastX = X; 
       lastY = Y; 

       //AppMain.txtLoc.setText(String.valueOf(Preferences.DetectionArea.left) + ", " + String.valueOf(Preferences.DetectionArea.top)); 
       break; 

      case MotionEvent.ACTION_UP: 
       mBoxTouched = false; 
       mArrowTouched = false; 
       //BoxPaint.setStyle(Style.STROKE); 
       BoxPaint.setStyle(Style.FILL_AND_STROKE); 
       BoxPaint.setColor(ContextCompat.getColor(mContext,R.color.bwff_60)); 
       ArrowPaint.setColor(ContextCompat.getColor(mContext,R.color.redDD)); 
       //AppMain.txtLoc.setText(String.valueOf(Preferences.DetectionArea.left) + ", " + String.valueOf(Preferences.DetectionArea.top)); 

       if (Preferences.DetectionArea.left < 0) { 
        Preferences.DetectionArea.left = 0; 
       } 
//   if (Preferences.DetectionArea.right > Preferences.gDisplay.getWidth()) { 
//    Preferences.DetectionArea.right = Preferences.gDisplay.getWidth(); 
//   } 
       // ver 2.5.9 
       if (Preferences.DetectionArea.right > Width) { 
        Preferences.DetectionArea.right = Width; 
       } 
       if (Preferences.DetectionArea.top < 0) { 
        Preferences.DetectionArea.top = 0; 
       } 
//   if (Preferences.DetectionArea.bottom > Preferences.gDisplay.getHeight()) { 
//    Preferences.DetectionArea.bottom = Preferences.gDisplay.getHeight(); 
//   } 
       if (Preferences.DetectionArea.bottom > Height) { 
        Preferences.DetectionArea.bottom = Height; 
       } 

       Preferences.gDetectionBitmapInt = new int[Preferences.DetectionArea.width() * Preferences.DetectionArea.height()]; 
       Preferences.gDetectionBitmapIntPrev = new int[Preferences.DetectionArea.width() * Preferences.DetectionArea.height()]; 
       //Preferences.gDetectionBitmapInt = null; 
       //Preferences.gDetectionBitmapIntPrev = null; 

       String area = String.valueOf(Preferences.DetectionArea.left) 
         + "," + String.valueOf(Preferences.DetectionArea.top) 
         + "," + String.valueOf(Preferences.DetectionArea.right) 
         + "," + String.valueOf(Preferences.DetectionArea.bottom); 

       // UtilGeneralHelper.SavePreferenceSetting(Preferences.gContext, Preferences.PREF_DETECTION_AREA_KEY, area); 

       break; 
     } 


     invalidate(); 
     return retValue; 
    } 

    private boolean TouchedInBoxArea(int x, int y) { 
     boolean retValue = false; 
     try { 

      if (x > Preferences.DetectionArea.left && x < Preferences.DetectionArea.right) { 
       if (y > Preferences.DetectionArea.top && y < Preferences.DetectionArea.bottom) { 
        retValue = true; 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return retValue; 
    } 

    private boolean TouchedInArrow(int x, int y) { 
     boolean retValue = false; 
     try { 

      if (x > Preferences.DetectionArea.right - ArrowWidth && x < Preferences.DetectionArea.right) { 
       if (y > Preferences.DetectionArea.bottom - ArrowWidth && y < Preferences.DetectionArea.bottom) { 
        retValue = true; 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return retValue; 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     int width = MeasureSpec.getSize(widthMeasureSpec); 
     int height = MeasureSpec.getSize(heightMeasureSpec); 
     setMeasuredDimension(width, height); 
     Width = width; 
     Height = height; 
     InitDetectionArea(); 
    } 

    @Override 
    protected void onFinishInflate() { 
     super.onFinishInflate(); 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     // TODO Auto-generated method stub 
     for (int i = 0; i < this.getChildCount()-1; i++){ 
      (this.getChildAt(i)).layout(l, t, r, b); 
     } 

     if (changed) { 
      // check width height 
      if (r != Width || b != Height) { 
       // size does not match 
      } 
     } 
    } 
} 
+1

何かが、私はカタリーナの答えはクラッシュを固定@anddevmanuあなたはAreaDetectorView – anddevmanu

+0

に間違っているので、あなたのxmlファイルを提供しています。 –

+1

を更新したあなたのxml – anddevmanu

答えて

2

problあなたの変数mContextはnullです - コンストラクタにmContext = contextを設定する必要があります。

+0

これはクラッシュを修正しました。しかし、私が望んでいたように見えていない。私は動き検出アプリケーションを作成しようとしています。このビューは、検出領域を定義するためにサーフェイスビューの上に展開されます。今私が見ることができるのは、このカスタムビューです。 –

+2

@DioptrePicあなたはそれがどのように見えるかについてのより詳細な情報を提供できますか?その後、問題を簡単に検索することができます。 – Katharina

0

はこれを試してください:あなたは あなたのプロジェクト\アプリ\ SRC \メイン\ RES \値\ colors.xmlファイルに追加する場合にのみ

BoxPaint.setColor(getResources().getColor(R.color.bwff_60)); 

色が適用されます。

関連する問題