借鉴Image View android

时间:2012-11-17 19:23:19

标签: android android-canvas android-imageview

每次从另一个ImageView收到像素值(要绘制的圆圈的中心)时,是否有人可以帮我了解如何在Activity上绘制特定半径的圆?

1 个答案:

答案 0 :(得分:3)

扩展ImageView并覆盖onDraw(Canvas c)方法 - 添加c.drawCirle(x,y,radius,Paint)

或者对于绘制圆圈,您也可以使用纯画布。

我试过这种方式,但仍然没有运气

public class MyActivity extends Activity {
private MyImageView myImageView;
 float x, y;
 float [] loc;
float radius = 50;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_location);
    myImageView = (MyImageView) findViewById(R.id.mapimageView);
    myImageView.setImageResource(R.drawable.mymap);

    //Receiving location information(x/y pixels array) from myLocation Activity         
        Intent i = getIntent();     
        loc = i.getFloatArrayExtra("Location");

}
public class MyImageView extends ImageView
{

    public MyImageView(Context context) {
        super(context);         
    }
    // Constructor for inflating via XML
   public MyImageView(Context context, AttributeSet attrs) {
   super(context, attrs);         
    }


            @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint();
        x= loc[0];
        y=loc[1];
        p.setColor(Color.RED);
        p.setStrokeWidth(2);        
        canvas.drawCircle(x, y, radius, p);
    }       
}

在xml中将<ImageView更改为your.package.name.MyImageView

//编辑我只是从memmory写的,所以可能会出现拼写错误