2011-11-14 99 views
3

我創建一個類:如何將視圖擴展類的畫布用於活動類?

public class TestCanvas extends View { 

    public TestCanvas(Context context) { 
     super(context); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     Paint paint = new Paint(); 
     paint.setColor(Color.RED); 

     canvas.drawText("kal", 0, 100, paint); 
     canvas.save(); 
    } 
} 

現在我調用類從活動:

public class TestActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TestCanvas tcanvas=new TestCanvas(); 

     frameLayout=(FrameLayout)findViewById(R.id.frameLayout1); 
     frameLayout.addView(tcanvas); 
    } 
} 

現在我想畫布到活動類,並設置爲ImageView的。我將如何做到這一點?

+0

誰做一些成'ImageView'元素的解決方案嗎?或者你只是想在'R.id.frameLayout1'中顯示畫布? –

+0

我想將畫布設置爲一個Imageview ... – Kalpesh

+0

您是否只想在特定視圖內的屏幕上繪製?因爲它不一定是一個ImageView,它可以是任何自定義視圖。爲什麼你需要它成爲ImageView?這有什麼特別的要求嗎? –

回答

5

您需要從View繼承自己的班級,並覆蓋onDraw()onMeasure()。就像你開始使用TestCanvas一樣。舉個例子:

package com.yourapphere; 

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

public class TwoDee extends View { 
    private int mWidth; 
    private int mHeight; 


    public TwoDee(Context context) { 
     super(context); 
    } 

    public TwoDee(Context context, AttributeSet attribs) { 
     super(context, attribs); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     Paint paint = new Paint(); 
     paint.setColor(Color.GRAY); 
     paint.setStyle(Style.FILL); 
     canvas.drawPaint(paint); 

     paint.setColor(Color.BLUE); 
     canvas.drawLine(0, 0, mWidth, mHeight, paint); 
     canvas.drawLine(mWidth, 0, 0, mHeight, paint); 

     paint.setColor(Color.RED); 
     canvas.drawText("0 kal", 50, 85, paint); 
     canvas.save(); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     mWidth = View.MeasureSpec.getSize(widthMeasureSpec); 
     mHeight = View.MeasureSpec.getSize(heightMeasureSpec); 
     setMeasuredDimension(mWidth, mHeight); 
    } 
} 


添加自定義視圖到您的活動的XML佈局如下。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello" 
     /> 
     <com.yourapphere.TwoDee 
      android:layout_width="150dp" 
      android:layout_height="100dp" 
     /> 
</LinearLayout> 


沒有進入你的活動類。而已!

如果您確實需要使用ImageView:從ImageView而不是View繼承您的自定義視圖。然後更換您的活動的佈局XML適當的ImageView標籤與像com.yourapphere.MyImageView您的自定義的ImageView)


參考&鏈接
見類似的問題:How to take canvas on imageview in android
簡單的自定義視圖示例代碼:Adding my custom View into an XML layout throws exception

閱讀關於Android 2D繪圖:http://developer.android.com/guide/topics/graphics/2d-graphics.html
Android 2D編碼教程:http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_2D.html
簡單的2D遊戲教程:http://www.barebonescoder.com/2010/06/android-development-simple-2d-graphics-part-1/

+0

現在,我如何從自定義視圖中獲取位圖? – Kalpesh

+0

好的....好的...我知道了...謝謝你,非常感謝你,先生...詹姆斯先生,你好嗎? – Kalpesh

+0

如果能幫助解決你的問題,請你註冊並接受這個答案嗎? –

1

這裏是爲那些有同樣的問題,你要設置你的畫布

public class TestActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    TestCanvas tcanvas=new TestCanvas(); 

    frameLayout=(FrameLayout)findViewById(R.id.frameLayout1); 
    frameLayout.addView(tcanvas); 

    Bitmap bitmap=Bitmap.createBitmap(440,587,Bitmap.Config.ARGB_8888); 
    Canvas c=new Canvas(bitmap); 
    tcanvas.draw(bitmap); 

    //now i use bitmap at for any use....... 

    } 
} 
相關問題