每当从另一个ImageView
接收到像素值(要绘制的圆的中心)时,有人能帮助我在Activity
上绘制一个特定半径的圆吗?
最佳答案
extendImageView
和overrideonDraw(Canvas c)
方法-addc.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
//edit im只从memmory写入,所以可能会有拼写错误