2011-04-10 71 views
0

我想使用已經在外部類中創建的畫布。 但是,該應用程序不運行。這裏是我的MyImge代碼,其中活動開始在Android中使用畫布

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.widget.ImageView; 


public class MyImage2 extends Activity { 
    DemoView draw; 
    private int  imageSizeX = 2047; 
    private int  imageSizeY = 2047; 
    private int  current_drawable = R.drawable.image; 



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

    Map<String, Integer> map = new HashMap<String, Integer>(); 
    map.put("blah", current_drawable); 

    ImageView img=(ImageView)findViewById(R.id.imageView); 

    Bitmap bmp=BitmapFactory.decodeResource(getResources() ,current_drawable); 
    draw = (DemoView)findViewById(R.id.demo); 

    imageSizeX = bmp.getWidth()/2; 
    imageSizeY = bmp.getHeight()/2; 
    Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, imageSizeX, imageSizeY, true); 
    img.setImageBitmap(resizedbitmap);  


    } 
} 

和我DemoView如下

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

import android.view.View; 

public class DemoView extends View{ 
    public DemoView(Context context){ 
     super(context); 
    } 

    @Override protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 


     Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.FILL); 

     // make the entire canvas white 
     paint.setColor(Color.WHITE); 
     canvas.drawPaint(paint); 

     paint.setAntiAlias(false); 
     paint.setColor(Color.GREEN); 
     canvas.drawRect(100, 5, 200, 30, paint); 
     canvas.drawLine(0, 300 , 320, 300, paint); 

    } 
} 

佈局

<?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" 
    > 
     <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="fill_parent"  
     android:scaleType="matrix"   
     android:layout_height="525px"> 
    </ImageView> 

    <view class="com.test.DemoView" 
    android:id="@+id/demo" 
     android:layout_width="fill_parent" 
     android:layout_height="125px"/> 
</LinearLayout> 

如果刪除標籤com.myimage2.DemoView我可以看到形象,但我的目標是看到圖像和畫布。有人可以幫忙嗎?

錯誤日誌:

!ENTRY com.android.ide.eclipse.adt 2 0 2011-04-10 02:19:27.204 
!MESSAGE AndroidManifest: Ignoring unknown 'com.test.DemoView' XML element 
!ENTRY com.android.ide.eclipse.adt 2 0 2011-04-10 02:19:27.891 
!MESSAGE AndroidManifest: Ignoring unknown 'view' XML element 

提前非常感謝。

+0

嘗試清理該項目。視圖元素應該是已知的。 – MByD 2011-04-10 01:39:34

回答

1

我認爲您需要提供其他構造函數(可以處理AttributeSet的構造函數),以便從xml實例化類。嘗試添加DemoView(上下文上下文,AttributeSet attrs)。

+0

非常感謝!這工作。但是你能否解釋一下另一個構造函數的原因。 – Alanagh 2011-04-10 15:35:13

+0

該框架只是想把你的XML參數。所以你需要提供一個可以處理這些的構造函數。您的(舊)號碼可能未被呼叫,可能會被刪除。 – devisnik 2011-04-11 16:12:20

1

我不知道你想在你的onCreate方法中的代碼「MyImage2」中做什麼。

你只需要2行設置畫布視圖:

draw=new DemoView(this); 
setContentView(draw); 

通過您的內容視圖設置爲主要佈局,畫布將永遠不會被看到。所以不要使用主佈局,只需在畫布上打印所有內容。

希望這會幫助你。