我有这段代码,所以我可以用手指在特定图像上绘图。现在我想在这个布局中添加一个布局和一些 Imageviews。我怎么能做到这一点?因为我想在布局中添加一些 onTouch 和 onClick 监听器来移动 ImageViews。另外,我可以添加现有的 xml 布局还是只动态创建新的布局?
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView view1 =new MyView(this);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
setContentView(view1);
}
public class MyView extends View {
private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public MyView(Context c) {
super(c);
setBackgroundResource(R.drawable.half_court);
// this.inflate(c, R.drawable.playera1, null);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
// canvas.drawLine(mX, mY, Mx1, My1, mPaint);
// canvas.drawLine(mX, mY, x, y, mPaint);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
// mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
// Mx1=(int) event.getX();
// My1= (int) event.getY();
invalidate();
break;
}
return true;
}
}
(编辑)当我尝试 Gunaseelan 的解决方案时,我会遇到以下错误:
04-09 13:05:08.842: D/dalvikvm(852): GC_FOR_ALLOC 释放 50K, 4% 释放 9056K/9347K, 暂停 89ms 04-09 13:05:08.892: I/dalvikvm-heap(852): 增长堆 ( frag case) 到 11.503MB 用于 2716016 字节分配 04-09 13:05:09.033: D/dalvikvm(852): GC_CONCURRENT freed <1K, 3% free 11708K/12039K, paused 9ms+19ms 04-09 13:05: 09.741: D/AndroidRuntime(852): 关闭 VM 04-09 13:05:09.751: W/dalvikvm(852): threadid=1: 线程退出未捕获异常 (group=0x409c01f8) 04-09 13:05:09.783 :E/AndroidRuntime(852):致命异常:主要 04-09 13:05:09.783:E/AndroidRuntime(852):java.lang.RuntimeException:无法启动活动 ComponentInfo{com.mitsos.thebasic/com.mitsos。 thebasic.main}:java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。reflect.Method.invokeNative(Native Method) 04-09 13:05:09.783: E/AndroidRuntime(852): at java.lang.reflect.Method.invoke(Method.java:511) 04-09 13:05:09.783 : E/AndroidRuntime(852): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 04-09 13:05:09.783: E/AndroidRuntime(852): 在 com.android。 internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-09 13:05:09.783: E/AndroidRuntime(852): at dalvik.system.NativeStart.main(Native Method) 04-09 13:05:09.783 :E / AndroidRuntime(852):引起:java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。04-09 13:05:09.783: E/AndroidRuntime(852): 在 android.view.ViewGroup.addViewInner(ViewGroup.java:3337) 04-09 13:05:09.783: E/AndroidRuntime(852): 在 android.看法。1049) 04-09 13:05:09.783: E/AndroidRuntime(852): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 04-09 13:05:09.783: E/AndroidRuntime(852): . .. 11 更多 04-09 13:05:21.712: I/Process(852): 发送信号。PID:852 SIG:9