2013-08-20 34 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