gpt4 book ai didi

android - 如何将 Canvas 保留在布局内?

转载 作者:行者123 更新时间:2023-11-30 03:58:04 27 4
gpt4 key购买 nike

我的代码:

public class MainActivity extends GraphicsActivity{    

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));

mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xffffffff);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);

mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
0.4f, 6, 3.5f);
mPaint.setMaskFilter(mEmboss);

//mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
}

private Paint mPaint;
private MaskFilter mEmboss;

public class MyView extends View {

private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;

public MyView(Context c) {
super(c);

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.t01);
Display display = getWindowManager().getDefaultDisplay();
int height = display.getWidth();
int width = display.getHeight();
mBitmap = Bitmap.createScaledBitmap(bmp, height, width, true);
//mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}

@Override
protected void onDraw(Canvas canvas) {

canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

canvas.drawPath(mPath, mPaint);
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}

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

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}

}

我的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/t01"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />

</RelativeLayout>

现在我想将 Activity 中的 View 添加到此 xml,并且我想将此 View 放置在布局上,以便必须看到图像和 View 。实际上,我正在尝试在图像中的字母线条内绘制。就像追踪这封信一样。而且我也不希望我的 Canvas 绘图排除图像中我的字母的轮廓。如果它穿过,那么我想发出一些警告声音。 enter image description here请帮我怎么做?提前致谢。

最佳答案

当我尝试 MarvinLabs 给出的答案时,我得到 Inflate Exception。所以尝试使用java本身编写代码。我的代码对我有用:

public class MainActivity extends GraphicsActivity{

private Button clearButton;
MyView signature;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signature = new MyView(this);

RelativeLayout myLayout = new RelativeLayout(this);
myLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

clearButton = new Button(this);
clearButton.setText("Clear");
clearButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// To clear the drawn lines with finger.
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});

myLayout.addView(signature);
myLayout.addView(clearButton);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
clearButton.setLayoutParams(params);

clearButton.bringToFront();
this.setContentView(myLayout);

mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xffffffff);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);

mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
0.4f, 6, 3.5f);
mPaint.setMaskFilter(mEmboss);

}

private Paint mPaint;
private MaskFilter mEmboss;


public class MyView extends View {

private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;

private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;

public MyView(Context c) {
super(c);

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.t01);
Display display = getWindowManager().getDefaultDisplay();
int height = display.getWidth();
int width = display.getHeight();
mBitmap = Bitmap.createScaledBitmap(bmp, height, width, true);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}


@Override
protected void onDraw(Canvas canvas) {

canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

canvas.drawPath(mPath, mPaint);
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}

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

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
}

感谢您的支持和帮助。

关于android - 如何将 Canvas 保留在布局内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13028977/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com