我有这个代码,所以我可以用手指画一个特定的图像。现在我想在此布局中添加布局和一些图像视图。我怎么能这样做? 因为我想在布局中添加一些onTouch和onClick Listeners来移动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释放<1K,3%免费 11708K / 12039K,暂停9ms + 19ms 04-09 13:05:09.741: D / AndroidRuntime(852):关闭VM 04-09 13:05:09.751: W / dalvikvm(852):threadid = 1:线程退出未捕获的异常 (组= 0x409c01f8)04-09 13:05:09.783:E / AndroidRuntime(852):致命 EXCEPTION:主04-09 13:05:09.783:E / AndroidRuntime(852): java.lang.RuntimeException:无法启动活动 ComponentInfo {com.mitsos.thebasic / com.mitsos.thebasic.main}: java.lang.IllegalStateException:指定的子节点已经有了 家长。您必须首先在孩子的父母上调用removeView()。 04-09 13:05:09.783:E / AndroidRuntime(852):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 04-09 13:05:09.783:E / AndroidRuntime(852):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 04-09 13:05:09.783:E / AndroidRuntime(852):at android.app.ActivityThread.access $ 600(ActivityThread.java:123)04-09 13:05:09.783:E / AndroidRuntime(852):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147) 04-09 13:05:09.783:E / AndroidRuntime(852):at android.os.Handler.dispatchMessage(Handler.java:99)04-09 13:05:09.783:E / AndroidRuntime(852):at android.os.Looper.loop(Looper.java:137)04-09 13:05:09.783: E / AndroidRuntime(852):at android.app.ActivityThread.main(ActivityThread.java:4424)04-09 13:05:09.783:E / AndroidRuntime(852):at java.lang.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):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 04-09 13:05:09.783:E / AndroidRuntime(852):at 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:The 指定的孩子已经有父母。您必须调用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):at android.view.ViewGroup.addView(ViewGroup.java:3208)04-09 13:05:09.783:E / AndroidRuntime(852):at android.view.ViewGroup.addView(ViewGroup.java:3188)04-09 13:05:09.783:E / AndroidRuntime(852):at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270) 04-09 13:05:09.783:E / AndroidRuntime(852):at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:260) 04-09 13:05:09.783:E / AndroidRuntime(852):at android.app.Activity.setContentView(Activity.java:1855)04-09 13:05:09.783:E / AndroidRuntime(852):at com.mitsos.thebasic.main.onCreate(main.java:53)04-09 13:05:09.783: E / AndroidRuntime(852):at android.app.Activity.performCreate(Activity.java:4465)04-09 13:05:09.783:E / AndroidRuntime(852):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 04-09 13:05:09.783:E / AndroidRuntime(852):at 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
答案 0 :(得分:0)
试试这种方式
LinearLayout layout = new LinearLayout(this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.your_image);
layout.addView(iv);
layout.addView(view1);
setContentView(layout);
然后你的onCreate()
应该是
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView view1 =new MyView(this);
LinearLayout layout = new LinearLayout(this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.your_image);
layout.addView(iv);
layout.addView(view1);
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(layout);
}
我希望这会奏效。