Android: 在位图上画一个圆
Android: Drawing a circle on bitmap
我正在尝试制作一个在位图上绘制圆圈的应用程序。现在,我有一个生成圆圈的按钮。相反,我想绘制用户双击的圆圈(而不是按下按钮)。我如何以编程方式执行此操作?以下是目前activity的内容:
public static final String KEY_PATH = "img.jpg";
private ZoomInZoomOut touch;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_display);
Intent intent = getIntent();
String path = getIntent().getStringExtra(ImageDisplayActivity.KEY_PATH);
try {
java.io.FileInputStream in = this.openFileInput(path);
bitmap = BitmapFactory.decodeStream(in);
bitmap = bitmap.copy(bitmap.getConfig(), true);
touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);
touch = arrangeImageView(touch);
touch.setImageBitmap(bitmap);
in.close();
Button draw = (Button) findViewById(R.id.draw);
draw.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(),
bitmap.getConfig());
Canvas canvas = new Canvas(bmOverlay);
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(Color.BLUE);
p.setStrokeWidth(2);
p.setStyle(Paint.Style.STROKE);
canvas.drawBitmap(bitmap,new Matrix(),null);
canvas.drawCircle(1000, 1000, 20, p);
touch.setImageBitmap(bmOverlay);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
您必须实施 GestureDetector 并将您的代码放入 single/double click
。这里可以用位图替换按钮。
TestActivity.java
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//putyour first activity call.
}
}
iv.setOnTouchListener(new OnTouchListener() {
GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector(context));
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
现在您必须创建 GestureDetector.java
class.
public class MyGestureDetector extends SimpleOnGestureListener {
public Context context;
public String phno;
public MyGestureDetector(Context con)
{
this.context=con;
}
@Override
public boolean onDown(MotionEvent e) {
return super.onDown(e);
}
public MyGestureDetector(Context con) {
this.context=con;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
System.out.println("in Double tap");
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
System.out.println("in single tap up");
//put your second activity.
return super.onSingleTapUp(e);
}
}
我正在尝试制作一个在位图上绘制圆圈的应用程序。现在,我有一个生成圆圈的按钮。相反,我想绘制用户双击的圆圈(而不是按下按钮)。我如何以编程方式执行此操作?以下是目前activity的内容:
public static final String KEY_PATH = "img.jpg";
private ZoomInZoomOut touch;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_display);
Intent intent = getIntent();
String path = getIntent().getStringExtra(ImageDisplayActivity.KEY_PATH);
try {
java.io.FileInputStream in = this.openFileInput(path);
bitmap = BitmapFactory.decodeStream(in);
bitmap = bitmap.copy(bitmap.getConfig(), true);
touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);
touch = arrangeImageView(touch);
touch.setImageBitmap(bitmap);
in.close();
Button draw = (Button) findViewById(R.id.draw);
draw.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(),
bitmap.getConfig());
Canvas canvas = new Canvas(bmOverlay);
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(Color.BLUE);
p.setStrokeWidth(2);
p.setStyle(Paint.Style.STROKE);
canvas.drawBitmap(bitmap,new Matrix(),null);
canvas.drawCircle(1000, 1000, 20, p);
touch.setImageBitmap(bmOverlay);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
您必须实施 GestureDetector 并将您的代码放入 single/double click
。这里可以用位图替换按钮。
TestActivity.java
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//putyour first activity call.
}
}
iv.setOnTouchListener(new OnTouchListener() {
GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector(context));
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
现在您必须创建 GestureDetector.java
class.
public class MyGestureDetector extends SimpleOnGestureListener {
public Context context;
public String phno;
public MyGestureDetector(Context con)
{
this.context=con;
}
@Override
public boolean onDown(MotionEvent e) {
return super.onDown(e);
}
public MyGestureDetector(Context con) {
this.context=con;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
System.out.println("in Double tap");
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
System.out.println("in single tap up");
//put your second activity.
return super.onSingleTapUp(e);
}
}