使用Canvas在Android上绘图

3

目前,我只希望画布绘制一种颜色。

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);
    }
}

上述代码是我目前为止所写的,但显然它不起作用。我怀疑Bitmap与我所做的事情没有联系,但我不知道该如何修复它。我该怎么做?


你到底想做什么?“不知何故没有连接”。你是什么意思?除非你在某个地方显示位图,否则你当然看不到它。 - Simon
画布没有显示,整个屏幕都是黑色的(我在我的S2上用于调试)。我该如何显示画布? - Zacharias Hortén
1
你还没有解释你想要做什么。你不能“显示”一个画布。画布只是一个位图绘制调用的容器。除非你在 UI 的位图画布上绘制,否则你将看不到任何东西。 - Simon
我想让屏幕变成蓝色。那么如何将位图部分加入我的用户界面? - Zacharias Hortén
你想使用画布的原因是什么?我认为你需要从一些“Hello World”示例开始。将屏幕变成蓝色非常简单,就像上学的第一天一样。创建一个XML布局并将根视图的背景颜色设置为蓝色。然后在onCreate()中使用setContentView来将XML填充到你的UI中。http://developer.android.com/guide/topics/ui/declaring-layout.html - Simon
显示剩余2条评论
2个回答

1

在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;
    }
}

0
LinearLayout ll = (LinearLayout) findViewById(R.id.rect);
Paint paint = new Paint();
paint.setColor(Color.parseColor("#CD5C5C"));
Bitmap bg = Bitmap.createBitmap(ll.getWidth(),ll.getHeight() , Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bg);
canvas.drawARGB(200, 0, 225, 255);
ll.setBackgroundDrawable(new BitmapDrawable(bg));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接