gpt4 book ai didi

android - 触摸画圆

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:53:49 26 4
gpt4 key购买 nike

我想在用户触摸屏幕的地方画一个圆圈。

public class MainActivity extends Activity implements OnTouchListener {

LinearLayout layout;
float x=0;
float y=0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
layout=(LinearLayout)findViewById(R.id.layout);
layout.addView(new CustomView(MainActivity.this));
}

public class CustomView extends View {

Bitmap mBitmap;
Paint paint;
public CustomView(Context context) {
super(context);
mBitmap = Bitmap.createBitmap(400, 800, Bitmap.Config.ARGB_8888);
paint=new Paint();
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.setBitmap(mBitmap);
canvas.drawCircle(x, y, 50, paint);
Toast.makeText(MainActivity.this, "DDD", 1).show();
}
}

@Override
public boolean onTouch(View v, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {
layout.invalidate();

}
return false;
}}

最佳答案

我会这样做:

public class TestActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawingView(this));
}

class DrawingView extends SurfaceView {

private final SurfaceHolder surfaceHolder;
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

public DrawingView(Context context) {
super(context);
surfaceHolder = getHolder();
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
if (surfaceHolder.getSurface().isValid()) {
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.BLACK);
canvas.drawCircle(event.getX(), event.getY(), 50, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
return false;
}
}
}

关于android - 触摸画圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11796357/

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