크기

2014-01-25 1 views
1

에 대한 @dimen 사용하는 캔버스를 설정하는 방법에는 다음과 같은 XML :크기

<LinearLayout 
     android:id="@+id/llColorSpect" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/color_scheme_height" 
     android:orientation="vertical" 
     android:background="@drawable/colorspect" 
     android:layout_marginRight="@dimen/activity_horizontal_margin" 
     android:layout_marginLeft="@dimen/activity_horizontal_margin" 
     android:layout_marginBottom="@dimen/seek_bar_margin" 
     android:layout_below="@+id/tvBGColor" > 
     <RelativeLayout 
      android:id="@+id/rlColorSpect" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
      <ImageView 
       android:id="@+id/ivSquare" 
       android:layout_width="@dimen/title_text_pad" 
       android:layout_height="@dimen/title_text_pad" 
       android:layout_alignParentBottom="true" 
       android:scaleType="fitXY" 
       android:src="@drawable/esquare" /> 
     </RelativeLayout> 
    </LinearLayout> 

다음과 같은 출력이 표시

enter image description here

내가 그래서 내가 할 수있는 설정에 캔버스 시도를 사용자가 레이아웃 주위로 작은 정사각형을 드래그 할 수있게하십시오 :

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 

public class CanvasView extends View { 

    private Bitmap bitmap; 
    private Bitmap square; 
    private float mScaleFactor = 1f; 
    int x = 0; 
    int y = 0; 

    public CanvasView(Context c) { 
     super(c); 
     bitmap= BitmapFactory.decodeResource(c.getResources(), R.drawable.colorspect); 
     square = BitmapFactory.decodeResource(c.getResources(), R.drawable.esquare); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     canvas.scale(mScaleFactor, mScaleFactor); 
     canvas.drawBitmap(bitmap, 0, 0, null); 
     canvas.drawBitmap(square, x, y, null); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     float x = event.getX(); 
     float y = event.getY(); 
     Log.d("x and y", "X: " + x + " Y: " + y); 
     int pixel = bitmap.getPixel((int)x,(int) y); 
     int redValue = Color.red(pixel); 
     int blueValue = Color.blue(pixel); 
     int greenValue = Color.green(pixel); 
     Log.d("Colors","R:" +redValue +" B:" + blueValue + " G:" +greenValue); 

     //Draw onto the square onto image 
     this.x = (int) x; 
     this.y = (int) y; 
     invalidate(); 

     return true; 
    } 
} 

나는 다음과 같이 내 주요 기능을 M :

CanvasView canvasView = new CanvasView(this); 
RelativeLayout rlDragDrop = (RelativeLayout) findViewById(R.id.rlColorSpect); 
rlDragDrop.addView(canvasView); 

을 대신 위의 그림에서와 같이 동일한 레이아웃을 유지의 레이아웃 변경 다음과 같이

enter image description here

  • 가 어떻게이 수정 보관하지 XML 파일/첫 번째 이미지를 반영하도록 코드에 의해 생성 된 두 번째 이미지를 만드는 코드?
  • 또한 사각형이 항상 상단 또는 좌측 또는 하단 또는 오른쪽으로 이동하여 앱 FC가 Y = 0 또는 X = 0을 말합니다. 어떻게 수정해야합니까?

내가 원하는 것은 사각형의 X 좌표와 Y 좌표를 가져 와서 R, G, B 값으로 변환하는 것입니다. (내가 겪고있는 두 가지 문제를 제외하고는 작동 중임)

답변

0

구현하려는 것은 색상 선택기처럼 보입니다. 나는 그 해결책을 많이 가지고 있다고 확신한다. Android Color Picker

코드에 관해서는 간단한 View의 ImageView insted를 직접 서브 클래 싱하는 것이 더 좋다. 그래서 ImageView는 당신을 위해 배경 이미지를 그릴 것입니다 (모든 xml 속성이 가능할 것입니다) 그리고 당신은 당신의 사각형에 대해서만 걱정할 것입니다.

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    canvas.drawBitmap(square, x, y, null); 
} 

두 번째 문제는 canvas.scale(mScaleFactor, mScaleFactor);의 accure 수 있지만, 난 당신이 비판적으로 스케일링을 필요로하는 경우 사실, Canvas#drawBitmap(Bitmap, Rect, RectF, Paint) 또는 Canvas.save()Canvas.restore()를 사용하는 것이 더 좋을 수도 있습니다 : 확실하지 않다.