0

2D グラフィックスは初めてです。 drawText メソッドを使用してテキストを作成します。drawtext 領域をペイントする必要があります。テキストの領域をクリップしてサーフェスをペイントするにはどうすればよいですか

package com.example.testingcanvas;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MainActivity extends Activity {

class MyCustomView extends View {
    private float x = 0, y = 0;

    Path path = new Path();
    Paint paint = new Paint();
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ii);

    private Rect m_ImageRect;
    private Rect m_TextRect;
    Context m_Context;

    // you need these constructor
    // you can init paint object or anything on them
    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        m_Context = context;

    }

    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        m_Context = context;

    }

    public MyCustomView(Context context) {
        super(context);
        m_Context = context;

    }

    // then override on draw method
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setAntiAlias(true);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(3);
        paint.setTextSize(100);
        // here frist create two rectangle
        // one for your image and two for text you want draw on it
        m_ImageRect = canvas.getClipBounds();
        m_TextRect = canvas.getClipBounds();
        // it gives you an area that can draw on it,
        // the width and height of your rect depend on your screen size
        // device
        canvas.save();
        canvas.drawBitmap(bm, null, m_ImageRect, paint);

        canvas.drawPath(path, paint);
        canvas.restore();
        canvas.save();
        canvas.clipRect(m_TextRect);

        canvas.drawText("A", 100, 300, paint);

        // canvas.drawText("A", 20, 20,50,50, paint);

        // canvas.restore();
    }

    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_MOVE:

            x = event.getX();
            y = event.getY();
            path.lineTo(x, y);

            break;
        case MotionEvent.ACTION_DOWN:
            x = event.getX();
            y = event.getY();
            path.moveTo(x, y);

            break;
        case MotionEvent.ACTION_UP: {
        }
        default:

        }

        invalidate();
        return true;
    }
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new MyCustomView(this));

}

public void onBackPressed() {
    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);
    finish();
    onDestroy();

}
}

テキスト領域に描画しようとしていますが、取得できません。

4

0 に答える 0