2010-07-26 39 views
7

我是Android的新人,但不是Java。我一直在通過XML文件在android中設計UI,在那個頁面中我有3個線性佈局,在我的頂層佈局(第一個LinearLayout)中,我保留了一些圖像,並且在最後一個佈局中我保留了一些按鈕,現在我需要放置一個在我的中心佈局使用畫布圈(紅色),我已經寫了一個單獨的類,擴展視圖在onDraw(畫布畫布),我畫了一個圓圈。如何在特定的LinearLayout中添加畫布?

 
package com.project.TargetTrackr3; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.view.View; 

public class DrawCanvasCircle extends View{ 
    public DrawCanvasCircle(Context mContext) { 
     super(mContext); 
    } 
    public void onDraw(Canvas canvas) { 
     Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.FILL); 
     canvas.drawColor(Color.WHITE); 
     paint.setColor(Color.BLUE); 
     canvas.drawCircle(20, 20, 15, paint); 
    } 

} 

現在我不得不把這個畫布第二佈局,我的main.xml如下所示

 
package com.project.TargetTrackr3; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.ColorFilter; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.LinearLayout; 
public class TargetTrackr3Activity extends Activity { 
    /** Called when the activity is first created. */ 
     protected LinearLayout ll; 
     DrawCanvasCircle c; 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main1); //layouting file 
     ll = (LinearLayout) findViewById(R.id.LinearLayout_DrawCircle);//This is where i have to bring the canvas 
     c = new DrawCanvasCircle(this); 
      ................................... 
      ................................ 



    } 
} 

回答

7

這裏是我做過什麼,包括你的看法。

開始增加一個新的佈局,以你的XML文件,然後你可以檢索,然後你可以添加到它:

DrawCanvasCircle pcc = new DrawCanvasCircle (this); 
    Bitmap result = Bitmap.createBitmap(25, 25, Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(result); 
    pcc.draw(canvas); 
    pcc.setLayoutParams(new LayoutParams(25, 25)); 
    mControls.addView(pcc); 

在這個例子中mControls是添加到佈局主要活動佈局。

+0

您好,請問您如何將它置於mControls中間? – user3290180 2015-08-18 08:48:43

相關問題