에 대한 @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>
다음과 같은 출력이 표시
내가 그래서 내가 할 수있는 설정에 캔버스 시도를 사용자가 레이아웃 주위로 작은 정사각형을 드래그 할 수있게하십시오 :
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);
을 대신 위의 그림에서와 같이 동일한 레이아웃을 유지의 레이아웃 변경 다음과 같이
- 가 어떻게이 수정 보관하지 XML 파일/첫 번째 이미지를 반영하도록 코드에 의해 생성 된 두 번째 이미지를 만드는 코드?
- 또한 사각형이 항상 상단 또는 좌측 또는 하단 또는 오른쪽으로 이동하여 앱 FC가 Y = 0 또는 X = 0을 말합니다. 어떻게 수정해야합니까?
내가 원하는 것은 사각형의 X 좌표와 Y 좌표를 가져 와서 R, G, B 값으로 변환하는 것입니다. (내가 겪고있는 두 가지 문제를 제외하고는 작동 중임)