2013-08-20 31 views
1

我有下面的类代码:上绘制一个TextView

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> 

它在做什么越来越布局,并允许我在它上面绘制。

当我运行应用程序,以下是显示我的屏幕上,我可以在上面画: enter image description here

当我查看Eclipse中的XML布局,它让我看到: enter image description here

如何我是否将A显示保留在绘图画布后面?我试图让用户追踪字母。

+1

你“viewd”应该是一个FrameLayout里,这样增加新的视图栈它在Z方向的文本之上。不知道这是唯一的问题。 – Tenfour04

+0

试着用'FrameLayout'更换您的ID/viewd'LinearLayout'在XML和明显的代码。 –

+0

aah评论后,我看到@ TenFour04的评论。 –

回答

3

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

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

正如有人说上面你可能要被使用的FrameLayout也因此,这个观点是对的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; 
     } 
    } 
} 
+0

谢谢。现在测试它...... **更新**:没有A没有被显示。 – Si8

+0

对不起,我认为该视图正在扩展textview。有一个去设置背景可绘制透明:this.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); –

+0

这是一个贬值的方法? – Si8