我有以下课程代码:
package com.example.tview;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.*;
public class TestView extends Activity {
float x = 0;
float y = 0;
LinearLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_view);
layout=(LinearLayout)findViewById(R.id.viewd);
layout.addView(new CustomView(TestView.this));
}
public class CustomView extends View {
Bitmap mBitmap;
Paint paint;
Path path;
public CustomView(Context context) {
super(context);
mBitmap = Bitmap.createBitmap(640, 1024, Bitmap.Config.ARGB_8888);
paint = new Paint();
path= new Path();
paint.setColor(Color.BLUE);
//paint.setStyle(Style.FILL); //if I want to fill but I don't
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(5);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path,paint);
canvas.drawCircle(x, y, 25, paint);
}
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
path.lineTo(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
path.lineTo(event.getX(), event.getY());
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;}
return true;
}}
}
我的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000" >
<LinearLayout
android:id="@+id/viewd"
android:layout_width="250dp"
android:layout_height="304dp"
android:background="#FFFFFF"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A"
android:textSize="125dp" />
</LinearLayout>
</LinearLayout>
它所做的是获取布局并允许我在其上进行绘制。
当我运行该应用程序时,我的屏幕上会显示以下内容,我可以在上面绘图:
当我在 Eclipse 中查看 XML 布局时,它显示:
如何将 A 显示保持在绘图画布后面?我试图让用户追踪字母。