我使用UIL将我的apps目录中存储的图像加载到imageView中。我想要做的是根据用户的触摸在屏幕上的图像上绘制一个小圆圈。所以我决定使用表面视图。我可以根据onTouch
事件绘制圆圈,但我面临的问题是整个屏幕都有白色背景。我想要在图像视图上绘制圆圈。
public class SingleViewActivity extends Activity {
int position;
String[] imageUrls;
ImageView imageView;
ImageLoader imageloader;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.single_view);
File Path = getExternalFilesDir(null);
Constants cons = new Constants();
imageUrls = cons.getImagePath(Path.toString());
// Get intent data
Intent i = getIntent();
// Selected image id
position = i.getExtras().getInt("position", -1);
imageView = (ImageView) findViewById(R.id.SingleView);
imageloader = ImageLoader.getInstance();
imageloader.displayImage("file://" + imageUrls[position], imageView);
setContentView(new DrawingView(this));
}
class DrawingView extends SurfaceView {
private final SurfaceHolder surfaceHolder;
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public DrawingView(Context context) {
super(context);
setZOrderOnTop(true);
surfaceHolder = getHolder();
surfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
paint.setColor(Color.RED);
paint.setStyle(android.graphics.Paint.Style.FILL);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (surfaceHolder.getSurface().isValid()) {
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawCircle(event.getX(), event.getY(), 50, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
return false;
}
}
}