在存在的位图图像上绘制正方形

时间:2015-05-04 13:34:22

标签: android

我有一个位图图像,我想在此图像上绘制一个方形特殊位置,我研究但是我找不到任何解决方案,所有解决方案都是在空视图上创建Square,请指教我是初学者

public class DrawView extends View
{
    Paint paint = new Paint();

    public DrawView(Context context)
    {
        super(context);
    }


    @Override
    public void onDraw(Canvas canvas)
    {


        Paint myPaint = new Paint();
        myPaint.setColor(Color.WHITE);
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(2);
        canvas.drawRect(10, 10, 100, 100, myPaint);


    }

}

我应该将我的位图图像转换为convas?how?

1 个答案:

答案 0 :(得分:1)

试试这个,它会做你想要的,还有更多:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class DrawView extends View {
    Paint paint = new Paint();

    public DrawView(Context context) {
        super(context);            
    } 

    @Override 
    public void onDraw(Canvas canvas) {
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3);
        canvas.drawRect(30, 30, 80, 80, paint);
        paint.setStrokeWidth(0);
        paint.setColor(Color.CYAN);
        canvas.drawRect(33, 60, 77, 77, paint );
        paint.setColor(Color.YELLOW);
        canvas.drawRect(33, 33, 77, 60, paint );

    } 

} 

绘制它:     import android.app.Activity;     import android.graphics.Color;     import android.os.Bundle;

public class StartDraw extends Activity {
    DrawView drawView;

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

        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);

    } 
} 

来源:Android canvas draw rectangle