gpt4 book ai didi

Android:如何动态地在屏幕的一部分上制作 Canvas ?

转载 作者:行者123 更新时间:2023-11-29 02:29:58 25 4
gpt4 key购买 nike

在 android 中,当用户执行操作时,如何使可绘制 Canvas 仅出现在屏幕的一部分?

我的最终目标是在布局中动态插入一个栏,当用户触摸它时,将在栏上绘制特定文本。

我搜索过,但只看到将 Canvas 作为布局本身处理的方法;而不是作为可以添加到 Activity 中的 View 。我的理解是,要使其仅成为屏幕的一部分,我必须以某种方式将其作为可以添加到布局的 View 来处理。我该怎么做?

最佳答案

要创建 Canvas ,请创建自定义 View ,如下所示:

public class CanvasView extends View {

public int width;
public int height;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
Context context;
private Paint mPaint;
private float mX, mY;
private static final float TOLERANCE = 5;

public CanvasView(Context c, AttributeSet attrs) {
super(c, attrs);
context = c;

// we set a new Path
mPath = new Path();

// and we set a new Paint with the desired attributes
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeWidth(4f);
}

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

// your Canvas will draw onto the defined Bitmap
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}

// override onDraw
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// draw the mPath with the mPaint on the canvas when onDraw
canvas.drawPath(mPath, mPaint);
}

// when ACTION_DOWN start touch according to the x,y values
private void startTouch(float x, float y) {
mPath.moveTo(x, y);
mX = x;
mY = y;
}

// when ACTION_MOVE move touch according to the x,y values
private void moveTouch(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOLERANCE || dy >= TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}

public void clearCanvas() {
mPath.reset();
invalidate();
}

// when ACTION_UP stop touch
private void upTouch() {
mPath.lineTo(mX, mY);
}

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

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startTouch(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
moveTouch(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
upTouch();
invalidate();
break;
}
return true;
}
}

之后将这个 canvasView 用于 xml 文件:

<com.example.canvasdemo.CanvasView
android:id="@+id/signature_canvas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/colorWhite" />

最后进入你的主要 Activity :

public class MainActivity extends AppCompatActivity {

Button btnClear;
private CanvasView customCanvas;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

customCanvas = (CanvasView) findViewById(R.id.signature_canvas);
btnClear = (Button) findViewById(R.id.button1);


btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customCanvas.clearCanvas(); // to clear canvas
}
});


}

example picture

关于Android:如何动态地在屏幕的一部分上制作 Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50072869/

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