gpt4 book ai didi

android - 用 canvas 画画 Android

转载 作者:太空狗 更新时间:2023-10-29 14:23:55 26 4
gpt4 key购买 nike

现在我希望 Canvas 只绘制一种颜色。

public class AndroidTentaTestActivity extends Activity {

private Bitmap bm;
private Canvas c;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);

bm = Bitmap.createBitmap(100, 100, Config.ARGB_8888);
c = new Canvas(bm);
c.drawARGB(100, 0, 0, 150);
}
}

上面的代码是我到目前为止所写的代码,但显然行不通。我怀疑位图不知何故与我正在做的事情没有联系,但我不知道如何修复它。我该怎么办?

最佳答案

在 mPaint.setColor() 中更改所需的颜色。

public class AndroidTentaTestActivity extends AppCompatActivity {

MyView mv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


mv = new MyView(this);
mv.setDrawingCacheEnabled(true);
setContentView(mv);

mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
}

private Paint mPaint;

@Override
public void onClick(View v) {

}

public class MyView extends View {

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

public MyView(Context c) {
super(c);
context = c;
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);

}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);

}


@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(0xFFFFFFFF);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}

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

private void touch_start(float x, float y) {
//showDialog();
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); //2,2.old values

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 画画 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14112998/

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