2

我有以下课程代码:

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 显示保持在绘图画布后面?我试图让用户追踪字母。

4

1 回答 1

4

尝试在构造函数中将视图的背景设置为透明:

setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

正如上面其他人所说,您可能也希望使用框架布局,以便此视图位于 textview 之上。就个人而言,我会将 textview 从 xml 中取出,扩展它并将视图的功能放入其中,然后您只需要在自己的绘图方法之后调用 super.onDraw。

我很快把它放在一起,我已经在记事本中完成了,所以它可能需要一些工作:

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.removeAllViews();
        CustomView view = new CustomView(TestView.this);
        view.setText("A");
        view.setLayoutParams(new LinearLayout.LayoutParams(
            250,
            340));
        layout.addView(view);
    }

    public class CustomView extends TextView {
        Bitmap mBitmap;
        Paint paint;
        Path path;

        public CustomView(Context context) {
            super(context);
            paint = new Paint();
            path= new Path();
            paint.setColor(Color.BLUE);
            paint.setStyle(Style.STROKE);
            paint.setStrokeWidth(5);
        }

        protected void onDraw(Canvas canvas) {
            canvas.drawPath(path,paint);
            canvas.drawCircle(x, y, 25, paint);
            super.onDraw(canvas);
        }

        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;
        }
    }
}
于 2013-08-20T21:09:44.520 回答